-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdich.sh
143 lines (120 loc) · 3.32 KB
/
dich.sh
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
CONFIG_FILE="config.json"
read_json_config() {
suffix=$(jq -r '.suffix' "$CONFIG_FILE")
directory=$(jq -r '.directory' "$CONFIG_FILE")
target=$(jq -r '.target' "$CONFIG_FILE")
}
initialize_config_file() {
if [ ! -s "$CONFIG_FILE" ]; then
echo '{"suffix": "", "directory": "", "target": "", "files": []}' > "$CONFIG_FILE"
fi
}
init_mode() {
initialize_config_file
read_json_config
if [ -d "$directory" ]; then
files=($(find "$directory" -type f -name "${suffix}*"))
if [ ${#files[@]} -eq 0 ]; then
echo "No files with suffix '$suffix' found in directory '$directory'."
exit 1
fi
jq --argjson files "$(printf '%s\n' "${files[@]}" | jq -R . | jq -s .)" \
'.files = $files' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && \
mv "$CONFIG_FILE.tmp" "$CONFIG_FILE"
echo "File list initialized and saved to $CONFIG_FILE."
if [ ! -f "$target" ]; then
cp "${files[0]}" "$target"
echo "Target file not found. Created target by copying ${files[0]} to $target."
fi
else
echo "Directory '$directory' not found."
exit 1
fi
}
get_mode() {
read_json_config
files=$(jq -r '.files[]' "$CONFIG_FILE")
for file in $files; do
if cmp -s "$file" "$target"; then
echo "Match found: $file"
return 0
fi
done
echo "No matching file found."
}
next_mode() {
read_json_config
files=($(jq -r '.files[]' "$CONFIG_FILE"))
match_found=false
for i in "${!files[@]}"; do
if cmp -s "${files[$i]}" "$target"; then
next_index=$(( (i + 1) % ${#files[@]} ))
cp "${files[$next_index]}" "$target"
echo "Copied ${files[$next_index]} to $target."
match_found=true
break
fi
done
if ! $match_found; then
echo "No matching file found for $target. No changes made."
fi
}
set_mode() {
read_json_config
files=($(jq -r '.files[]' "$CONFIG_FILE"))
echo "Choose a file to set as the target:"
select choice in "${files[@]}"; do
if [ -n "$choice" ]; then
cp "$choice" "$target"
echo "Copied $choice to $target."
return 0
else
echo "Invalid selection. Please try again."
fi
done
}
help_mode() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -i Initialize mode: Reads the configuration, lists files with a given suffix,"
echo " and stores them in the configuration file. If the target file does not exist,"
echo " it will create one by copying the first file in the list."
echo
echo " -g Get mode: Compares each file in the list to the target file and outputs the"
echo " filename of the matching file, if found."
echo
echo " -n Next mode: Finds the file in the list that matches the target, then copies"
echo " the next file in the list to overwrite the target."
echo
echo " -s Set mode: Interactively allows the user to choose a file from the list to"
echo " set as the target, overwriting the current target file."
echo
echo " -h Display this help message and exit."
}
while getopts ":ignsh" opt; do
case $opt in
i)
init_mode
;;
g)
get_mode
;;
n)
next_mode
;;
s)
set_mode
;;
h)
help_mode
exit 0
;;
\?)
echo "Invalid option: -$OPTARG" >&2
help_mode
exit 1
;;
esac
done