chore(u/bk-export-min-pubkeys):make script executable
[BK-2020-03.git] / unitproc / bktemp-checkAppFileDir
1 #!/bin/bash
2 # Desc: Checks that apps, files, or dirs exist.
3
4 #==BEGIN Define script parameters==
5 #===BEGIN Define variables===
6 declare -Ag appRollCall # Associative array for storing app status
7 declare -Ag fileRollCall # Associative array for storing file status
8 declare -Ag dirRollCall # Associative array for storing dir status
9 #===END Define variables===
10
11 #===BEGIN Declare local script functions===
12 checkapp() {
13 # Desc: If arg is a command, save result in assoc array 'appRollCall'
14 # Usage: checkapp arg1 arg2 arg3 ...
15 # Version: 0.1.1
16 # Input: global assoc. array 'appRollCall'
17 # Output: adds/updates key(value) to global assoc array 'appRollCall'
18 # Depends: bash 5.0.3
19 local returnState
20
21 #===Process Args===
22 for arg in "$@"; do
23 if command -v "$arg" 1>/dev/null 2>&1; then # Check if arg is a valid command
24 appRollCall[$arg]="true";
25 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
26 else
27 appRollCall[$arg]="false"; returnState="false";
28 fi;
29 done;
30
31 #===Determine function return code===
32 if [ "$returnState" = "true" ]; then
33 return 0;
34 else
35 return 1;
36 fi;
37 } # Check that app exists
38 checkfile() {
39 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
40 # Usage: checkfile arg1 arg2 arg3 ...
41 # Version: 0.1.1
42 # Input: global assoc. array 'fileRollCall'
43 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
44 # Output: returns 0 if app found, 1 otherwise
45 # Depends: bash 5.0.3
46 local returnState
47
48 #===Process Args===
49 for arg in "$@"; do
50 if [ -f "$arg" ]; then
51 fileRollCall["$arg"]="true";
52 if ! [ "$returnState" = "false" ]; then returnState="true"; fi;
53 else
54 fileRollCall["$arg"]="false"; returnState="false";
55 fi;
56 done;
57
58 #===Determine function return code===
59 if [ "$returnState" = "true" ]; then
60 return 0;
61 else
62 return 1;
63 fi;
64 } # Check that file exists
65 checkdir() {
66 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
67 # Usage: checkdir arg1 arg2 arg3 ...
68 # Version 0.1.2
69 # Input: global assoc. array 'dirRollCall'
70 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
71 # Output: returns 0 if all args are dirs; 1 otherwise
72 # Depends: Bash 5.0.3
73 local returnState
74
75 #===Process Args===
76 for arg in "$@"; do
77 if [ -z "$arg" ]; then
78 dirRollCall["(Unspecified Dirname(s))"]="false"; returnState="false";
79 elif [ -d "$arg" ]; then
80 dirRollCall["$arg"]="true";
81 if ! [ "$returnState" = "false" ]; then returnState="true"; fi
82 else
83 dirRollCall["$arg"]="false"; returnState="false";
84 fi
85 done
86
87 #===Determine function return code===
88 if [ "$returnState" = "true" ]; then
89 return 0;
90 else
91 return 1;
92 fi
93 } # Check that dir exists
94 displayMissing() {
95 # Desc: Displays missing apps, files, and dirs
96 # Usage: displayMissing
97 # Version 0.1.1
98 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
99 # Output: stderr: messages indicating missing apps, file, or dirs
100 # Depends: bash 5, checkAppFileDir()
101 local missingApps value appMissing missingFiles fileMissing
102 local missingDirs dirMissing
103
104 #==BEGIN Display errors==
105 #===BEGIN Display Missing Apps===
106 missingApps="Missing apps :";
107 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
108 for key in "${!appRollCall[@]}"; do
109 value="${appRollCall[$key]}";
110 if [ "$value" = "false" ]; then
111 #echo "DEBUG:Missing apps: $key => $value";
112 missingApps="$missingApps""$key ";
113 appMissing="true";
114 fi;
115 done;
116 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
117 echo "$missingApps" 1>&2;
118 fi;
119 unset value;
120 #===END Display Missing Apps===
121
122 #===BEGIN Display Missing Files===
123 missingFiles="Missing files:";
124 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
125 for key in "${!fileRollCall[@]}"; do
126 value="${fileRollCall[$key]}";
127 if [ "$value" = "false" ]; then
128 #echo "DEBUG:Missing files: $key => $value";
129 missingFiles="$missingFiles""$key ";
130 fileMissing="true";
131 fi;
132 done;
133 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
134 echo "$missingFiles" 1>&2;
135 fi;
136 unset value;
137 #===END Display Missing Files===
138
139 #===BEGIN Display Missing Directories===
140 missingDirs="Missing dirs:";
141 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
142 for key in "${!dirRollCall[@]}"; do
143 value="${dirRollCall[$key]}";
144 if [ "$value" = "false" ]; then
145 #echo "DEBUG:Missing dirs: $key => $value";
146 missingDirs="$missingDirs""$key ";
147 dirMissing="true";
148 fi;
149 done;
150 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
151 echo "$missingDirs" 1>&2;
152 fi;
153 unset value;
154 #===END Display Missing Directories===
155
156 #==END Display errors==
157 } # Display missing apps, files, dirs
158
159 #===END Declare local script functions===
160 #==END Define script parameters==
161
162 #==BEGIN sample code==
163 if checkapp cat; then echo "cat found."; fi;
164 if checkapp gpg; then echo "gpg found."; fi;
165 if checkapp emaaaacs; then echo "emaaaacs found."; fi;
166 sleep 1;
167 displayMissing;
168 #==END sample code==
169
170 # Author: Steven Baltaktei Sandoval
171 # License: GPLv3+