Skip to content

Instantly share code, notes, and snippets.

@ffflorian
Created August 5, 2021 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ffflorian/927b1414a6bb21370cc37dfd61b7158b to your computer and use it in GitHub Desktop.
Save ffflorian/927b1414a6bb21370cc37dfd61b7158b to your computer and use it in GitHub Desktop.
nextcloud-update.sh
#!/usr/bin/env bash
# Update (or install) the latest NextCloud AppImage
# Optimized for Debian/Ubuntu + GNOME
# ffflorian 2021
set -e
SCRIPT_NAME="${0##*/}"
INSTALL_DIR="/opt/nextcloud"
APPIMAGE_FILE="nextcloud.AppImage"
EXEC_FILE="${INSTALL_DIR}/${APPIMAGE_FILE}"
TEMP_DIR="$(mktemp -d)"
TEMP_APPIMAGE="${TEMP_DIR}/${APPIMAGE_FILE}"
TEMP_SIGNATURE="${TEMP_SIGNATURE}.asc"
ICON_FILE="${INSTALL_DIR}/nextcloud.png"
DESKTOP_FILE="/usr/share/applications/nextcloud.desktop"
RELEASE_URL="https://api.github.com/repos/nextcloud/desktop/releases/latest"
USE_CURL="yes"
FORCE="no"
_cleanup() {
rm -rf "${TEMP_DIR}"
}
trap _cleanup EXIT
_compare_version() {
printf "%03d%03d%03d%03d" $(echo "${1}" | tr '.' ' ')
}
get_latest_release() {
if [ "${USE_CURL}" = "yes" ]; then
curl -sL "${RELEASE_URL}" | grep '"tag_name":' | sed -E 's/.*"v?([^"]+)".*/\1/'
else
wget -qO- "${RELEASE_URL}" | grep '"tag_name":' | sed -E 's/.*"v?([^"]+)".*/\1/'
fi
}
_print_usage() {
cat <<EOF
Usage: ${SCRIPT_NAME} [option]
Options:
--force (-f) Reinstall NextCloud if it is already installed.
Commands:
--help (-h) Display this help message
EOF
}
while :
do
case "${1}" in
-f|--force )
FORCE="force"
shift
;;
-h|--help )
_print_usage
exit 0
;;
* )
break
;;
esac
done
if [ -d "${INSTALL_DIR}" ]; then
if [ -r "${EXEC_FILE}" ]; then
CURRENT_VERSION="$(${EXEC_FILE} --version | sed -n 's/Nextcloud version \(.*\)git.*/\1/p')"
else
read -r -p "The current NextCloud installation seems to be broken. Would you like to reinstall the latest version? [y/N] " RESPONSE
case "${RESPONSE}" in
[nN][oO]|[nN]|"" ) exit 0 ;;
esac
FORCE="force"
fi
else
read -r -p "NextCloud is not installed yet. Would you like to install it? [y/N] " RESPONSE
case "${RESPONSE}" in
[nN][oO]|[nN]|"" ) exit 0 ;;
esac
FORCE="force"
fi
if ! command -v "curl" > /dev/null; then
USE_CURL="no"
fi
LATEST_VERSION="$(get_latest_release)"
if [ "${FORCE}" != "force" ]; then
if [ "$(_compare_version "${LATEST_VERSION}")" -le "$(_compare_version "${CURRENT_VERSION}")" ]; then
echo "No update needed, ${CURRENT_VERSION} is the latest NextCloud version available."
echo "Run this script with --force to reinstall this NextCloud version."
exit 0
else
echo "A new version ${LATEST_VERSION} is available."
fi
fi
DOWNLOAD_URL="https://github.com/nextcloud/desktop/releases/download/v${LATEST_VERSION}/Nextcloud-${LATEST_VERSION}-x86_64.AppImage"
SIGNATURE_URL="${DOWNLOAD_URL}.asc"
echo "Downloading NextCloud ${LATEST_VERSION} from ${DOWNLOAD_URL} ... "
if [ "${USE_CURL}" = "yes" ]; then
curl -o "${TEMP_APPIMAGE}" -L "${DOWNLOAD_URL}"
else
wget -O "${TEMP_APPIMAGE}" "${DOWNLOAD_URL}"
fi
echo "Downloading PGP signature from ${SIGNATURE_URL} ... "
if [ "${USE_CURL}" = "yes" ]; then
curl -o "${TEMP_SIGNATURE}" -L "${SIGNATURE_URL}"
else
wget -O "${TEMP_SIGNATURE}" "${SIGNATURE_URL}"
fi
echo "Verifying AppImage with GPG ..."
gpg --verify "${TEMP_SIGNATURE}" "${TEMP_APPIMAGE}"
printf "OK\\n\\n"
if [ ! -r "${TEMP_APPIMAGE}" ]; then
echo "Error: could not find downloaded file \"${TEMP_APPIMAGE}\"".
exit 1
fi
echo "Copying AppImage ... "
sudo mv "${TEMP_APPIMAGE}" "${INSTALL_DIR}/${APPIMAGE_FILE}"
sudo chmod +x "${INSTALL_DIR}/${APPIMAGE_FILE}"
sudo chown root:root "${INSTALL_DIR}/${APPIMAGE_FILE}"
printf "OK\\n\\n"
printf "Creating soft link at /usr/bin ... "
sudo ln -fs "${EXEC_FILE}" "/usr/bin/"
printf "OK\\n\\n"
printf "Setting start menu entry ... "
sudo tee "${DESKTOP_FILE}" > /dev/null <<EOF
[Desktop Entry]
Categories=Utility;X-SuSE-SyncUtility;
Type=Application
Exec=${EXEC_FILE}
Name=Nextcloud desktop sync client
Comment=Nextcloud desktop synchronization client
GenericName=Folder Sync
Icon=${ICON_FILE}
Keywords=Nextcloud;syncing;file;sharing;
X-GNOME-Autostart-Delay=3
EOF
echo "All done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment