|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# ================ |
| 4 | +# TERMINAL COLORS~ |
| 5 | +# ================ |
| 6 | +BOLD=`tput bold` |
| 7 | +UNDERLINE=`tput smul` |
| 8 | +RED_TEXT=`tput setaf 1` |
| 9 | +GREEN_TEXT=`tput setaf 2` |
| 10 | +YELLOW_TEXT=`tput setaf 3` |
| 11 | +BLUE_TEXT=`tput setaf 4` |
| 12 | +RESET=`tput sgr0` |
| 13 | + |
| 14 | +CURRENT_DIR=$(pwd) |
| 15 | +# source: https://stackoverflow.com/questions/59895/how-do-i-get-the-directory-where-a-bash-script-is-located-from-within-the-script |
| 16 | +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) |
| 17 | +cd ../$SCRIPT_DIR |
| 18 | + |
| 19 | +# ======================== |
| 20 | +# UPSTREAM VERSION CHECKER |
| 21 | +# ======================== |
| 22 | + |
| 23 | +# Reads the version number in package.json |
| 24 | +# src: https://gist.github.com/DarrenN/8c6a5b969481725a4413 |
| 25 | +# + https://stackoverflow.com/questions/428109/extract-substring-in-bash to remove space in front |
| 26 | +CURRENT_VERSION=${$(cat package.json \ |
| 27 | + | grep version \ |
| 28 | + | head -1 \ |
| 29 | + | awk -F: '{ print $2 }' \ |
| 30 | + | sed 's/[",]//g')#* } |
| 31 | + |
| 32 | +# Remove anything past the .'s in CURRENT_VERSION |
| 33 | +# src: https://stackoverflow.com/questions/428109/extract-substring-in-bash to remove space in front |
| 34 | +CURRENT_MAJOR_VERSION=${CURRENT_VERSION%\.*\.*} |
| 35 | +RAW_CONTENT_URL=https://raw.githubusercontent.com/ayubun/discord-ttl/v${CURRENT_MAJOR_VERSION} |
| 36 | + |
| 37 | +# Using the major version, grab the latest package.json version number on the ttl repo |
| 38 | +UPSTREAM_VERSION=${$(curl -s ${RAW_CONTENT_URL}/package.json \ |
| 39 | + | grep version \ |
| 40 | + | head -1 \ |
| 41 | + | awk -F: '{ print $2 }' \ |
| 42 | + | sed 's/[",]//g')#* } |
| 43 | + |
| 44 | +# We will check this JUST in case the major versions are mismatched (this shouldn't happen but /shrug ppl make mistakes) |
| 45 | +UPSTREAM_MAJOR_VERSION=${UPSTREAM_VERSION%\.*\.*} |
| 46 | + |
| 47 | +if [[ $CURRENT_MAJOR_VERSION != $UPSTREAM_MAJOR_VERSION ]]; then |
| 48 | + echo "${RESET}${RED_TEXT}[${BOLD}ERROR${RESET}${RED_TEXT}]${RESET}${BOLD}${YELLOW_TEXT} The upstream major version is unexpected! The updater will only update the container(s) now.${RESET}" |
| 49 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling any new docker image(s)...${RESET}" |
| 50 | + docker-compose pull |
| 51 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Upping container(s)...${RESET}" |
| 52 | + docker-compose up -d |
| 53 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${GREEN_TEXT} Done! Discord TTL container(s) should now be up-to-date :)${RESET}" |
| 54 | + return 1 |
| 55 | +fi |
| 56 | + |
| 57 | +if [[ $CURRENT_VERSION < $UPSTREAM_VERSION ]]; then |
| 58 | + # PULL UPDATED SCRIPTS |
| 59 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling updated scripts...${RESET}" |
| 60 | + SETUP_UPDATED=false |
| 61 | + if [[ $(curl -s ${RAW_CONTENT_URL}/setup.sh) != $(cat ./setup.sh) ]]; then |
| 62 | + SETUP_UPDATED=true |
| 63 | + fi |
| 64 | + # Currently unneeded \/ |
| 65 | + # UPDATER_UPDATED=false |
| 66 | + # if [[ $(curl -s ${RAW_CONTENT_URL}/bin/update-ttl.sh) != $(cat ./bin/update-ttl.sh) ]]; then |
| 67 | + # UPDATER_UPDATED=true |
| 68 | + # fi |
| 69 | + # BASH_CMD_UPDATED=false |
| 70 | + # if [[ $(curl -s ${RAW_CONTENT_URL}/bin/discord-ttl) != $(cat ./bin/discord-ttl) ]]; then |
| 71 | + # BASH_CMD_UPDATED=true |
| 72 | + # fi |
| 73 | + curl -s ${RAW_CONTENT_URL}/setup.sh > ./setup.sh |
| 74 | + curl -s ${RAW_CONTENT_URL}/bin/update-ttl.sh > ./bin/update-ttl.sh |
| 75 | + curl -s ${RAW_CONTENT_URL}/bin/discord-ttl > ./bin/discord-ttl |
| 76 | + # PULL UPDATED DOCKER COMPOSE |
| 77 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling updated docker compose...${RESET}" |
| 78 | + curl -s ${RAW_CONTENT_URL}/docker-compose.yaml > ./docker-compose.yaml |
| 79 | + # PULL UPDATED README & .ENV EXAMPLE (I mean, why not, right?) |
| 80 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling updated README & .env example...${RESET}" |
| 81 | + curl -s ${RAW_CONTENT_URL}/README.md > ./README.md |
| 82 | + curl -s ${RAW_CONTENT_URL}/.env.example > ./.env.example |
| 83 | + # PULL UPDATED PACKAGE.JSON (purely so the new version is reflected) |
| 84 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling updated package.json (to reflect the updated version)...${RESET}" |
| 85 | + curl -s ${RAW_CONTENT_URL}/package.json > ./package.json |
| 86 | + if [[ $SETUP_UPDATED == true ]]; then |
| 87 | + echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Running updated setup.sh...${RESET}" |
| 88 | + source ./setup.sh |
| 89 | + fi |
| 90 | +fi |
| 91 | + |
| 92 | +echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Pulling any new docker image(s)...${RESET}" |
| 93 | +docker-compose pull |
| 94 | +echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${BLUE_TEXT} Upping container(s)...${RESET}" |
| 95 | +docker-compose up -d |
| 96 | +echo "${RESET}${YELLOW_TEXT}[${BOLD}TTL Updater${RESET}${YELLOW_TEXT}]${RESET}${BOLD}${GREEN_TEXT} Done! Discord TTL should now be up-to-date :)${RESET}" |
| 97 | + |
| 98 | +# Return to whatever location this script was intitally run from for UX purposes |
| 99 | +cd $CURRENT_DIR |
0 commit comments