#!/bin/bash

# according to /usr/include/sysexit.h
EXIT_USAGE_ERROR=64

exit_with_usage() {
    echo ""
    echo "  tc-config     <key>   <value list>"
    echo ""
    echo "Key List"
    echo "  db.localhost   WO     <true|false>"
    echo "  https.enable   WO     <true|false>"
    echo "  fw.upgrade     WO     <file>  <phase>"
    echo "  util.random    RO     <length>"
    echo "  release        RO"
    echo "  revision       RO"
    exit $EXIT_USAGE_ERROR
} 

select_nic() {
    echo "Select the network interface controller (NIC)"

    if [ -f /tmp/INSNIC ]; then
        NIC=$(cat /tmp/INSNIC)
        echo "Select $NIC for TC system"
    else
        NIC_LIST=`ip link show | grep BROADCAST | awk -F ": " '{print $2}'`

        PS3='Input the number of NIC for TC system: '
        select NIC in $NIC_LIST;
        do
            if [ -n "$NIC" ]; then
                echo "Select $NIC for TC system"
                break
            fi
        done 2>&3
    fi

    if [ -z "$NIC" ]; then
        echo "No available NIC"
        exit 1
    fi

    nic_cfg=/etc/sysconfig/network-scripts/ifcfg-${NIC}
    if [ -f ${nic_cfg} ]; then
        echo "NIC configuration, ${nic_cfg}"
    else
        echo "[FAIL] Cannot find NIC script ${nic_cfg}"
        exit 1
    fi

    ip link set ${NIC} down
    sed -i "/^BOOTPROTO.*/d" ${nic_cfg}
    sed -i "/^IPADDR.*/d" ${nic_cfg}
    sed -i "/^NETMASK.*/d" ${nic_cfg}
    sed -i "/^ONBOOT.*/d" ${nic_cfg}
    sed -i "/^NM_CONTROLLED.*/d" ${nic_cfg}
    sed -i "$ a BOOTPROTO=static" ${nic_cfg}
    sed -i "$ a IPADDR=192.168.0.1" ${nic_cfg}
    sed -i "$ a NETMASK=255.255.255.0" ${nic_cfg}
    sed -i "$ a ONBOOT=yes" ${nic_cfg}
    sed -i "$ a NM_CONTROLLED=no" ${nic_cfg}
    ip link set ${NIC} up

    "$TC_HOME/bin/tc-tftp" nic ${NIC}

    # update nic in web console configuration file
    sed -i "s/eth0/${NIC}/g" "$TC_HOME/etc/tc.ini"
    sed -i "s/eth0/${NIC}/g" "$TC_HOME/etc/tcs-env"
}

if [ $# -lt 1 ]; then
    exit_with_usage
fi

sub1=$1

# general files and directories
lampp_bin=/opt/lampp/lampp
lampp_conf=/opt/lampp/etc/extra/httpd-xampp.conf
lampp_flag_https=/opt/lampp/etc/xampp/startssl


if [ $sub1 == "release" ]; then
    grep major /etc/tc/issue | awk -F= '{print $2}' | xargs

elif [ $sub1 == "revision" ]; then
    grep minor /etc/tc/issue | awk -F= '{print $2}' | xargs

elif [ $sub1 == "util.random" ]; then
    LEN=16
    if [ $# -gt 1 ]; then
        LEN=$2
    fi
    cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w $LEN | head -n 1

elif [ $sub1 == "db.localhost" ]; then
    if [ $# -lt 2 ]; then
        exit_with_usage
    fi
    if [ $2 == "true" ]; then
        sed -i "s/|publicdatabase|/|phpmyadmin|/" "$lampp_conf"
    else
        sed -i "s/|phpmyadmin|/|publicdatabase|/" "$lampp_conf"
    fi
    "$lampp_bin" reloadapache

elif [ $sub1 == "fw.upgrade" ]; then
    if [ $# -lt 3 ]; then
        exit_with_usage
    fi
    output="$2.tar"
    if [ -f "$output" ]; then
        rm --force --verbose "$output"
    fi
    gpg2 --decrypt --output "$output" --no-tty --batch --quiet --passphrase "$3" "$2"
    eno=$?
    if [ $eno -ne 0 ]; then
        echo "Bad password, import failed"
        exit $eno
    fi
    tar vxmf "$output" -C /home/tc/tftpboot
    chmod +x /home/tc/tftpboot/rootfs.cpio.gz

elif [ $sub1 == "store" ]; then
# STORE file support
#   tc-config store <store> <key> <value>
    del_cmd="/^$3.*/d"
    add_cmd="$ a $3=$4"
    sed -i "$del_cmd;" $2
    sed -i "$add_cmd" $2

elif [ $sub1 == "https.enable" ]; then
# enable HTTPS webconsole site
    if [ $# -lt 2 ]; then
        exit_with_usage
    fi
    lampp_index=/opt/lampp/htdocs/index.php
    lampp_httpd=/opt/lampp/etc/httpd.conf
    if [ $2 == "true" ]; then
        touch "$lampp_flag_https"
        sed -i "s/http:/https:/g" "$lampp_index"
        sed -i "/^Include.*site-tc.conf$/d" "$lampp_httpd"
    else
        if [ -f "$lampp_flag_https" ]; then
            rm -f "$lampp_flag_https"
        fi
        sed -i "s/https:/http:/g" "$lampp_index"        
        sed -i "/^Include.*site-tc.conf$/d" "$lampp_httpd"
        sed -i "$ a Include etc/site-tc.conf" "$lampp_httpd"
    fi
    "$lampp_bin" restartapache

elif [ $sub1 == "nic.init" ]; then
    select_nic
    exit $?
fi
