#!/usr/bin/env sh

set -e

: ${ZOLA_REPO='https://github.com/getzola/zola'}
: ${ZOLA_INSTALL_DIR="${HOME}/.cargo/bin"}

# Helpers for ANSI color and logging.
name="$(basename $0)"; esc=''; reset="${esc}[0m"
red() { local string="${1}"; echo "${esc}[31m${string}${reset}"; }
yellow() { local string="${1}"; echo "${esc}[33m${string}${reset}"; }
green() { local string="${1}"; echo "${esc}[32m${string}${reset}"; }
blue() { local string="${1}"; echo "${esc}[34m${string}${reset}"; }
magenta() { local string="${1}"; echo "${esc}[35m${string}${reset}"; }
log() { local string="${1}"; echo "[$(blue $name)] ${string}"; }

prompt_for_update() {
    local current="${1}"
    local latest="${2}"

    [ -n "${current}" ] \
        && installed="You're using Zola $(yellow $current)" \
        || installed="Zola is $(red 'not installed')"

    log "${installed}, but $(magenta $latest) is available."

    # Prompt for an update. Exit the entire script if declined.
    read -p "$(log "Would you like to install Zola $(magenta $latest)? [y/N]: ")" answer
    case $answer in
        [Yy]*) log "Downloading Zola $(magenta $latest) from GitHub." ;;
        [Nn]*|*) log $(red "Aborted!"); exit ;;
    esac
}

install_zola() {
    local version="${1}"
    local download_url="${ZOLA_REPO}/releases/download/${version}/zola-${version}-x86_64-unknown-linux-gnu.tar.gz"

    curl --progress-bar --location $download_url | tar --gunzip --extract --directory=$ZOLA_INSTALL_DIR --file -
}

update_zola() {
    # Find the current Zola version (if any).
    [ -n "$(which zola 2> /dev/null)" ] && current_version=$(zola --version | awk '{print "v"$2}')

    # Find the latest available Zola version. Use --refs to avoid listing
    # annotated commit objects that include the dereference syntax '^{}'.
    # Find more background at https://stackoverflow.com/a/15472310.
    local latest_version=$(git ls-remote --tags --refs $ZOLA_REPO | # Fetch remote tags.
                           sort -t '/' -k 3 -V |                    # Sort them by version.
                           tail -n 1 |                              # Take the latest one.
                           awk -F / '{print $3}')                   # Return only the tag.

    if [ "${current_version}" != "${latest_version}" ]; then
        prompt_for_update "${current_version}" "${latest_version}"
        install_zola "${latest_version}"
    fi

    log "You're using Zola $(magenta $latest_version). $(green "Everything is up-to-date!")"
}

update_zola
