#!/usr/bin/env bash

PACKAGE="linuxsecurity-installer"
ACTIVATE_CMD="/opt/f-secure/linuxsecurity/bin/activate"
LOG_FILE="wsls64-install.log"

print_usage () {
    cat <<EOF
Usage: $0 [OPTION]...

Possible options:
  --package=PATH                   Path to a content package for isolated
                                   environment;
  --automatic-updates=all|none     Enable or disable automatic updates
                                   (enabled by default);
  --product-version=VERSION        Use specific product version;
  --next-boot                      Schedule the product to be activated
                                   on the next boot;
  --updates-use-tls=yes|no         Use TLS for updates;
  --override-distro=ID:VERSION-ID  Spoof a supported operating system;
EOF
}

SRCDIR=$(dirname "$(readlink -f "$0")")

# Propagate errors through pipes. This is needed because we are
# capturing commands output with tee.
set -o pipefail

debianesque () {
    local override=$1
    if [[ -n $override ]]; then
        case "$override" in
            debian:*|ubuntu:*)
                true
                ;;
            *)
                false
                ;;
        esac
    else
        [ -e /etc/debian_version ]
    fi
}

install_package () {
    local override=$1
    if debianesque "$override"; then
        dpkg -i "${SRCDIR}/${PACKAGE}.deb" 2>&1 | tee -a "$LOG_FILE"
    else
        rpm -Uvh "${SRCDIR}/${PACKAGE}.rpm" 2>&1 | tee -a "$LOG_FILE"
    fi

    # shellcheck disable=SC2181
    if [[ $? -ne 0 ]]; then
        echo "Failed to install the product!" | tee -a "$LOG_FILE"
        exit 1
    fi
}

distro_override () {
    while [[ "X$1" =~ ^X- ]]; do
        case "$1" in
            --override-distro)
                echo "$2"
                return
                ;;
            --override-distro=*)
                echo "${1#*=}"
                return
                ;;
            *)
                shift
                ;;
        esac
    done
}

prepare_activate_package () {
    local override=$1 installed_version bundled_version
    if debianesque "$override"; then
        installed_version=$(dpkg -s "${PACKAGE}" 2>/dev/null | sed -n 's/Version:\s*//p')
        bundled_version=$(dpkg -I "${SRCDIR}"/"${PACKAGE}".deb | sed -n 's/\s*Version:\s*//p')
    else
        installed_version=$(rpm -qi "${PACKAGE}" 2>/dev/null | sed -n 's/Version\s*:\s*//p')
        bundled_version=$(rpm -qip "${SRCDIR}"/"${PACKAGE}".rpm | sed -n 's/Version\s*:\s*//p')
    fi

    echo "Installed: ${installed_version}, bundled: ${bundled_version}" >> "$LOG_FILE"

    if [ -z "${installed_version}" ]; then
        install_package "$override"
        return
    fi

    if [ "${installed_version}" != "${bundled_version}" ]; then
        echo "There already is ${PACKAGE} package installed with a different version." \
            "Please, remove it manually and run the installer again." | tee -a "$LOG_FILE"
        exit 1
    fi
}

activate () {
    local policy_file
    policy_file="${SRCDIR}/safe.cosmos"
    local policy_arg

    if [ -e "${policy_file}" ]; then
        policy_arg="--initial-policy=${policy_file}"
    fi

    "${ACTIVATE_CMD}" \
        --config-dir="${SRCDIR}" ${policy_arg:+"$policy_arg"} "$@" 2>&1 | tee -a "$LOG_FILE"

    # shellcheck disable=SC2181
    if [[ $? -ne 0 ]]; then
        echo "Failed to activate the product!" | tee -a "$LOG_FILE"
        exit 1
    fi
}

option_present () {
    local long_option
    local short_option
    long_option="$1"
    short_option="$2"
    shift 2
    for option in "$@"; do
        if [ "${option}" == "${long_option}" ] || [ "${option}" == "${short_option}" ]; then
            return
        fi
    done
    return 1
}

main () {
    if option_present "--help" "-h" "$@"; then
        print_usage "$0"
        exit
    fi
    echo "==== $(date) ====" >> "$LOG_FILE"
    local override
    override=$(distro_override "$@") &&
    prepare_activate_package "$override" &&
    activate "$@"
}

main "$@"
