4826b2f065becf15cf467b85f867d9e35001929d
[BK-2020-03.git] / user / bk-naut-hide
1 #!/usr/bin/env bash
2 # Desc: Scan file system and update '.hidden' files to hide certain
3 # files based on filenames.
4 # Usage: bk-naut-hide [DIRS...]
5
6 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
7 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
8 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
9 checkapp() {
10 # Desc: If arg is a command, save result in assoc array 'appRollCall'
11 # Usage: checkapp arg1 arg2 arg3 ...
12 # Version: 0.1.1
13 # Input: global assoc. array 'appRollCall'
14 # Output: adds/updates key(value) to global assoc array 'appRollCall'
15 # Depends: bash 5.0.3
16 local returnState
17
18 #===Process Args===
19 for arg in "$@"; do
20 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
21 appRollCall[$arg]="true";
22 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
23 else
24 appRollCall[$arg]="false"; returnState="false";
25 fi;
26 done;
27
28 #===Determine function return code===
29 if [ "$returnState" = "true" ]; then
30 return 0;
31 else
32 return 1;
33 fi;
34 } # Check that app exists
35 checkfile() {
36 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
37 # Usage: checkfile arg1 arg2 arg3 ...
38 # Version: 0.1.1
39 # Input: global assoc. array 'fileRollCall'
40 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
41 # Output: returns 0 if app found, 1 otherwise
42 # Depends: bash 5.0.3
43 local returnState
44
45 #===Process Args===
46 for arg in "$@"; do
47 if [ -f "$arg" ]; then
48 fileRollCall["$arg"]="true";
49 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
50 else
51 fileRollCall["$arg"]="false"; returnState="false";
52 fi;
53 done;
54
55 #===Determine function return code===
56 if [ "$returnState" = "true" ]; then
57 return 0;
58 else
59 return 1;
60 fi;
61 } # Check that file exists
62 checkdir() {
63 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
64 # Usage: checkdir arg1 arg2 arg3 ...
65 # Version 0.1.2
66 # Input: global assoc. array 'dirRollCall'
67 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
68 # Output: returns 0 if all args are dirs; 1 otherwise
69 # Depends: Bash 5.0.3
70 local returnState
71
72 #===Process Args===
73 for arg in "$@"; do
74 if [ -z "$arg" ]; then
75 dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false";
76 elif [ -d "$arg" ]; then
77 dirRollCall["$arg"]="true";
78 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
79 else
80 dirRollCall["$arg"]="false"; returnState="false";
81 fi
82 done
83
84 #===Determine function return code===
85 if [ "$returnState" = "true" ]; then
86 return 0;
87 else
88 return 1;
89 fi
90 } # Check that dir exists
91 displayMissing() {
92 # Desc: Displays missing apps, files, and dirs
93 # Usage: displayMissing
94 # Version 1.0.0
95 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
96 # Output: stderr: messages indicating missing apps, file, or dirs
97 # Output: returns exit code 0 if nothing missing; 1 otherwise
98 # Depends: bash 5, checkAppFileDir()
99 local missingApps value appMissing missingFiles fileMissing
100 local missingDirs dirMissing
101
102 #==BEGIN Display errors==
103 #===BEGIN Display Missing Apps===
104 missingApps="Missing apps :";
105 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
106 for key in "${!appRollCall[@]}"; do
107 value="${appRollCall[$key]}";
108 if [ "$value" = "false" ]; then
109 #echo "DEBUG:Missing apps: $key => $value";
110 missingApps="$missingApps""$key ";
111 appMissing="true";
112 fi;
113 done;
114 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
115 echo "$missingApps" 1>&2;
116 fi;
117 unset value;
118 #===END Display Missing Apps===
119
120 #===BEGIN Display Missing Files===
121 missingFiles="Missing files:";
122 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
123 for key in "${!fileRollCall[@]}"; do
124 value="${fileRollCall[$key]}";
125 if [ "$value" = "false" ]; then
126 #echo "DEBUG:Missing files: $key => $value";
127 missingFiles="$missingFiles""$key ";
128 fileMissing="true";
129 fi;
130 done;
131 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
132 echo "$missingFiles" 1>&2;
133 fi;
134 unset value;
135 #===END Display Missing Files===
136
137 #===BEGIN Display Missing Directories===
138 missingDirs="Missing dirs:";
139 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
140 for key in "${!dirRollCall[@]}"; do
141 value="${dirRollCall[$key]}";
142 if [ "$value" = "false" ]; then
143 #echo "DEBUG:Missing dirs: $key => $value";
144 missingDirs="$missingDirs""$key ";
145 dirMissing="true";
146 fi;
147 done;
148 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
149 echo "$missingDirs" 1>&2;
150 fi;
151 unset value;
152 #===END Display Missing Directories===
153
154 #==END Display errors==
155 #==BEGIN Determine function return code===
156 if [ "$appMissing" == "true" ] || [ "$fileMissing" == "true" ] || [ "$dirMissing" == "true" ]; then
157 return 1;
158 else
159 return 0;
160 fi
161 #==END Determine function return code===
162 } # Display missing apps, files, dirs
163 get_paths_dothide() {
164 # Desc: Recursively search specified ir for '.hidden' files
165 # Usage: get_paths_dothide arg1
166 # Input: arg1 dir to search
167 # Output: stdout list of paths of '.hidden' files
168
169 local
170 # Check input
171 if [[ $# -ne 1 ]]; then die "FATAL:Arg count does not equal 1:$#"; fi;
172 if [[ ! -d "$1" ]]; then die "FATAL:Not a dir:$1"; fi;
173
174 # Search for files named '.hidden'
175 find -L "$1" -type f -name ".hidden" 2>/dev/null
176 }; # Return list of paths of '.hidden' files
177
178 main() {
179 local -a dirs_target pat_tohide
180
181 # check input
182 # check and specify list of dirs to search
183 for arg in "$@"; do
184 if [[ -d $arg ]]; then
185 dirs_target+=("$arg");
186 else
187 die "FATAL:Not a dir:arg:$arg";
188 fi;
189 done;
190 yell "DEBUG:dirs_target:${dirs_target[*]}";
191
192 # specify filename regex patterns to mark '.hidden'
193 pat_tohide+=(".ots$");
194 pat_tohide+=(".ots.bak$");
195 yell "DEBUG:pat_tohide:${pat_tohide[*]}";
196
197 # generate list of paths of '.hidden' files
198 for dir in "${dirs_target[@]}"; do
199 yell "DEBUG:generating list of paths of '.hidden' files in dir:$dir";
200 while read -r file; do
201 ls_dothide_f+=("$file");
202 done < <(get_paths_dothide "$dir");
203 done;
204 yell "DEBUG:ls_dothide_f:${ls_dothide_f[*]}";
205 yell "DEBUG:";
206
207 # get list of dirs containing the files
208 while read -r line; do
209 yell "DEBUG:found .hidden file at:$line";
210 dir="$(dirname "$line")";
211 ls_dothide_d+=("$dir");
212 done < <(printf "%s\n" "${ls_dothide_f[@]}")
213 yell "DEBUG:ls_dothide_d:${ls_dothide_d[*]}";
214 yell "DEBUG:";
215
216 # for each dir, write list of files to hide in '.hidden'
217 while read -r dir; do
218 yell "DEBUG:performing actions in dir:$dir";
219 declare -a ls_tohide_fn; # array for filenames to write in '.hidden'
220
221 ## act on each file
222 while read -r file; do
223 yell "DEBUG:considering file:$file";
224 filename="$(basename "$file")";
225
226 ## check if file is actually a file
227 if [[ ! -f $file ]]; then continue; fi;
228
229 ## check if file should be hidden according to pattern match
230 unset flag_pat_match;
231 while read -r pat; do
232 if [[ $filename =~ $pat ]]; then
233 flag_pat_match="true";
234 break; # don't consider any more patterns for this file
235 else
236 flag_pat_match="false";
237 fi;
238 done < <(printf "%s\n" "${pat_tohide[@]}");
239
240 ## add file to list to add to '.hidden'
241 if [[ $flag_pat_match == "true" ]]; then
242 yell "DEBUG:adding to ls_tohide_fn:file:$file":
243 yell "DEBUG:filename:$filename";
244 ls_tohide_fn+=("$filename");
245 fi;
246 yell "DEBUG:";
247
248 ## unset variables defined only for this file
249 unset filename flag_pat_match;
250 done < <(find "$dir" -mindepth 1 -maxdepth 1 -type f 2>/dev/null);
251 yell "DEBUG:";
252
253 ## add file names to $dir/'.hidden'
254 ### remove blank lines and sort and uniq
255 while read -r line; do
256 ls_tohide_fn_sortuniq+=("$line");
257 done < <(printf "%s\n" "${ls_tohide_fn[@]}" | sed '/^$/d' | sort | uniq);
258 ls_tohide_fn=("${ls_tohide_fn_sortuniq[@]}");
259
260 ### write '.hidden'
261 path_hidefile="$dir"/.hidden;
262 yell "DEBUG:Writing contents of .hidden file at path_hidefile:$path_hidefile";
263 yell "DEBUG:ls_tohide_fn:$(printf "%s\n" "${ls_tohide_fn[@]}" )";
264 printf "%s\n" "${ls_tohide_fn[@]}" > "$path_hidefile"
265
266 ## unset variables defined only for this dir
267 unset ls_tohide_fn path_hidefile;
268
269 yell "DEBUG:";
270 done < <(printf "%s\n" "${ls_dothide_d[@]}");
271 }; # main program
272
273 main "$@";