Skip to content

Instantly share code, notes, and snippets.

@kylefrost
Created July 27, 2017 14:11
Show Gist options
  • Save kylefrost/861c83138f473240c6956044bb16e4ef to your computer and use it in GitHub Desktop.
Save kylefrost/861c83138f473240c6956044bb16e4ef to your computer and use it in GitHub Desktop.
Updater script for filemanager
#!/bin/bash
function currentverinstalled {
if [ ! -f .fmver ]; then
LINE=0
else
LINE=$(head -n 1 .fmver)
fi
LINE="${LINE//.}"
echo "${LINE//v}"
}
function getcurrentghver {
echo $(curl -s https://api.github.com/repos/hacdias/filemanager/releases/latest | python -c "import sys, json; data = json.load(sys.stdin); print(data['tag_name'])")
}
function lowercase {
echo $1 | tr "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
}
function getostype {
echo "$(lowercase $(uname -s))"
}
function getoshardware {
case "$(uname -m)" in
i686)
echo "386"
;;
x86_64)
echo "amd64"
;;
*arm*)
echo "arm"
;;
*)
echo "Error: Unrecognized architecture."
exit 1
;;
esac
}
function determineifwindows {
case "$(uname -s)" in
*CYGWIN*)
echo ".exe"
;;
*)
echo ""
;;
esac
}
function formatassetname {
echo "$(getostype)-$(getoshardware)-filemanager$(determineifwindows)"
}
function checkupdate {
if [ "$1" = "check" ]; then
echo "Checking for update..."
fi
CURINVER=$(currentverinstalled)
CURGHVER=$(getcurrentghver)
CLEANGHVER="${CURGHVER//.}"
CLEANGHVER="${CLEANGHVER//v}"
if [ $CURINVER -lt $CLEANGHVER ]; then
UPDATE=0
else
UPDATE=1
fi
case "$1" in
check)
if [ $UPDATE -eq 0 ]; then
echo "There is an update to $CURGHVER available."
else
echo "There is no update available."
fi
;;
update)
echo $UPDATE
;;
*)
echo "Error: Could not determine update status."
;;
esac
}
function runupdate {
UPDATEAVAILABLE=$(checkupdate "update")
if [ $UPDATEAVAILABLE -eq 1 ]; then
echo "No updates are available."
exit 0
else
echo "Update found."
fi
echo "Installing update..."
ASSETNAME=$(formatassetname)
UPDATEURL=$(curl -s https://api.github.com/repos/hacdias/filemanager/releases/latest | python -c "import sys, json; data = json.load(sys.stdin); myasset = [asset for asset in data['assets'] if asset['name'] == '$ASSETNAME'][0]; print(myasset['browser_download_url'])")
wget -q $UPDATEURL -O "filemanager"
echo $(getcurrentghver) > .fmver
chmod +x "filemanager"
echo "Update installed."
exit 0
}
case "$1" in
check)
checkupdate "check"
;;
update)
runupdate
;;
*)
echo "Usage: install {check|update}"
echo ""
echo " check Checks if on latest version."
echo " update Updates to latest version if available."
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment