Awful
[openitg.git] / gen-arcade-patch.sh
1 #!/bin/bash
2
3 # Usage: ./gen-arcade-patch [private RSA]
4 #
5 # Generates a full arcade patch from the assets data, then signs the patch
6 # with the Private.rsa file given on the command line.
7
8 # written by vyhd/pat
9
10 # work directory - this forms the root dir of the final patch
11 TMP_DIR="/tmp/.tmp-patch"
12
13 # arcade patch data that we copy wholesale to the temp dir
14 PATCH_DATA_DIR="assets/arcade-patch"
15
16 # values that aren't really magical, just used in a few places
17 OITG_BINARY="src/openitg"
18 CONFIG_HEADER="src/config.h"
19
20 # default to private.rsa in CWD (for the sake of a default value)
21 PRIVATE_RSA=${1-private.rsa}
22
23 # include the simple helper routines
24 source common.sh
25
26 # exit immediately on nonzero exit code
27 set -e
28 set -u
29 # set this for loquacious and slightly redundant verbosity
30 # set -x
31
32 function print_usage
33 {
34 echo "Usage: $0 [private RSA key]"
35 exit 0
36 }
37
38 if ! [ -f "$PRIVATE_RSA" ]; then print_usage; fi
39
40 # clean up after ourselves on exit
41 trap "rm -rf $TMP_DIR" EXIT
42 trap "echo \"Failed! \"" ERR
43
44 #
45 # see if we have all the functions and files we need here
46 #
47
48 echo "Checking dependencies..."
49
50 has_command javac
51 has_command java
52
53 has_file "$OITG_BINARY" "%s doesn't exist! Please build it."
54 has_file "$CONFIG_HEADER" "Why do you have a binary, but not config.h?"
55 has_file "$VERIFY_SIG_PATH" "%s is missing; try checking out the repo again?"
56 has_file "$PRIVATE_RSA" "RSA key \"%s\" not found! (failed sanity check)"
57
58 #
59 # warn the user if the util doesn't exist
60 has_file "assets/utilities/itg2-util/src/itg2ac-util" "cd assets/utilities/itg2-util && ./autogen.sh && ./configure && make"
61
62 #
63 # build the signature verification program if it hasn't been built
64 # - we usually make them build our utilities, but this is simple
65 #
66
67 if ! [ -f "${VERIFY_SIG_PATH%.*}.class" ]; then
68 echo "SignFile not built yet. building now..."
69 cd "$VERIFY_SIG_DIR"
70 javac "$VERIFY_SIG_FILE"
71 cd -
72 fi
73
74 #
75 # actually build the patch here
76 #
77
78 OITG_DATE="`date +%m-%d-%Y`"
79 OITG_VERSION="`git describe`"
80
81 if [ $? -ne 0 ]; then
82 echo "'git describe' failed! Are you working in a Git repo?"
83 exit 1
84 fi
85
86 # sanity check - make sure this build is actually for arcades
87 set +e
88
89 grep "#define ITG_ARCADE 1" "$CONFIG_HEADER" &> /dev/null
90
91 if [ $? -ne 0 ]; then
92 echo "Your config.h says this isn't an arcade build. Aborting."
93 exit 1
94 fi
95
96 set -e
97
98 mkdir -p "$TMP_DIR"
99
100 echo "Copying base patch data..."
101 cp -a $PATCH_DATA_DIR/* "$TMP_DIR"
102
103 echo "Generating patch.zip..."
104 ./gen-patch-zip.sh "$TMP_DIR/patch.zip" &> /dev/null
105
106 echo "Copying and stripping binary..."
107 cp -L "$OITG_BINARY" "$TMP_DIR"
108 strip --strip-unneeded "$TMP_DIR/openitg"
109
110 echo "Reticulating splines..."
111
112 # replace the placeholders in patch.xml with our actual data
113 sed -r -i -e "s/OITG_VERSION/$OITG_VERSION/g" "$TMP_DIR/patch.xml"
114 sed -r -i -e "s/OITG_DATE/$OITG_DATE/g" "$TMP_DIR/patch.xml"
115
116 echo "Zipping patch data..."
117
118 # NOTE: the "ITG 2 " prefix is required for the patches to be seen by itg/openitg
119 PATCH_OUTPUT_FILE="ITG 2 OpenITG-$OITG_VERSION.itg"
120 TEMP_SIG_FILE="$TMP_DIR/.sig"
121
122 # delete this if it exists, so we don't just "update"
123 rm -f "$PATCH_OUTPUT_FILE"
124
125 CWD="`pwd`"
126 cd "$TMP_DIR"
127 zip -r "$CWD/$PATCH_OUTPUT_FILE" * &> /dev/null
128 cd - &> /dev/null
129
130 echo "Signing and appending signature..."
131
132 java -classpath $VERIFY_SIG_DIR SignFile "$PATCH_OUTPUT_FILE" "$PRIVATE_RSA" "$TEMP_SIG_FILE"
133 cat "$TEMP_SIG_FILE" >> "$PATCH_OUTPUT_FILE"
134
135 echo "Done: $PATCH_OUTPUT_FILE"
136 exit 0