#! /bin/bash
#-------------------------------------------------------------------------------
#
# Copyright 2013 Cumulus Networks, Inc.  All rights reserved
#

_PAGER=${PAGER:-more}
URL_RE='^http://.*/.*'

LIC_DIR="/etc/cumulus"
PERSIST_LIC_DIR="/mnt/persist/etc/cumulus"

#Print current license, if any.
if [ -z "$1" ]; then
    if [ -f "$LIC_DIR/.license" ]; then
        cat "$LIC_DIR/.license"
        exit 0
    elif [ -f "$LIC_DIR/.license.txt" ]; then
        cat "$LIC_DIR/.license.txt"
        exit 0
    elif /usr/sbin/switchd -lic ; then
        # Platform does not require license
        exit 0
    fi
    echo "No license installed!" >&2
    exit 20
fi

#Must be root beyond this point
if (( EUID != 0 )); then
   echo "You must have root privileges to run this command." 1>&2
   exit 100
fi

#Delete license
if [ x"$1" == "x-d" ]; then
    if [ -f "$LIC_DIR/.license" ]; then
        rm -f "$LIC_DIR/.license"
        rm -f "$PERSIST_LIC_DIR/.license"
    elif [ -f "$LIC_DIR/.license.txt" ]; then
        rm -f "$LIC_DIR/.license.txt"
        rm -f "$PERSIST_LIC_DIR/.license.txt"
    fi

    echo License file uninstalled.
    exit 0
fi

function usage {
    echo "Usage: $0 (-i (license_file | URL) | -d)" >&2
    echo "    -i  Install a license, via stdin, file, or URL." >&2
    echo "    -d  Delete the current installed license." >&2
    echo >&2
    echo " cl-license prints, installs or deletes a license on this switch." >&2
}

if [ x"$1" != 'x-i' ]; then
    usage
    exit 100
fi
shift
if [ ! -f "$1" ]; then
    if [ -n "$1" ]; then
        if ! echo "$1" | grep -q "$URL_RE"; then
            usage
            echo "file $1 not found or not readable." >&2
            exit 100
        fi
    fi
fi

function clean_tmp {
    rm $1
    [ -n "$EMPTY_LICENSE" ] && rm -f "$EMPTY_LICENSE"
}

MKTEMPFILE='mktemp /tmp/lic.XXXXXX'
if [ -z "$1" ]; then
    LIC_FILE=$($MKTEMPFILE)
    trap "clean_tmp $LIC_FILE" EXIT
    echo "Paste license text here, then hit ctrl-d" >&2
    cat >$LIC_FILE
else
    if echo "$1" | grep -q "$URL_RE"; then
        LIC_FILE=$($MKTEMPFILE)
        trap "clean_tmp $LIC_FILE" EXIT
        if ! wget "$1" -O $LIC_FILE; then
            echo "Couldn't download $1 via HTTP!" >&2
            exit 10
        fi
    else
        LIC_FILE="$1"
    fi
fi

function backup {
    [ -f "$1" ] && mv -f "$1" "$1.install.bak"
}

function restore {
    [ -f "$1.install.bak" ] && mv -f "$1.install.bak" "$1"
}

function cp_p {
    cp "$1" "$2" && chmod 644 "$2"
}

grep -q "=" "$LIC_FILE"
LICENSE_V1=$?

[ -f "/etc/cumulus/.license" ] && [ $LICENSE_V1 -eq 0 ] && {
    echo '*****************************************************************' >&2
    echo 'INFO: You installed old style license when new one is present.' >&2
    echo '*****************************************************************' >&2
}

EMPTY_LICENSE=
[ $LICENSE_V1 -eq 0 ] && _LIC_FILE="$LIC_DIR/.license.txt" || _LIC_FILE="$LIC_DIR/.license"
[ "$LIC_FILE" != "_LIC_FILE" ] && {
    [ ! -f "$_LIC_FILE" ] && EMPTY_LICENSE="$_LIC_FILE"
    backup "$_LIC_FILE"
    cp "$LIC_FILE" "$_LIC_FILE"
}

/usr/sbin/switchd -lic > /dev/null
SWITCHD_RETCODE=$?

if [ $SWITCHD_RETCODE -eq 99 ]; then
    $_PAGER /usr/share/cumulus/EULA.txt
    tries=10
    while (( $tries > 0 )); do
        echo "I (on behalf of the entity who will be using the software) accept"
        read -p "and agree to the EULA  (yes/no): " yn
        case $yn in
            [Yy]es ) break;;
            [Nn]o )
                echo EULA not agreed to, aborting. >&2
                exit 2;;
                * )
                    echo "Please answer 'yes' or 'no'"
                    ((tries--))
        esac
    done
    if [ $tries = 0 ]; then
        echo "Invalid EULA response, aborting" >&2
        exit 3
    fi
elif [ $SWITCHD_RETCODE -ne 0 ]; then
    echo '******************************' >&2
    echo ERROR: License file not valid. >&2
    echo ERROR: No license installed. >&2
    echo '******************************' >&2
    restore "$_LIC_FILE"
    exit 1
fi

mkdir -p "$LIC_DIR"
cp_p "$LIC_FILE" "$_LIC_FILE" && EMPTY_LICENSE=

mkdir -p "$PERSIST_LIC_DIR"
[ $LICENSE_V1 -eq 0 ] && _PERSIST_LIC_FILE="$PERSIST_LIC_DIR/.license.txt" || _PERSIST_LIC_FILE="$PERSIST_LIC_DIR/.license"
cp_p "$LIC_FILE" "$_PERSIST_LIC_FILE"

echo License file installed.
systemctl -q is-active switchd &>/dev/null || {
    echo Service \'switchd\' is not running.
    echo Run this command:
    echo sudo systemctl restart switchd
    echo
    echo Or reboot to enable functionality.
}
