diff --git a/server/scripts/auto-update.sh b/server/scripts/auto-update.sh index dbee6bc..528d4ee 100755 --- a/server/scripts/auto-update.sh +++ b/server/scripts/auto-update.sh @@ -8,6 +8,10 @@ WARNING=$(tput setaf 3) INFO=$(tput setaf 6) RESET=$(tput sgr0) +# Params + +BUILD_BINARY="n" + # Vars REPO_URL="https://github.com/DavidGomesDev/RustyController" @@ -15,9 +19,63 @@ RUSTY_HOME_DIR="$HOME/RustyController" BINARY_PATH="$RUSTY_HOME_DIR/server/target/release/rusty_controller" HASH_FILE="$RUSTY_HOME_DIR/current.sha256" +# Get params + +show_usage () { + echo "Parameters:" + echo "-b: build binary (instead of downloading the latest release)" +} + +while getopts "bh" opt; do + case ${opt} in + b) + BUILD_BINARY="y" + ;; + h) + show_usage + exit 0 + ;; + ?) + echo "Invalid option: -${OPTARG}." + show_usage + exit 1 + ;; + esac +done + +echo "$START* Updating server$RESET" + cd "$HOME" || exit 1 +download_latest () { + # Get arch (and trim output) + arch=$(dpkg --print-architecture | xargs echo -n) + + mkdir -p target/release || exit 1 + wget -q "$REPO_URL/releases/download/latest/server-$arch" -O target/release/rusty_controller || exit 1 + + echo "$INFO* Downloaded latest release binary!$RESET" +} + build () { + echo + echo "$INFO* Updating crates...$RESET" + echo + + cargo update -q || exit 1 + + echo "$INFO* Build...$RESET" + echo + + time cargo build --release -q || exit 1 + + newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}') + + echo "$newest_hash" > "$HASH_FILE" + echo "$SUCCESS* Built successfully!$RESET" +} + +update () { echo "$INFO* Checking out main...$RESET" echo @@ -30,21 +88,12 @@ build () { cd server/ || exit 1 - echo - echo "$INFO* Updating crates...$RESET" - echo - - cargo update -q || exit 1 - - echo "$INFO* Build...$RESET" - echo - - time cargo build --release -q || exit 1 - - newest_hash=$(sha256sum "$BINARY_PATH" | gawk '{print $1}') + if [[ "$BUILD_BINARY" == "y" ]]; then + build + else + download_latest + fi - echo "$newest_hash" > "$HASH_FILE" - echo "$SUCCESS* Built successfully!$RESET" cd .. } @@ -54,10 +103,10 @@ launch () { } if [[ ! -d "$RUSTY_HOME_DIR" ]]; then - echo "$WARNING* Rusty not found. Cloning...$RESET" + echo "$WARNING* Rusty repo not found. Cloning...$RESET" git clone "$REPO_URL" - build + update launch exit 0 fi @@ -67,13 +116,13 @@ cd "$RUSTY_HOME_DIR" || exit 1 if [[ -f "$HASH_FILE" ]]; then if [ ! -f "$BINARY_PATH" ]; then echo "$INFO* Binary not found, building...$RESET" - build + update launch exit 0 fi current_hash=$(cat "$HASH_FILE") - build + update if [[ "$current_hash" != "$newest_hash" ]]; then echo "$INFO* Built a new version!$RESET" launch @@ -82,6 +131,6 @@ if [[ -f "$HASH_FILE" ]]; then fi else echo "$WARNING* Couldn't find current hash. Updating to latest version anyway.$RESET" - build + update launch fi