@@ -8,16 +8,74 @@ WARNING=$(tput setaf 3)
88INFO=$( tput setaf 6)
99RESET=$( tput sgr0)
1010
11+ # Params
12+
13+ BUILD_BINARY=" n"
14+
1115# Vars
1216
1317REPO_URL=" https://github.com/DavidGomesDev/RustyController"
1418RUSTY_HOME_DIR=" $HOME /RustyController"
1519BINARY_PATH=" $RUSTY_HOME_DIR /server/target/release/rusty_controller"
1620HASH_FILE=" $RUSTY_HOME_DIR /current.sha256"
1721
22+ # Get params
23+
24+ show_usage () {
25+ echo " Parameters:"
26+ echo " -b: build binary (instead of downloading the latest release)"
27+ }
28+
29+ while getopts " bh" opt; do
30+ case ${opt} in
31+ b)
32+ BUILD_BINARY=" y"
33+ ;;
34+ h)
35+ show_usage
36+ exit 0
37+ ;;
38+ ? )
39+ echo " Invalid option: -${OPTARG} ."
40+ show_usage
41+ exit 1
42+ ;;
43+ esac
44+ done
45+
46+ echo " $START * Updating server$RESET "
47+
1848cd " $HOME " || exit 1
1949
50+ download_latest () {
51+ # Get arch (and trim output)
52+ arch=$( dpkg --print-architecture | xargs echo -n)
53+
54+ mkdir -p target/release || exit 1
55+ wget -q " $REPO_URL /releases/download/latest/server-$arch " -O target/release/rusty_controller || exit 1
56+
57+ echo " $INFO * Downloaded latest release binary!$RESET "
58+ }
59+
2060build () {
61+ echo
62+ echo " $INFO * Updating crates...$RESET "
63+ echo
64+
65+ cargo update -q || exit 1
66+
67+ echo " $INFO * Build...$RESET "
68+ echo
69+
70+ time cargo build --release -q || exit 1
71+
72+ newest_hash=$( sha256sum " $BINARY_PATH " | gawk ' {print $1}' )
73+
74+ echo " $newest_hash " > " $HASH_FILE "
75+ echo " $SUCCESS * Built successfully!$RESET "
76+ }
77+
78+ update () {
2179 echo " $INFO * Checking out main...$RESET "
2280 echo
2381
@@ -30,21 +88,12 @@ build () {
3088
3189 cd server/ || exit 1
3290
33- echo
34- echo " $INFO * Updating crates...$RESET "
35- echo
36-
37- cargo update -q || exit 1
38-
39- echo " $INFO * Build...$RESET "
40- echo
41-
42- time cargo build --release -q || exit 1
43-
44- newest_hash=$( sha256sum " $BINARY_PATH " | gawk ' {print $1}' )
91+ if [[ " $BUILD_BINARY " == " y" ]]; then
92+ build
93+ else
94+ download_latest
95+ fi
4596
46- echo " $newest_hash " > " $HASH_FILE "
47- echo " $SUCCESS * Built successfully!$RESET "
4897 cd ..
4998}
5099
@@ -54,10 +103,10 @@ launch () {
54103}
55104
56105if [[ ! -d " $RUSTY_HOME_DIR " ]]; then
57- echo " $WARNING * Rusty not found. Cloning...$RESET "
106+ echo " $WARNING * Rusty repo not found. Cloning...$RESET "
58107 git clone " $REPO_URL "
59108
60- build
109+ update
61110 launch
62111 exit 0
63112fi
@@ -67,13 +116,13 @@ cd "$RUSTY_HOME_DIR" || exit 1
67116if [[ -f " $HASH_FILE " ]]; then
68117 if [ ! -f " $BINARY_PATH " ]; then
69118 echo " $INFO * Binary not found, building...$RESET "
70- build
119+ update
71120 launch
72121 exit 0
73122 fi
74123
75124 current_hash=$( cat " $HASH_FILE " )
76- build
125+ update
77126 if [[ " $current_hash " != " $newest_hash " ]]; then
78127 echo " $INFO * Built a new version!$RESET "
79128 launch
@@ -82,6 +131,6 @@ if [[ -f "$HASH_FILE" ]]; then
82131 fi
83132else
84133 echo " $WARNING * Couldn't find current hash. Updating to latest version anyway.$RESET "
85- build
134+ update
86135 launch
87136fi
0 commit comments