Awful
[openitg.git] / common.sh
1 # common.sh - defines common operations so we can quickly check the
2 # sanity of the environment our scripts run in.
3
4 # Somewhat counterintuitively, these return 0 if true and 1 if false.
5 # That's so we can set -e and do sanity checking quickly.
6
7 #
8 # global constants, relative to the repository root
9 #
10
11 ASSETS_DIR="assets"
12
13 ITG2_UTIL_DIR="$ASSETS_DIR/utilities/itg2-util/src"
14 ITG2_UTIL_FILE="itg2ac-util"
15 ITG2_UTIL_PATH="$ITG2_UTIL_DIR/$ITG2_UTIL_FILE"
16
17 VERIFY_SIG_DIR="src/verify_signature/java"
18 VERIFY_SIG_FILE="SignFile.java"
19 VERIFY_SIG_PATH="$VERIFY_SIG_DIR/$VERIFY_SIG_FILE"
20
21 #
22 # generic sanity checking functions and their generic error messages
23 #
24
25 COMMAND_ERROR="Command \"%s\" not found! Please fix that so we can continue."
26 FILE_ERROR="File \"%s\" not found! We need that to continue; please find it."
27
28 # has_command [cmd] [error] - if cmd cannot be found, display "error" if
29 # supplied, (which may use %s to get the cmd name) or use a generic error.
30 function has_command
31 {
32 if [ -z "$1" ]; then
33 echo "$0: no command given to check!"
34 return 1
35 fi
36
37 set +e
38 which $1 &> /dev/null
39 RET="$?"
40 set -e
41
42 if [ $RET -eq 0 ]; then return 0; fi
43
44 # use the custom error message if we have it
45 printf "${2-$COMMAND_ERROR}\n" $1
46 return 1
47 }
48
49 # has_file [file] [error] - has_command, but for files. Same rules apply.
50 function has_file
51 {
52 if [ -z "$1" ]; then
53 echo "$0: no file given to check!"
54 return 1
55 fi
56
57 set +e
58 file "$1" &> /dev/null
59 RET=$?
60 set -e
61
62 if [ $RET -eq 0 ]; then return 0; fi
63
64 # use the custom error message if we have it
65 printf "${2-$FILE_ERROR}\n" $1
66 return 1
67 }