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