Small refactoring and tidyups (#279)

This commit is contained in:
Andrew Scheller
2019-06-11 23:29:11 +01:00
committed by popcornmix
parent bd9b831ffd
commit 4246effd99

View File

@@ -4,17 +4,21 @@ set -o nounset
set -o errexit set -o errexit
REPO_URI=${REPO_URI:-"https://github.com/Hexxeh/rpi-firmware"} REPO_URI=${REPO_URI:-"https://github.com/Hexxeh/rpi-firmware"}
REPO_API_URI=${REPO_URI/github.com/api.github.com\/repos}
REPO_CONTENT_URI=${REPO_URI/github.com/raw.githubusercontent.com}
UPDATE_SELF=${UPDATE_SELF:-1} UPDATE_SELF=${UPDATE_SELF:-1}
UPDATE_URI="https://raw.githubusercontent.com/Hexxeh/rpi-update/master/rpi-update" UPDATE_REPO_URI="https://github.com/Hexxeh/rpi-update"
UPDATE_REPO_CONTENT_URI=${UPDATE_REPO_URI/github.com/raw.githubusercontent.com}
UPDATE_URI="${UPDATE_REPO_CONTENT_URI}/master/rpi-update"
if [[ ${BOOT_PATH:-"unset"} == "unset" && ${ROOT_PATH:-"unset"} != "unset" ]] || if [[ "${BOOT_PATH:-"unset"}" == "unset" && "${ROOT_PATH:-"unset"}" != "unset" ]] ||
[[ ${BOOT_PATH:-"unset"} != "unset" && ${ROOT_PATH:-"unset"} == "unset" ]]; then [[ "${BOOT_PATH:-"unset"}" != "unset" && "${ROOT_PATH:-"unset"}" == "unset" ]]; then
echo " *** You need to specify both ROOT_PATH and BOOT_PATH, or neither" echo " *** You need to specify both ROOT_PATH and BOOT_PATH, or neither"
exit 1 exit 1
fi fi
if [[ ${BOOT_PATH:-"unset"} == "unset" ]]; then if [[ "${BOOT_PATH:-"unset"}" == "unset" ]]; then
NOOBS_CHECK=${NOOBS_CHECK:-1} NOOBS_CHECK=${NOOBS_CHECK:-1}
else else
NOOBS_CHECK=${NOOBS_CHECK:-0} NOOBS_CHECK=${NOOBS_CHECK:-0}
@@ -35,50 +39,56 @@ PRUNE_MODULES=${PRUNE_MODULES:-0}
RPI_UPDATE_UNSUPPORTED=${RPI_UPDATE_UNSUPPORTED:-0} RPI_UPDATE_UNSUPPORTED=${RPI_UPDATE_UNSUPPORTED:-0}
JUST_CHECK=${JUST_CHECK:-0} JUST_CHECK=${JUST_CHECK:-0}
RPI_REBOOT=${RPI_REBOOT:-0} RPI_REBOOT=${RPI_REBOOT:-0}
CURL_OPTIONS=${CURL_OPTIONS:-""}
GITHUB_API_TOKEN=${GITHUB_API_TOKEN:-""} GITHUB_API_TOKEN=${GITHUB_API_TOKEN:-""}
FW_REPO="${REPO_URI}.git" FW_REPO="${REPO_URI}.git"
FW_REPOLOCAL=${FW_REPOLOCAL:-"${WORK_PATH}/.rpi-firmware"} FW_REPOLOCAL=${FW_REPOLOCAL:-"${WORK_PATH}/.rpi-firmware"}
FW_PATH="${BOOT_PATH}" FW_PATH="${BOOT_PATH}"
FW_MODPATH="${ROOT_PATH}/lib/modules" FW_MODPATH="${ROOT_PATH}/lib/modules"
FW_REV_IN=${1:-""} FW_REV_IN=${1:-""}
FW_REVFILE="${FW_PATH}/.firmware_revision" FW_REVFILE="${FW_PATH}/.firmware_revision"
SELFUPDATE_SCRIPT="${WORK_PATH}/.updateScript.sh"
[ "${RPI_UPDATE_UNSUPPORTED}" -ne 0 ] && echo -e "You appear to be trying to update firmware on an incompatible distribution. To force update, run the following:\nsudo -E RPI_UPDATE_UNSUPPORTED=0 rpi-update" && exit 1 [ "${RPI_UPDATE_UNSUPPORTED}" -ne 0 ] && echo -e "You appear to be trying to update firmware on an incompatible distribution. To force update, run the following:\nsudo -E RPI_UPDATE_UNSUPPORTED=0 rpi-update" && exit 1
if command -v vcgencmd > /dev/null; then if command -v vcgencmd > /dev/null; then
vcgencmd get_config str | grep -qE "^kernel=" && echo -e "You appear to be using a custom kernel file.\nSkipping installation of new kernel, as bundled dtb files may be incompatible with your kernel." && SKIP_KERNEL=1 vcgencmd get_config str | grep -qE "^kernel=" && echo -e "You appear to be using a custom kernel file.\nSkipping installation of new kernel, as bundled dtb files may be incompatible with your kernel." && SKIP_KERNEL=1
fi fi
# Always follow redirects
CURL_OPTIONS="${CURL_OPTIONS} -L"
# Support for custom GitHub Auth Tokens # Support for custom GitHub Auth Tokens
GITHUB_AUTH_PARAM=""
if [[ -n "${GITHUB_API_TOKEN}" ]]; then if [[ -n "${GITHUB_API_TOKEN}" ]]; then
echo " *** Using GitHub token for all requests." echo " *** Using GitHub token for all requests."
GITHUB_AUTH_PARAM="--header \"Authorization: token ${GITHUB_API_TOKEN}\"" CURL_OPTIONS="${CURL_OPTIONS} --header \"Authorization: token ${GITHUB_API_TOKEN}\""
fi fi
GITHUB_API_LIMITED=$(eval curl -Ls ${GITHUB_AUTH_PARAM} https://api.github.com/rate_limit | tr -d "," | awk 'BEGIN {reset=0;} { if ($1 == "\"limit\":") limit=$2; else if ($1 == "\"remaining\":") remaining=$2; else if ($1 == "\"reset\":" && limit>0 && remaining==0) reset=$2;} END { print reset }') GITHUB_API_LIMITED=$(eval curl -s ${CURL_OPTIONS} "https://api.github.com/rate_limit" | tr -d "," | awk 'BEGIN {reset=0;} { if ($1 == "\"limit\":") limit=$2; else if ($1 == "\"remaining\":") remaining=$2; else if ($1 == "\"reset\":" && limit>0 && remaining==0) reset=$2;} END { print reset }')
if [ ${GITHUB_API_LIMITED} -gt 0 ]; then if [ ${GITHUB_API_LIMITED} -gt 0 ]; then
echo " *** Github api is currently rate limited - please try again after $(date --date @${GITHUB_API_LIMITED})" echo " *** Github API is currently rate limited - please try again after $(date --date @${GITHUB_API_LIMITED})"
exit 1 exit 1
fi fi
function update_self() { function update_self() {
echo " *** Performing self-update" echo " *** Performing self-update"
_tempFileName="$0.tmp" local _tempFileName="$0.tmp"
if ! eval curl -Lfs ${GITHUB_AUTH_PARAM} --output "${_tempFileName}" "${UPDATE_URI}"; then if ! eval curl -fs ${CURL_OPTIONS} --output "${_tempFileName}" "${UPDATE_URI}"; then
echo " !!! Failed to download update for rpi-update!" echo " !!! Failed to download update for rpi-update!"
echo " !!! Make sure you have ca-certificates installed and that the time is set correctly" echo " !!! Make sure you have ca-certificates installed and that the time is set correctly"
exit 1 exit 1
fi fi
OCTAL_MODE=$(stat -c '%a' "$0") local OCTAL_MODE=$(stat -c '%a' "$0")
if ! chmod ${OCTAL_MODE} "${_tempFileName}" ; then if ! chmod ${OCTAL_MODE} "${_tempFileName}" ; then
echo " !!! Failed: Error while trying to set mode on ${_tempFileName}" echo " !!! Failed: Error while trying to set mode on ${_tempFileName}"
exit 1 exit 1
fi fi
cat > "${WORK_PATH}/.updateScript.sh" << EOF cat > "${SELFUPDATE_SCRIPT}" << EOF
if mv "${_tempFileName}" "$0"; then if mv "${_tempFileName}" "$0"; then
rm -- "\$0" rm -- "\$0"
exec env UPDATE_SELF=0 /bin/bash "$0" "${FW_REV_IN}" exec env UPDATE_SELF=0 /bin/bash "$0" "${FW_REV_IN}"
@@ -88,7 +98,7 @@ function update_self() {
EOF EOF
echo " *** Relaunching after update" echo " *** Relaunching after update"
exec /bin/bash "${WORK_PATH}/.updateScript.sh" exec /bin/bash "${SELFUPDATE_SCRIPT}"
} }
function update_modules { function update_modules {
@@ -101,10 +111,10 @@ function update_modules {
if [[ ${PRUNE_MODULES} -ne 0 ]]; then if [[ ${PRUNE_MODULES} -ne 0 ]]; then
find "${FW_MODPATH}" -mindepth 1 -maxdepth 1 -type d | while read DIR; do find "${FW_MODPATH}" -mindepth 1 -maxdepth 1 -type d | while read DIR; do
COUNT=$(find ${DIR} -type f ! \( -name '*.ko' -o -name 'modules.*' \) | wc -l); COUNT=$(find "${DIR}" -type f ! \( -name '*.ko' -o -name 'modules.*' \) | wc -l);
if [[ ${COUNT} -eq 0 ]]; then if [[ ${COUNT} -eq 0 ]]; then
echo "Pruning ${DIR}" echo "Pruning ${DIR}"
rm -rf ${DIR} rm -rf "${DIR}"
else else
echo "Keeping ${DIR}" echo "Keeping ${DIR}"
fi fi
@@ -126,9 +136,9 @@ function update_vc_libs {
echo " *** Updating VideoCore libraries" echo " *** Updating VideoCore libraries"
if [[ -e ${ROOT_PATH}/bin/sh ]]; then if [[ -e ${ROOT_PATH}/bin/sh ]]; then
ELFOUTPUT=$(readelf -a "${ROOT_PATH}/bin/sh"; readelf -h "${ROOT_PATH}/bin/sh") local ELFOUTPUT=$(readelf -a "${ROOT_PATH}/bin/sh"; readelf -h "${ROOT_PATH}/bin/sh")
else else
ELFOUTPUT="VFP_args" local ELFOUTPUT="VFP_args"
fi fi
if [[ "${ELFOUTPUT}" != "${ELFOUTPUT/VFP_args/}" || \ if [[ "${ELFOUTPUT}" != "${ELFOUTPUT/VFP_args/}" || \
"${ELFOUTPUT}" != "${ELFOUTPUT/hard-float/}" ]]; then "${ELFOUTPUT}" != "${ELFOUTPUT/hard-float/}" ]]; then
@@ -150,28 +160,31 @@ function update_sdk {
} }
function show_notice { function show_notice {
local FULL_NOTICE=`eval curl -Lfs ${GITHUB_AUTH_PARAM} https://raw.githubusercontent.com/hexxeh/rpi-firmware/${FW_REV}/NOTICE.md` local NOTICE_URI=${REPO_CONTENT_URI}/${FW_REV}/NOTICE.md
local NOTICE=$(echo "$FULL_NOTICE" | tail -n+2) local FULL_NOTICE=$(eval curl -fs ${CURL_OPTIONS} "${NOTICE_URI}")
if [ -z "$NOTICE" ]; then if [ -z "${FULL_NOTICE}" ]; then
return return
fi fi
local NOTICE_HASH_HEAD=$(echo "$FULL_NOTICE" | head -1) local NOTICE_HASH_HEAD=$(echo "${FULL_NOTICE}" | head -1)
if [ $(echo "${NOTICE_HASH_HEAD}" | awk -F: '{print $1}') == "HASH" ]; then if [ "$(echo "${NOTICE_HASH_HEAD}" | awk -F: '{print $1}')" == "HASH" ]; then
local NOTICE_HASH_EXISTS=true local NOTICE_HASH_EXISTS=true
local NOTICE_HASH=$(echo "${NOTICE_HASH_HEAD}" | awk '{print $2}') local NOTICE_HASH=$(echo "${NOTICE_HASH_HEAD}" | awk '{print $2}')
else else
local NOTICE_HASH_EXISTS=false local NOTICE_HASH_EXISTS=false
fi fi
if ${NOTICE_HASH_EXISTS}; then if ${NOTICE_HASH_EXISTS}; then
local NOTICE=$(echo "${FULL_NOTICE}" | tail -n+2)
local NEW_HASH=${FW_REV} local NEW_HASH=${FW_REV}
local LOCAL_lt_NOTICE=$(compare_hashes ${LOCAL_HASH} lt ${NOTICE_HASH}) local LOCAL_lt_NOTICE=$(compare_hashes "${LOCAL_HASH}" lt "${NOTICE_HASH}")
local NEW_ge_NOTICE=$(compare_hashes ${NEW_HASH} ge ${NOTICE_HASH}) local NEW_ge_NOTICE=$(compare_hashes "${NEW_HASH}" ge "${NOTICE_HASH}")
if ! ${LOCAL_lt_NOTICE} && ! ${NEW_ge_NOTICE}; then if ! ${LOCAL_lt_NOTICE} && ! ${NEW_ge_NOTICE}; then
return return
fi fi
else
local NOTICE=${FULL_NOTICE}
fi fi
echo "$NOTICE" echo "${NOTICE}"
if ! echo "$NOTICE" | grep -q WARNING; then if ! echo "${NOTICE}" | grep -q WARNING; then
return return
fi fi
if [[ ${SKIP_WARNING} -ne 0 ]]; then if [[ ${SKIP_WARNING} -ne 0 ]]; then
@@ -235,20 +248,23 @@ function finalise {
function do_backup { function do_backup {
if [[ ${SKIP_BACKUP} -eq 0 ]]; then if [[ ${SKIP_BACKUP} -eq 0 ]]; then
echo " *** Backing up files (this will take a few minutes)" echo " *** Backing up files (this will take a few minutes)"
if [[ -d "${FW_PATH}.bak" ]]; then local OLD_FW_PATH=${FW_PATH}.bak
if [[ -d "${OLD_FW_PATH}" ]]; then
echo " *** Remove old firmware backup" echo " *** Remove old firmware backup"
rm -rf "${FW_PATH}.bak" rm -rf "${OLD_FW_PATH}"
fi fi
echo " *** Backing up firmware" echo " *** Backing up firmware"
cp -a "${FW_PATH}" "${FW_PATH}.bak" cp -a "${FW_PATH}" "${OLD_FW_PATH}"
if [[ ${SKIP_KERNEL} -eq 0 ]]; then if [[ ${SKIP_KERNEL} -eq 0 ]]; then
if [[ -d "${FW_MODPATH}.bak" ]]; then OLD_FW_MODPATH=${FW_MODPATH}.bak
if [[ -d "${OLD_FW_MODPATH}" ]]; then
echo " *** Remove old modules backup" echo " *** Remove old modules backup"
rm -rf "${FW_MODPATH}.bak" rm -rf "${OLD_FW_MODPATH}"
fi fi
echo " *** Backing up modules $(uname -r)" echo " *** Backing up modules $(uname -r)"
if [[ -d "${FW_MODPATH}/$(uname -r)" ]]; then local CURRENT_FW_MODPATH=${FW_MODPATH}/$(uname -r)
mkdir -p "${FW_MODPATH}.bak" && cp -a "${FW_MODPATH}/$(uname -r)" "${FW_MODPATH}.bak" if [[ -d "${CURRENT_FW_MODPATH}" ]]; then
mkdir -p "${OLD_FW_MODPATH}" && cp -a "${CURRENT_FW_MODPATH}" "${OLD_FW_MODPATH}"
fi fi
fi fi
fi fi
@@ -286,14 +302,15 @@ function do_update {
function download_rev { function download_rev {
if [[ ${SKIP_DOWNLOAD} -eq 0 ]]; then if [[ ${SKIP_DOWNLOAD} -eq 0 ]]; then
if ! eval curl -Lfs ${GITHUB_AUTH_PARAM} --output /dev/null --head --fail "${REPO_URI}/tarball/${FW_REV}"; then local FW_TARBALL_URI=${REPO_URI}/tarball/${FW_REV}
if ! eval curl -fs ${CURL_OPTIONS} --output /dev/null --head "${FW_TARBALL_URI}"; then
echo "Invalid git hash specified" echo "Invalid git hash specified"
exit 1 exit 1
fi fi
echo " *** Downloading specific firmware revision (this will take a few minutes)" echo " *** Downloading specific firmware revision (this will take a few minutes)"
rm -rf "${FW_REPOLOCAL}" rm -rf "${FW_REPOLOCAL}"
mkdir -p "${FW_REPOLOCAL}" mkdir -p "${FW_REPOLOCAL}"
eval curl -L ${GITHUB_AUTH_PARAM} "${REPO_URI}/tarball/${FW_REV}" | tar xzf - -C "${FW_REPOLOCAL}" --strip-components=1 eval curl ${CURL_OPTIONS} "${FW_TARBALL_URI}" | tar xzf - -C "${FW_REPOLOCAL}" --strip-components=1
fi fi
} }
@@ -305,7 +322,7 @@ function remove_rev {
} }
function noobs_fix { function noobs_fix {
echo " !!! $BOOT_PATH appears to contain NOOBS files" echo " !!! ${BOOT_PATH} appears to contain NOOBS files"
echo " This may occur if fstab contains incorrect entries." echo " This may occur if fstab contains incorrect entries."
echo " rpi-update will attempt to correct fstab." echo " rpi-update will attempt to correct fstab."
read -p "Would you like to proceed? (y/N)" -n 1 -r -s read -p "Would you like to proceed? (y/N)" -n 1 -r -s
@@ -314,45 +331,45 @@ function noobs_fix {
exit 1; exit 1;
fi fi
if ! grep -qE "/dev/mmcblk0p1\s+/boot" ${ROOT_PATH}/etc/fstab; then if ! grep -qE "/dev/mmcblk0p1\s+/boot" "${ROOT_PATH}/etc/fstab"; then
echo "Unexpected fstab entry" echo "Unexpected fstab entry"
exit 1 exit 1
fi fi
local ROOTNUM=`cat /proc/cmdline | sed -n 's|.*root=/dev/mmcblk0p\([0-9]*\).*|\1|p'` local ROOTNUM=$(cat "/proc/cmdline" | sed -n 's|.*root=/dev/mmcblk0p\([0-9]*\).*|\1|p')
if [ ! "$ROOTNUM" ];then if [ ! "${ROOTNUM}" ];then
echo "Could not determine root partition." echo "Could not determine root partition."
exit 1 exit 1
fi fi
local BOOT_DEV="/dev/mmcblk0p$((ROOTNUM-1))" local BOOT_DEV="/dev/mmcblk0p$((ROOTNUM-1))"
local ROOT_DEV="/dev/mmcblk0p${ROOTNUM}" local ROOT_DEV="/dev/mmcblk0p${ROOTNUM}"
sed ${ROOT_PATH}/etc/fstab -e "s|^.*[^#].* / |${ROOT_DEV} / |;s|^.*[^#].* /boot |${BOOT_DEV} /boot |" sed "${ROOT_PATH}/etc/fstab" -e "s|^.*[^#].* / |${ROOT_DEV} / |;s|^.*[^#].* /boot |${BOOT_DEV} /boot |"
read -p "Does this look correct? (y/N)" -n 1 -r -s read -p "Does this look correct? (y/N)" -n 1 -r -s
echo echo
if ! [[ $REPLY =~ ^[Yy]$ ]]; then if ! [[ $REPLY =~ ^[Yy]$ ]]; then
exit 1; exit 1;
fi fi
sed ${ROOT_PATH}/etc/fstab -i -e "s|^.*[^#].* / |${ROOT_DEV} / |;s|^.*[^#].* /boot |${BOOT_DEV} /boot |" sed "${ROOT_PATH}/etc/fstab" -i -e "s|^.*[^#].* / |${ROOT_DEV} / |;s|^.*[^#].* /boot |${BOOT_DEV} /boot |"
umount /boot umount "${BOOT_PATH}"
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo "Failed to umount /boot. Remount manually and try again." echo "Failed to umount ${BOOT_PATH}. Remount manually and try again."
exit 1 exit 1
else else
mount /boot mount "/boot"
fi fi
} }
function get_hash_date { function get_hash_date {
commit_api="https://api.github.com/repos/Hexxeh/rpi-firmware/git/commits/" local COMMITS_URI=${REPO_API_URI}/commits/$1
eval curl -Ls ${GITHUB_AUTH_PARAM} "$commit_api"$1 | grep "date" | head -1 | awk -F\" '{print $4}' eval curl -s ${CURL_OPTIONS} "${COMMITS_URI}" | grep "date" | head -1 | awk -F\" '{print $4}'
} }
function compare_hashes { function compare_hashes {
date1=$(get_hash_date $1) local DATE1=$(get_hash_date "$1")
date2=$(get_hash_date $3) local DATE2=$(get_hash_date "$3")
if [ $(date -d "$date1" +%s) -$2 $(date -d "$date2" +%s) ]; then if [ $(date -d "${DATE1}" +%s) -$2 $(date -d "${DATE2}" +%s) ]; then
echo true echo true
else else
echo false echo false
@@ -361,8 +378,8 @@ function compare_hashes {
function get_long_hash { function get_long_hash {
# ask github for long version hash # ask github for long version hash
local REPO_API=${REPO_URI/github.com/api.github.com\/repos}/commits/$1 local COMMITS_URI=${REPO_API_URI}/commits/$1
eval curl -Ls ${GITHUB_AUTH_PARAM} ${REPO_API} | awk 'BEGIN {hash=""} { if (hash == "" && $1 == "\"sha\":") {hash=substr($2, 2, 40);} } END {print hash}' eval curl -s ${CURL_OPTIONS} "${COMMITS_URI}" | awk 'BEGIN {hash=""} { if (hash == "" && $1 == "\"sha\":") {hash=substr($2, 2, 40);} } END {print hash}'
} }
@@ -375,24 +392,26 @@ echo " *** Raspberry Pi firmware updater by Hexxeh, enhanced by AndrewS and Dom"
if [[ ! -d ${WORK_PATH} ]]; then if [[ ! -d ${WORK_PATH} ]]; then
echo " !!! ${WORK_PATH} doesn't exist - creating" echo " !!! ${WORK_PATH} doesn't exist - creating"
mkdir -p ${WORK_PATH} mkdir -p "${WORK_PATH}"
fi fi
if [[ ${UPDATE_SELF} -ne 0 ]]; then if [[ ${UPDATE_SELF} -ne 0 ]]; then
update_self update_self
else
rm -f "${SELFUPDATE_SCRIPT}"
fi fi
if [[ ! -d "${FW_PATH}" ]]; then if [[ ! -d "${FW_PATH}" ]]; then
echo " !!! ${FW_PATH} doesn't exist - creating" echo " !!! ${FW_PATH} doesn't exist - creating"
mkdir -p ${FW_PATH} mkdir -p "${FW_PATH}"
fi fi
if [[ ${SKIP_KERNEL} -eq 0 ]] && [[ ! -d "${FW_MODPATH}" ]]; then if [[ ${SKIP_KERNEL} -eq 0 ]] && [[ ! -d "${FW_MODPATH}" ]]; then
echo " !!! ${FW_MODPATH} doesn't exist - creating" echo " !!! ${FW_MODPATH} doesn't exist - creating"
mkdir -p ${FW_MODPATH} mkdir -p "${FW_MODPATH}"
fi fi
if [[ ${NOOBS_CHECK} -eq 1 ]] && [[ -f ${BOOT_PATH}/recovery.elf ]]; then if [[ ${NOOBS_CHECK} -eq 1 ]] && [[ -f "${BOOT_PATH}/recovery.elf" ]]; then
noobs_fix noobs_fix
fi fi
@@ -407,7 +426,7 @@ if [[ "${FW_REV_IN}" == "" ]]; then
FW_REV_IN=${BRANCH} FW_REV_IN=${BRANCH}
fi fi
FW_REV=$(get_long_hash ${FW_REV_IN}) FW_REV=$(get_long_hash "${FW_REV_IN}")
if [[ "${FW_REV}" == "" ]]; then if [[ "${FW_REV}" == "" ]]; then
echo " *** Invalid hash given" echo " *** Invalid hash given"
@@ -418,25 +437,26 @@ if [[ ! -f "${FW_REVFILE}" ]]; then
LOCAL_HASH=0 LOCAL_HASH=0
echo " *** We're running for the first time" echo " *** We're running for the first time"
if [[ ${JUST_CHECK} -ne 0 ]]; then if [[ ${JUST_CHECK} -ne 0 ]]; then
echo " *** Firmware update required. No local hash to compare to."
exit 2 exit 2
fi fi
do_backup do_backup
else else
LOCAL_HASH=$(get_long_hash $(cat "${FW_REVFILE}")) LOCAL_HASH=$(get_long_hash "$(cat "${FW_REVFILE}")")
if [[ ${LOCAL_HASH} == "${FW_REV}" ]]; then if [[ "${LOCAL_HASH}" == "${FW_REV}" ]]; then
echo " *** Your firmware is already up to date" echo " *** Your firmware is already up to date"
exit 0 exit 0
fi fi
if [[ ${JUST_CHECK} -ne 0 ]]; then if [[ ${JUST_CHECK} -ne 0 ]]; then
if $(compare_hashes ${LOCAL_HASH} lt ${FW_REV}); then if $(compare_hashes "${LOCAL_HASH}" lt "${FW_REV}"); then
echo " *** Firmware update required. New commits available:" echo " *** Firmware update required. New commits available:"
DIFF_API=${REPO_URI/github.com/api.github.com\/repos}/compare/${LOCAL_HASH}...${FW_REV} DIFF_URI=${REPO_API_URI}/compare/${LOCAL_HASH}...${FW_REV}
else else
echo " *** Firmware downgrade requested. Commits to drop:" echo " *** Firmware downgrade requested. Commits to drop:"
DIFF_API=${REPO_URI/github.com/api.github.com\/repos}/compare/${FW_REV}...${LOCAL_HASH} DIFF_URI=${REPO_API_URI}/compare/${FW_REV}...${LOCAL_HASH}
fi fi
SEPARATOR="======================================================" SEPARATOR="======================================================"
eval curl -Ls ${GITHUB_AUTH_PARAM} ${DIFF_API} | awk -v SEPARATOR="${SEPARATOR}" -F\" ' { if ($2 == "commits") {commits=1} if (commits && $2 == "message") {print SEPARATOR "\nCommit: " $4} }' | sed 's/\\n\\n/\nCommit:\ /g' eval curl -s ${CURL_OPTIONS} "${DIFF_URI}" | awk -v SEPARATOR="${SEPARATOR}" -F\" ' { if ($2 == "commits") {commits=1} if (commits && $2 == "message") {print SEPARATOR "\nCommit: " $4} }' | sed 's/\\n\\n/\nCommit:\ /g'
exit 2 exit 2
fi fi
fi fi