Add update feature to script

This commit is contained in:
causefx 2023-10-13 08:35:32 -07:00 committed by GitHub
parent 47bd6e7c56
commit db1edd13a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1435,10 +1435,46 @@ DoUpdateTimestamp() {
TimeStamp="$(date "+%Y-%m-%d_%H.%M.%S")" TimeStamp="$(date "+%Y-%m-%d_%H.%M.%S")"
} }
# Get latest version from Github
GetLatestRelease() {
response=$(curl -s "https://api.github.com/repos/ChuckPa/PlexDBRepair/tags")
if [ $? -eq 0 ]; then
LatestVersion=$(echo "$response" | grep -oP '"name":\s*"\K[^"]*' | sed -n '1p')
else
LatestVersion=$Version
fi
}
# Download and update script
DownloadAndUpdate() {
url="$1"
filename="$2"
# Download the file and check if the download was successful
if curl -s "$url" --output "$filename"; then
Output "Update downloaded successfully"
# Check if the file was written to
if [ -f "$filename" ]; then
Output "Update written successfully"
else
Output "Error: File not written"
fi
else
Output "Error: Download failed"
fi
}
############################################################# #############################################################
# Main utility begins here # # Main utility begins here #
############################################################# #############################################################
# Set Script Path
ScriptPath="$( readlink -f "$0")"
ScriptName="$(basename "$ScriptPath")"
ScriptWorkingDirectory="$(dirname "$ScriptPath")"
# Initialize LastName LastTimestamp # Initialize LastName LastTimestamp
SetLast "" "" SetLast "" ""
@ -1569,6 +1605,7 @@ do
echo " 11 - 'status' - Report status of PMS (run-state and databases)" echo " 11 - 'status' - Report status of PMS (run-state and databases)"
echo " 12 - 'undo' - Undo last successful command" echo " 12 - 'undo' - Undo last successful command"
echo "" echo ""
echo " 88 - 'update' - Check for script update"
echo " 99 - 'quit' - Quit immediately. Keep all temporary files." echo " 99 - 'quit' - Quit immediately. Keep all temporary files."
echo " 'exit' - Exit with cleanup options." echo " 'exit' - Exit with cleanup options."
fi fi
@ -1870,6 +1907,34 @@ do
DoUndo DoUndo
;; ;;
88|upda*)
DoUpdate=0
Output "Checking for script update"
GetLatestRelease
if [ $LatestVersion != $Version ] && ([ $Scripted -eq 1 ] || ConfirmYesNo "Download and update script?"); then
DoUpdate=1
Output "Version update available"
else
Output "No update available or user declined update"
fi
# Check if script path is writable
if [ $DoUpdate -eq 1 ]; then
if [ -w "$ScriptWorkingDirectory" ]; then
Output "Performing update"
DownloadAndUpdate "https://raw.githubusercontent.com/ChuckPa/PlexDBRepair/master/DBRepair.sh" "$ScriptWorkingDirectory/$ScriptName"
exit 0
else
Output "Script path is not writable."
fi
fi
if [ $Scripted -eq 1 ]; then
exit 0
fi
;;
# Quit # Quit
99|quit) 99|quit)