#!/usr/bin/env bash

PACKAGE="f-secure-linuxsecurity"
ACTIVATE_CMD="/opt/f-secure/linuxsecurity/bin/activate"
LOG_FILE="fsls64-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;
EOF
}

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

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

install_package () {
    if [ -e /etc/debian_version ]; 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
}

prepare_activate_package () {
    local installed_version
    local bundled_version
    if [ -e /etc/debian_version ]; 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
        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 pm_guts2
    pm_guts2=$(sed -n 's/guts2_address=\(.*\)/\1/p' "${SRCDIR}/setup-config.ini")
    local policy_file
    policy_file="${SRCDIR}/safe.cosmos"
    local policy_arg

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

    FSECURE_GUTS2_SERVER="${pm_guts2}" \
    FSECURE_BASEGUARD_GUTS2_SERVER="${pm_guts2}" \
    FSECURE_FSBG_GUTS2_SERVER="${pm_guts2}" \
    "${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"
    prepare_activate_package
    activate "$@"
}

main "$@"
