export DEBIAN_FRONTEND=noninteractive
apt -y update
apt install -y curl unzip jq python3 build-essential ca-certificates
mkdir -p /mnt/server
cd /mnt/server
export NODE_OPTIONS=--max-old-space-size=8192
chown -R root:root /mnt
mkdir -p .backup
cp -f .env /mnt/server/.backup
cp -f sogebot.db /mnt/server/.backup
rm -fR dest
rm -fR public
rm -fR node-modules
rm -fR tools
LATEST_JSON=$(curl --silent "https://api.github.com/repos/sogebot/sogeBot/releases/latest")
RELEASES=$(curl --silent "https://api.github.com/repos/sogebot/sogeBot/releases")
MATCH=sogeBot
if [ -z "${VERSION}" ] || [ "${VERSION}" == "latest" ]; then
DOWNLOAD_LINK=$(echo ${LATEST_JSON} | jq .assets | jq -r .[].browser_download_url | grep -i ${MATCH})
else
VERSION_CHECK=$(echo ${RELEASES} | jq -r --arg VERSION "${VERSION}" '.[] | select(.tag_name==$VERSION) | .tag_name')
if [ "${VERSION}" == "${VERSION_CHECK}" ]; then
DOWNLOAD_LINK=$(echo ${RELEASES} | jq -r --arg VERSION "${VERSION}" '.[] | select(.tag_name==$VERSION) | .assets[].browser_download_url' | grep -i ${MATCH})
else
echo -e "defaulting to latest release"
DOWNLOAD_LINK=$(echo ${LATEST_JSON} | jq .assets | jq -r .[].browser_download_url)
fi
fi
echo -e "Downloading from $DOWNLOAD_LINK"
curl -sSL -o sogeBot.zip ${DOWNLOAD_LINK}
unzip -o sogeBot.zip
rm -rf sogeBot.zip
echo "--------------------------------------------------------------------------"
echo "Installing dependencies, this will take some time so sit back and relax..."
echo "IGNORE WARNINGS. THESE ARE NOT ERRORS !!!"
echo "--------------------------------------------------------------------------"
npm install -g npm@8
npm install --verbose --force
echo -e "install completed"
exit 0