#!/bin/bash

# Script to install Naver Whale on HamoniKR system.
# Run as root or insert `sudo -E` before `bash`:
# curl -sL https://pkg.hamonikr.org/whale.install | sudo -E bash -
#   or
# wget -qO- https://pkg.hamonikr.org/whale.install | sudo -E bash -
#

export DEBIAN_FRONTEND=noninteractive

print_status() {
    echo
    echo "## $1"
    echo
}

if test -t 1; then # if terminal
    ncolors=$(which tput > /dev/null && tput colors) # supports color
    if test -n "$ncolors" && test $ncolors -ge 8; then
        termcols=$(tput cols)
        bold="$(tput bold)"
        underline="$(tput smul)"
        standout="$(tput smso)"
        normal="$(tput sgr0)"
        black="$(tput setaf 0)"
        red="$(tput setaf 1)"
        green="$(tput setaf 2)"
        yellow="$(tput setaf 3)"
        blue="$(tput setaf 4)"
        magenta="$(tput setaf 5)"
        cyan="$(tput setaf 6)"
        white="$(tput setaf 7)"
    fi
fi

print_bold() {
    title="$1"
    text="$2"

    echo
    echo "${red}================================================================================${normal}"
    echo "${red}================================================================================${normal}"
    echo
    echo -e "  ${bold}${yellow}${title}${normal}"
    echo
    echo -en "  ${text}"
    echo
    echo "${red}================================================================================${normal}"
    echo "${red}================================================================================${normal}"
}

bail() {
    echo 'Error executing command, exiting'
    exit 1
}

exec_cmd_nobail() {
    echo "+ $1"
    bash -c "$1"
}

exec_cmd() {
    exec_cmd_nobail "$1" || bail
}

setup() {

print_status "Installing the Naver Whale Repo..."

PRE_INSTALL_PKGS=""

# Check that HTTPS transport is available to APT

if [ ! -e /usr/lib/apt/methods/https ]; then
    PRE_INSTALL_PKGS="${PRE_INSTALL_PKGS} apt-transport-https"
fi

if [ ! -x /usr/bin/lsb_release ]; then
    PRE_INSTALL_PKGS="${PRE_INSTALL_PKGS} lsb-release"
fi

if [ ! -x /usr/bin/curl ] && [ ! -x /usr/bin/wget ]; then
    PRE_INSTALL_PKGS="${PRE_INSTALL_PKGS} curl"
fi

# Used by apt-key to add new keys

if [ ! -x /usr/bin/gpg ]; then
    PRE_INSTALL_PKGS="${PRE_INSTALL_PKGS} gnupg"
fi

if [ "X${PRE_INSTALL_PKGS}" != "X" ]; then
    print_status "Installing packages required for setup:${PRE_INSTALL_PKGS}..."
    # This next command needs to be redirected to /dev/null or the script will bork
    # in some environments
    exec_cmd "apt-get install -y${PRE_INSTALL_PKGS} > /dev/null 2>&1"
fi

DistributorID=$(lsb_release -i -s)
RELEASE=$(lsb_release -d -s)
CODENAME=$(lsb_release -c -s)

check_alt_hamonikr() {
    if [ "X${RELEASE}" == "X${2}" ]; then
        echo
        echo "## You seem to be using ${1} version ${RELEASE}."
        echo "## This maps to ${3} \"${4}\"... Adjusting for you..."
        DISTRO="${4}"
    fi
}

if [ "X${DistributorID}" != "XHamoniKR" ]; then
    print_status "Detected ${DistributorID}"
else
    check_alt_hamonikr "HamoniKR" "me" "HamoniKR" "me"
    check_alt_hamonikr "HamoniKR" "sun" "HamoniKR" "sun"
    check_alt_hamonikr "HamoniKR" "jin" "HamoniKR" "jin"
fi

if [[ -f "/etc/apt/sources.list.d/naver-whale.list" ]]; then
    exec_cmd "rm -f /etc/apt/sources.list.d/naver-whale.list"
    print_status 'Removing Repository'
fi

print_status 'Adding Naver Whale signing key to your keyring...'

if [ -x /usr/bin/curl ]; then
    exec_cmd 'curl -s -L http://repo.whale.naver.com/stable/deb/public.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/naver-whale.gpg > /dev/null'
else
    exec_cmd 'wget -qO - http://repo.whale.naver.com/stable/deb/public.gpg | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/naver-whale.gpg > /dev/null'
fi

print_status "Creating apt list file for Naver Whale..."

if [ "X${CODENAME}" == "Xbionic" ]  || [ "X${CODENAME}" == "Xfocal" ]  || [ "X${CODENAME}" == "Xgroovy" ] ; then
	exec_cmd "echo 'deb [arch=amd64] http://repo.whale.naver.com/stable/deb stable main' > /etc/apt/sources.list.d/naver-whale.list"
elif [ "X${CODENAME}" == "Xtricia" ] ||  [ "X${CODENAME}" == "Xtina" ] ||  [ "X${CODENAME}" == "Xtessa" ] ||  [ "X${CODENAME}" == "Xtara" ] ; then
	exec_cmd "echo 'deb [arch=amd64] http://repo.whale.naver.com/stable/deb stable main' > /etc/apt/sources.list.d/naver-whale.list"
else
	exec_cmd "echo 'deb [arch=amd64] http://repo.whale.naver.com/stable/deb stable main' > /etc/apt/sources.list.d/naver-whale.list"
fi	

print_status 'Running `apt-get update` for you...'

exec_cmd 'apt-get update'

print_status """\`${bold}Updated Naver Whale Repo Settings.${normal}\`
## If you have any questions, please visit the link below :
     https://hamonikr.org
"""
print_bold "Naver Whale 저장소를 추가 했습니다.\r\n  웨일 브라우러를 설치합니다."
exec_cmd 'sudo apt install -y naver-whale-stable'
}

## Defer setup until we have the complete script
setup
