~dricottone/my-utils

my-utils/core/enumerate -rwxr-xr-x 2.8 KiB
a5400e39Dominic Ricottone Starting point for cryptographic utilities 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash

name="enumerate"
version="1.0"
read -r -d '' help_message <<-EOF
	Rename files in current directory into sequential numbers
	Usage: enumerate [OPTIONS]
	Options:
	 -d, --dry-run     print, not execute, changes (Default)
	 -f P, --filter P  filter targets with filename pattern P
	 -h, --help        print this message and exit
	 -q, --quiet       suppress error messages
	 -s N, --start N   start enumeration at N (Default: 1)
	 -S N, --step N    step enumeration by N (Default: 1)
	 -v, --verbose     show additional messages
	 -V, --version     print version number and exit
	 -w N, --width N   rename files to numbers N wide (Default: 4)
	 -x, --execute     execute changes
EOF

source /usr/local/lib/mylib.bash

positional=()
execute=0
filter="*"
enum_start=1
enum_step=1
width=4
quiet=0
verbose=0

while [[ $# -gt 0 ]]; do
  case $1 in

  -f|--filter)
    debug_msg "Setting filter option to ${2} (was ${filter})"
    filter="$2"
    shift; shift
    ;;

  -h|--help)
    help_msg
    shift
    ;;

  -q|--quiet)
    debug_msg "Setting quiet option to 1 (was ${quiet})"
    quiet=1
    shift
    ;;

  -s|--start)
    if ! is_natural "$2"; then
      error_msg "Cannot set enumeration start to '${2}' (not an integer >= 0)"
    fi
    debug_msg "Setting enumeration start to ${2} (was ${enum_start})"
    enum_start="$2"
    shift; shift
    ;;

  -S|--step)
    if ! is_positive_integer "$2"; then
      error_msg "Cannot set enumeration step to '${2}' (not an integer >= 1)"
    fi
    debug_msg "Setting enumeration step to ${2} (was ${enum_step})"
    enum_step="$2"
    shift; shift
    ;;

  -v|--verbose)
    debug_msg "Setting verbose option to 1 (was ${verbose})"
    verbose=1
    shift
    ;;

  -V|--version)
    version_msg
    ;;

  -w|--width)
    if ! is_natural "$2"; then
      error_msg "Cannot set filename width to '${2}' (not an integer >= 0)"
    fi
    debug_msg "Setting filename width to ${2} (was ${width})"
    width="$2"
    shift; shift
    ;;

  -x|--execute)
    debug_msg "Setting execution to 1 (was ${execute})"
    execute=1
    shift
    ;;

  *)
    debug_msg "Argument '${1}' added to positional array"
    positional+=("$1")
    shift
    ;;
  esac
done

# main routine
n="$enum_start"
s="$enum_step"
code=0
find . -maxdepth 1 -name "$filter" -type f -printf "%f\0" \
  | sort -z \
  | while IFS= read -r -d '' original_fn; do
  wide_n=$(/usr/bin/printf "%0*d\n" "$width" "$n")
  enum_fn="${wide_n}.$(fn_extension "$original_fn")"
  debug_msg "Widened format of '${n}' is '${wide_n}'"
  debug_msg "Input filename is '${original_fn}'"
  debug_msg "Output filename is '${enum_fn}'"
  if [[ "$execute" -eq 1 ]]; then
    if ! mv "$original_fn" "${enum_fn}"; then
      code=1
    fi
  else
    /usr/bin/printf "#mv %s %s\n" "$original_fn" "$enum_fn"
  fi
  n=$((n+s))
done

# return stored code
exit "$code"