From 10157c35d0bd97b4e1106eb0b4dc388f29674863 Mon Sep 17 00:00:00 2001 From: lshprung Date: Fri, 20 Aug 2021 11:20:21 -0700 Subject: Changed to single script approach --- compare_versions.sh | 27 --- fetch_info.sh | 105 ----------- update_ungoogled_chromium-portable_linux_64.sh | 190 -------------------- update_ungoogled_chromium.sh | 230 +++++++++++++++++++++++++ 4 files changed, 230 insertions(+), 322 deletions(-) delete mode 100755 compare_versions.sh delete mode 100755 fetch_info.sh delete mode 100755 update_ungoogled_chromium-portable_linux_64.sh create mode 100755 update_ungoogled_chromium.sh diff --git a/compare_versions.sh b/compare_versions.sh deleted file mode 100755 index 757100d..0000000 --- a/compare_versions.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/bash - -# Bash script to compare ungoogled-chromium version numbers -# $1 -> up-to-date version number (from atom feed) -# $2 -> currently installed version number -# Return Values: - # 0 -> installed version is up-to-date - # 1 -> installed version can be upgraded - - -# Break into arrays -IFS='.' -read -ra V1 <<< "$1" -read -ra V2 <<< "$2" - -# Determine shorter array (for the loop) -LENGTH=${#V1[@]} - -for (( i = 0 ; i < LENGTH ; i++ )); do - if [ "${V1[$i]}" -gt "${V2[$i]}" ]; then - exit 1 - elif [ "${V1[$i]}" -lt "${V2[$i]}" ]; then - exit 0 - fi -done - -exit 0 diff --git a/fetch_info.sh b/fetch_info.sh deleted file mode 100755 index 601f394..0000000 --- a/fetch_info.sh +++ /dev/null @@ -1,105 +0,0 @@ -#!/bin/bash - -# Bash script to query for information about latest version of ungoogled-chromium (for specific version) -# $1 -> platform name -# Return Values: - # 0 -> error - # 1 -> success - -# URL for atom feed -ATOM_URL="https://raw.githubusercontent.com/ungoogled-software/ungoogled-chromium-binaries/master/feed.xml" - -PARSED_XML=$(curl -s $ATOM_URL | xml2) - -# Get table of available platforms, with versions, and URLs on the following line -PLATFORM_TABLE=$(echo "$PARSED_XML" | grep -E '(/feed/entry/title=)|(/feed/entry/link/@href=)' | sed 's/^.*=//g') - -# echo "$PLATFORM_TABLE" - - -# Set DEFAULT_PLATFORM -if [ -n "$1" ]; then - PLATFORM="$1" -else - echo "Error: $0 missing argument" - exit 0 -fi - -# grep for PLATFORM in PLATFORM_TABLE, pull out two lines, starting from matching LINE_NUMBER -LINE_NUMBER=$(echo "$PLATFORM_TABLE" | grep -m 1 -n "$PLATFORM" | cut -d ':' -f 1) -echo "$PLATFORM_TABLE" | sed -n "$LINE_NUMBER,$((LINE_NUMBER+1))p" - -exit 1 - -# """ -# --- -# -# # Function to print a message that the currently installed ungoogled-chromium is up to date -# nothing_to_do() { -# echo "$UG_PATH is up to date" -# } -# -# # Function to check if $1 is higher version than $2 -# # $1 -> remote version (example: 91.0.4472.164) -# # $2 -> local version (example: 91.0.4472.114) -# # Return value: -# # 0 -> $1 <= $2 (no need to update) -# # 1 -> $1 > $2 (update available) -# compare_version() { -# echo "$1" -# echo "$2" -# -# # Break into arrays -# local IFS='.' -# read -ra V1 <<< "$1" -# read -ra V2 <<< "$2" -# -# for val in "${V1[@]}"; do -# echo "$val" -# done -# echo ${#V1[@]} -# for val in "${V2[@]}"; do -# echo "$val" -# done -# echo ${#V2[@]} -# -# # Determine shorter array (for the loop) -# local LENGTH=${#V1[@]} -# -# for (( i = 0 ; i < LENGTH ; i++ )); do -# if [ "${V1[$i]}" -gt "${V2[$i]}" ]; then -# return 1 -# elif [ "${V1[$i]}" -lt "${V2[$i]}" ]; then -# return 0 -# fi -# done -# -# return 0 -# } -# -# -# # DEBUG -# #compare_version "91.0.4472.164" "91.0.4472.114" -# -# # Determine local version if ungoogled-chromium is installed -# if [ -x "$UG_PATH" ]; then -# VERSION=$($UG_PATH --version | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") -# echo "$VERSION" -# fi -# -# # If ungoogled-chromium is installed on the machine, check if there is a newer version -# LATEST=$(echo "$PLATFORM_TABLE" | grep "$DEFAULT_PLATFORM") -# # TODO handle ambiguous DEFAULT_PLATFORM -# # TODO handle if unknown DEFAULT_PLATFORM -# -# if [ -n "$VERSION" ]; then -# -# compare_version "$(echo "$LATEST" | head -n 1 | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+")" "$VERSION" -# if [ $? -eq 0 ]; then -# nothing_to_do -# exit -# else -# echo "Gotta update!" -# fi -# fi -# """ diff --git a/update_ungoogled_chromium-portable_linux_64.sh b/update_ungoogled_chromium-portable_linux_64.sh deleted file mode 100755 index 6b118e8..0000000 --- a/update_ungoogled_chromium-portable_linux_64.sh +++ /dev/null @@ -1,190 +0,0 @@ -#!/bin/bash - -# Shell script to update ungoogled-chromium Portable Linux 64-bit to the latest version -# $1 -> Wrapper symlink location -# $2 (optional) -> Install location -# Return Values: - # 0 -> failed - # 1 -> success, installed new version - # 2 -> success, did not install new version - -# If first time installing: user must specify a symlink location and an installation directory -# If checking for updates: user must pass a symlink for ungoogled-chromium (this is typically just the command name if a symlink is part of the path) - # Use the symlink target to determine installation directory - -PLATFORM="Portable Linux 64-bit" - - -# Print help message -print_help() { - echo "Usage: $0 LINK [LOCATION]" - echo - echo "LINK is the path for a symlink pointing to the ungoogled-chromium executable" - echo "LOCATION is the desired install location for ungoogled-chromium. It does not need to be specified unless installing ungoogled-chromium for the first time" -} - -# Function to determine absolute path of helper scripts -# $1 -> script name -get_absolute_path() { - echo "$(dirname "$(which "$1")")/$1" -} - -# Function to determine path to install to -# $1 -> LOCATION or LINK target -# Return Values: - # 0 -> failed, should print_help and exit after returning - # 1 -> success -get_install_path() { - if [ -n "$1" ]; then - if [ ! -e "$1" ]; then - echo "Error: $1 does not exist" - return 0 - fi - if [ ! -w "$1" ]; then - echo "Error: cannot write to $1; insufficient permissions" - return 0 - fi - - echo "$1" - return 1 - fi - - return 0 -} - - -# Exit if $1 does not exist -if [ -z "$1" ]; then - echo "Error: too few arguments" - print_help - exit 0 -fi - -# Fetch info, break and store into variables -FULL_INFO=$($(get_absolute_path "fetch_info.sh") "$PLATFORM") - -if [ $? -eq 0 ]; then - echo "Error: Could not get latest version info" - exit 0 -fi - -NAME=$(echo "$FULL_INFO" | head -n 1 | cut -d ':' -f 1) -VERSION=$(echo "$FULL_INFO" | head -n 1 | cut -d ':' -f 2 | sed 's/^[ ]*//g' | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") -URL=$(echo "$FULL_INFO" | sed -n "2p") - -# Check installed version number (if exists and link is valid) -if [ -h "$1" ] && [ "$(readlink -f "$1" | grep -E -o "[/][^/]*$")" == "/chrome-wrapper" ]; then - - # If the symlink exists, ensure I can write to it - if [ ! -w "$1" ]; then - echo "Error: Cannot write to $1" - print_help - exit 0 - fi - - MY_VERSION=$($1 --version | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") - - # Compare versions to determine if an update is necessary - $(get_absolute_path "compare_versions.sh") "$VERSION" "$MY_VERSION" - if [ $? -eq 0 ]; then - echo "ungoogled-chromium $MY_VERSION is up to date" - exit 2 - fi - - echo -n "Upgrade ungoogled-chromium to $VERSION? [Y/n] " - read -r INPUT - if [ "$INPUT" == "n" ]; then - exit 2 - fi - - # Get old install location for deletion later - OLD_INSTALL=$(get_install_path "$(readlink -f "$1" | sed 's/[/][^/]*$//g')") - - # Get install path - INSTALL_TO=$(get_install_path "$(echo "$2" | sed 's/[/]*$//g')") - if [ $? -eq 0 ]; then - INSTALL_TO=$(get_install_path "$(readlink -f "$1" | sed 's/[/][^/]*[/][^/]*$//g')") - if [ $? -eq 0 ]; then - print_help - exit 0 - fi - fi - -else - # First time installation - if [ -z "$2" ]; then - echo "Error: Missing LOCATION argument" - print_help - exit 0 - fi - - # If a file exists already (that is not a symlink to ungoogled-chromium), then exit - if [ -e "$1" ]; then - echo "Error: $1 already exists" - print_help - exit 0 - fi - - # Check that a file can be created in this directory - touch "$1" - if [ ! $? -eq 0 ] || [ ! -w "$1" ]; then - echo "Error: Cannot write to $1" - print_help - exit 0 - else - rm "$1" - fi - - # Check install path argument - INSTALL_TO=$(get_install_path "$(echo "$2" | sed 's/[/]*$//g')") - if [ $? -eq 0 ]; then - print_help - exit 0 - fi - - echo -n "Install ungoogled-chromium $VERSION to $INSTALL_TO/? [Y/n] " - read -r INPUT - if [ "$INPUT" == "n" ]; then - exit 2 - fi -fi - -# Get the realpath of INSTALL_TO -INSTALL_TO=$(realpath "$INSTALL_TO") - -# Download tar file to /tmp -echo "Downloading ungoogled-chromium $VERSION" -DOWNLOAD_URL=$(curl -s "$URL" | grep -E -o "href=\".*tar\.xz\"" | cut -d '"' -f 2) -TAR_FILE="ungoogled-chromium_${VERSION}_linux.tar.xz" -wget --quiet -O "/tmp/$TAR_FILE" "$DOWNLOAD_URL" -if [ ! -r "/tmp/$TAR_FILE" ]; then - echo "Error: Issue downloading ungoogled-chromium $VERSION from $DOWNLOAD_URL" - exit 0 -fi - -# Check hash -echo "Checking MD5 hash" -HASH=$(curl -s "$URL" | grep "MD5" | sed 's/<[^<>]*>//g;s/[ ]//g' | cut -d ':' -f 2) -if [ "$HASH" != "$(md5sum "/tmp/$TAR_FILE" | cut -d ' ' -f 1)" ]; then - echo "Error: MD5 checksum failed" - rm "/tmp/$TAR_FILE" - exit 0 -fi - -# Extract to INSTALL_TO and get parent directory name from archive -echo "Extracting to $INSTALL_TO/" -tar -xf "/tmp/$TAR_FILE" --directory "$INSTALL_TO/" -PARENT_DIR=$(tar -tvf "/tmp/$TAR_FILE" | head -n 1 | cut -d ' ' -f 6 | cut -d '/' -f 1) - -# Create symlink -echo "Creating symlink $1" -ln -fns "$INSTALL_TO/$PARENT_DIR/chrome-wrapper" "$1" - -# Cleanup -echo "Removing /tmp/$TAR_FILE" -rm "/tmp/$TAR_FILE" -echo "$OLD_INSTALL" | grep -q "ungoogled-chromium" -if [ "$?" -eq 0 ] && [ -w "$OLD_INSTALL" ]; then - echo "Removing $OLD_INSTALL" - rm -r "$OLD_INSTALL" -fi diff --git a/update_ungoogled_chromium.sh b/update_ungoogled_chromium.sh new file mode 100755 index 0000000..c433182 --- /dev/null +++ b/update_ungoogled_chromium.sh @@ -0,0 +1,230 @@ +#!/bin/bash + +# Shell script to update ungoogled-chromium Portable Linux 64-bit to the latest version +# $1 -> Wrapper symlink location +# $2 (optional) -> Install location +# Return Values: + # 0 -> failed + # 1 -> success, installed new version + # 2 -> success, did not install new version + +# If first time installing: user must specify a symlink location and an installation directory +# If checking for updates: user must pass a symlink for ungoogled-chromium (this is typically just the command name if a symlink is part of the path) + # Use the symlink target to determine installation directory + +# URL for atom feed +ATOM_URL="https://raw.githubusercontent.com/ungoogled-software/ungoogled-chromium-binaries/master/feed.xml" + +# Default platform to install/update +PLATFORM="Portable Linux 64-bit" + + +# Print help message +print_help() { + echo "Usage: $0 LINK [LOCATION]" + echo + echo "LINK is the path for a symlink pointing to the ungoogled-chromium executable" + echo "LOCATION is the desired install location for ungoogled-chromium. It does not need to be specified unless installing ungoogled-chromium for the first time" +} + +# Function to compare ungoogled-chromium version numbers +# $1 -> up-to-date version number (from atom feed) +# $2 -> currently installed version number +# Return Values: + # 0 -> installed version is up-to-date + # 1 -> installed version can be upgraded +compare_versions() { + # Break into arrays + local IFS='.' + read -ra V1 <<< "$1" + read -ra V2 <<< "$2" + + # Determine shorter array (for the loop) + local LENGTH=${#V1[@]} + + for (( i = 0 ; i < LENGTH ; i++ )); do + if [ "${V1[$i]}" -gt "${V2[$i]}" ]; then + return 1 + elif [ "${V1[$i]}" -lt "${V2[$i]}" ]; then + return 0 + fi + done + + return 0 +} + +# Function to query for information about latest version of ungoogled-chromium (for specific version) +# $1 -> platform name +# Return Values: + # 0 -> error + # 1 -> success +fetch_info() { + # Get table of available platforms, with versions, and URLs on the following line + local PLATFORM_TABLE=$(echo "$PARSED_XML" | grep -E '(/feed/entry/title=)|(/feed/entry/link/@href=)' | sed 's/^.*=//g') + + # grep for PLATFORM in PLATFORM_TABLE, pull out two lines, starting from matching LINE_NUMBER + local LINE_NUMBER=$(echo "$PLATFORM_TABLE" | grep -m 1 -n "$PLATFORM" | cut -d ':' -f 1) + echo "$PLATFORM_TABLE" | sed -n "$LINE_NUMBER,$((LINE_NUMBER+1))p" +} + +# Function to determine path to install to (and to perform sanity checks on that path) +# $1 -> LOCATION or LINK target +# Return Values: + # 0 -> failed, should print_help and exit after returning + # 1 -> success +get_install_path() { + if [ -n "$1" ]; then + if [ ! -e "$1" ]; then + echo "Error: $1 does not exist" + return 0 + fi + if [ ! -w "$1" ]; then + echo "Error: cannot write to $1; insufficient permissions" + return 0 + fi + + echo "$1" + return 1 + fi + + return 0 +} + + +# Exit if $1 does not exist +if [ -z "$1" ]; then + echo "Error: too few arguments" + print_help + exit 0 +fi + +# Fetch info, break and store into variables +PARSED_XML=$(curl -s $ATOM_URL | xml2) + +if [ ! $? -eq 0 ]; then + echo "Error: Could not parse atom URL $ATOM_URL" + exit 0 +fi + +FULL_INFO=$(fetch_info "$PLATFORM") + +NAME=$(echo "$FULL_INFO" | head -n 1 | cut -d ':' -f 1) +VERSION=$(echo "$FULL_INFO" | head -n 1 | cut -d ':' -f 2 | sed 's/^[ ]*//g' | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") +URL=$(echo "$FULL_INFO" | sed -n "2p") + +# Check installed version number (if exists and link is valid) +if [ -h "$1" ] && [ "$(readlink -f "$1" | grep -E -o "[/][^/]*$")" == "/chrome-wrapper" ]; then + + # If the symlink exists, ensure I can write to it + if [ ! -w "$1" ]; then + echo "Error: Cannot write to $1" + print_help + exit 0 + fi + + MY_VERSION=$($1 --version | grep -E -o "[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") + + # Compare versions to determine if an update is necessary + compare_versions "$VERSION" "$MY_VERSION" + if [ $? -eq 0 ]; then + echo "ungoogled-chromium $MY_VERSION is up to date" + exit 2 + fi + + echo -n "Upgrade ungoogled-chromium to $VERSION? [Y/n] " + read -r INPUT + if [ "$INPUT" == "n" ]; then + exit 2 + fi + + # Get old install location for deletion later + OLD_INSTALL=$(get_install_path "$(readlink -f "$1" | sed 's/[/][^/]*$//g')") + + # Get install path + INSTALL_TO=$(get_install_path "$(echo "$2" | sed 's/[/]*$//g')") + if [ $? -eq 0 ]; then + INSTALL_TO=$(get_install_path "$(readlink -f "$1" | sed 's/[/][^/]*[/][^/]*$//g')") + if [ $? -eq 0 ]; then + print_help + exit 0 + fi + fi + +else + # First time installation + if [ -z "$2" ]; then + echo "Error: Missing LOCATION argument" + print_help + exit 0 + fi + + # If a file exists already (that is not a symlink to ungoogled-chromium), then exit + if [ -e "$1" ]; then + echo "Error: $1 already exists" + print_help + exit 0 + fi + + # Check that a file can be created in this directory + touch "$1" + if [ ! $? -eq 0 ] || [ ! -w "$1" ]; then + echo "Error: Cannot write to $1" + print_help + exit 0 + else + rm "$1" + fi + + # Check install path argument + INSTALL_TO=$(get_install_path "$(echo "$2" | sed 's/[/]*$//g')") + if [ $? -eq 0 ]; then + print_help + exit 0 + fi + + echo -n "Install ungoogled-chromium $VERSION to $INSTALL_TO/? [Y/n] " + read -r INPUT + if [ "$INPUT" == "n" ]; then + exit 2 + fi +fi + +# Get the realpath of INSTALL_TO +INSTALL_TO=$(realpath "$INSTALL_TO") + +# Download tar file to /tmp +echo "Downloading ungoogled-chromium $VERSION" +DOWNLOAD_URL=$(curl -s "$URL" | grep -E -o "href=\".*tar\.xz\"" | cut -d '"' -f 2) +TAR_FILE="ungoogled-chromium_${VERSION}_linux.tar.xz" +wget --quiet -O "/tmp/$TAR_FILE" "$DOWNLOAD_URL" +if [ ! -r "/tmp/$TAR_FILE" ]; then + echo "Error: Issue downloading ungoogled-chromium $VERSION from $DOWNLOAD_URL" + exit 0 +fi + +# Check hash +echo "Checking MD5 hash" +HASH=$(curl -s "$URL" | grep "MD5" | sed 's/<[^<>]*>//g;s/[ ]//g' | cut -d ':' -f 2) +if [ "$HASH" != "$(md5sum "/tmp/$TAR_FILE" | cut -d ' ' -f 1)" ]; then + echo "Error: MD5 checksum failed" + rm "/tmp/$TAR_FILE" + exit 0 +fi + +# Extract to INSTALL_TO and get parent directory name from archive +echo "Extracting to $INSTALL_TO/" +tar -xf "/tmp/$TAR_FILE" --directory "$INSTALL_TO/" +PARENT_DIR=$(tar -tvf "/tmp/$TAR_FILE" | head -n 1 | cut -d ' ' -f 6 | cut -d '/' -f 1) + +# Create symlink +echo "Creating symlink $1" +ln -fns "$INSTALL_TO/$PARENT_DIR/chrome-wrapper" "$1" + +# Cleanup +echo "Removing /tmp/$TAR_FILE" +rm "/tmp/$TAR_FILE" +echo "$OLD_INSTALL" | grep -q "ungoogled-chromium" +if [ "$?" -eq 0 ] && [ -w "$OLD_INSTALL" ]; then + echo "Removing $OLD_INSTALL" + rm -r "$OLD_INSTALL" +fi -- cgit