2 # Desc: Scan file system and update '.hidden' files to hide certain
3 # files based on filenames.
4 # Usage: bk-naut-hide [DIRS...]
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
10 # Desc: If arg is a command, save result in assoc array 'appRollCall'
11 # Usage: checkapp arg1 arg2 arg3 ...
13 # Input: global assoc. array 'appRollCall'
14 # Output: adds/updates key(value) to global assoc array 'appRollCall'
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;
24 appRollCall
[$arg]="false"; returnState
="false";
28 #===Determine function return code===
29 if [ "$returnState" = "true" ]; then
34 } # Check that app exists
36 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
37 # Usage: checkfile arg1 arg2 arg3 ...
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
47 if [ -f "$arg" ]; then
48 fileRollCall
["$arg"]="true";
49 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
51 fileRollCall
["$arg"]="false"; returnState
="false";
55 #===Determine function return code===
56 if [ "$returnState" = "true" ]; then
61 } # Check that file exists
63 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
64 # Usage: checkdir arg1 arg2 arg3 ...
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
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
80 dirRollCall
["$arg"]="false"; returnState
="false";
84 #===Determine function return code===
85 if [ "$returnState" = "true" ]; then
90 } # Check that dir exists
92 # Desc: Displays missing apps, files, and dirs
93 # Usage: displayMissing
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
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 ";
114 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
115 echo "$missingApps" 1>&2;
118 #===END Display Missing Apps===
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 ";
131 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
132 echo "$missingFiles" 1>&2;
135 #===END Display Missing Files===
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 ";
148 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
149 echo "$missingDirs" 1>&2;
152 #===END Display Missing Directories===
154 #==END Display errors==
155 #==BEGIN Determine function return code===
156 if [ "$appMissing" == "true" ] ||
[ "$fileMissing" == "true" ] ||
[ "$dirMissing" == "true" ]; then
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
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;
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
179 local -a dirs_target pat_tohide
182 # check and specify list of dirs to search
184 if [[ -d $arg ]]; then
185 dirs_target
+=("$arg");
187 die
"FATAL:Not a dir:arg:$arg";
190 yell
"DEBUG:dirs_target:${dirs_target[*]}";
192 # specify filename regex patterns to mark '.hidden'
193 pat_tohide
+=(".ots$");
194 pat_tohide
+=(".ots.bak$");
195 yell
"DEBUG:pat_tohide:${pat_tohide[*]}";
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");
204 yell
"DEBUG:ls_dothide_f:${ls_dothide_f[*]}";
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[*]}";
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'
222 while read -r file; do
223 yell
"DEBUG:considering file:$file";
224 filename
="$(basename "$file")";
226 ## check if file is actually a file
227 if [[ ! -f $file ]]; then continue; fi;
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
236 flag_pat_match
="false";
238 done < <(printf "%s\n" "${pat_tohide[@]}");
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");
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
);
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[@]}");
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"
266 ## unset variables defined only for this dir
267 unset ls_tohide_fn path_hidefile
;
270 done < <(printf "%s\n" "${ls_dothide_d[@]}");