#!/bin/bash

check_ipaddr()
{
    echo $1|grep "^[0-9]\{1,3\}\.\([0-9]\{1,3\}\.\)\{2\}[0-9]\{1,3\}$" > /dev/null;
    if [ $? -ne 0 ]
    then
        echo "Bad IP address" 
        return 1
    fi
    ipaddr=$1
    a=`echo $ipaddr|awk -F . '{print $1}'`
    b=`echo $ipaddr|awk -F . '{print $2}'`
    c=`echo $ipaddr|awk -F . '{print $3}'`
    d=`echo $ipaddr|awk -F . '{print $4}'`
    for num in $a $b $c $d
    do
        if [ $num -gt 255 ] || [ $num -lt 0 ] 
        then
            echo "Bad IP address" 
            return 1
        fi
   done

   return 0
}
do_setup_admin_password() {
    dialog --title "Setup pi password" --msgbox "You will be asked to enter a new password." 5 60
    passwd pi
    RET=$?
    if [ $RET -eq 0 ]; then
        dialog --title "Setup pi password" --msgbox "Password has been changed succesfully." 5 60
        do_main_menu
    fi
}

do_setup_concentrator_shield() {
#    FUN=$(dialog --title "Setup LoRa concentrator shield" --menu "Select shield:" 15 60 4 \
#        1 "RAK      - RAK831 with GPS module" \
#        2 "RAK      - RAK831 without GPS module" \
#        3>&1 1>&2 2>&3)
#    RET=$?
#    if [ $RET -eq 1 ]; then
#        do_main_menu
#    elif [ $RET -eq 0 ]; then
#        case "$FUN" in
#            1) do_set_concentrator_reset_pin 17 && do_setup_channel_plan "rak831" ".gps";;
             #do_set_concentrator_reset_pin 17 && do_setup_channel_plan "rak831" ""
             do_setup_channel_plan "rak831" ""
#x        esac
#    fi
}

do_setup_ttn_channel_plan() {
    # $1: concentrator type
    # $2 ttn or loraserver
    FUN=$(dialog --title "TTN Channel-plan configuration" --menu "Select the Channel-plan:" 18 60 12 \
        1 "AS_923" \
        2 "AU_915_928" \
        3 "CN_470_510" \
        4 "EU_863_870" \
        5 "IN_865_867" \
        6 "KR_920_923" \
        7 "RU_864_870" \
        8 "US_902_928" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        do_main_menu
    elif [ $RET -eq 0 ]; then
        case "$FUN" in
            1) do_copy_global_conf $1 "as_923" $2;;
            2) do_copy_global_conf $1 "au_915_928" $2;;
            3) do_copy_global_conf $1 "cn_470_510" $2;;
            4) do_copy_global_conf $1 "eu_863_870" $2;;
            5) do_copy_global_conf $1 "in_865_867" $2;;
            6) do_copy_global_conf $1 "kr_920_923" $2;;
            7) do_copy_global_conf $1 "ru_864_870" $2;;
            8) do_copy_global_conf $1 "us_902_928" $2;;
        esac
    fi
}

set_lora_server_ip()
{
    rm /tmp/gate_server_ip -rf
    mkfifo /tmp/gate_server_ip
    dialog --title "LoRaServer IP" --nocancel --inputbox "SERVER_IP:" 10 40 "127.0.0.1" 2> /tmp/gate_server_ip & 
    RET=$?
	
    if [ $RET -eq 1 ]; then
        do_main_menu
    elif [ $RET -eq 0 ]; then
        gate_server_ip="$( cat /tmp/gate_server_ip  )" 
        sed -i "s/^.*server_address.*$/\t\"server_address\": \"$gate_server_ip\",/" /opt/ttn-gateway/packet_forwarder/lora_pkt_fwd/global_conf.json
        rm /tmp/gate_server_ip
        do_set_gateway_id
    fi
}

do_setup_lora_server_channel_plan() {
    # $1: concentrator type
    # $2: config suffix, eg ".gps"
    FUN=$(dialog --title "LoRaServer Channel-plan configuration" --menu "Server the Channel-plan:" 18 60 12 \
        1 "AS_923" \
        2 "AU_915_928" \
        3 "CN_470_510" \
        4 "EU_433" \
        5 "EU_863_870" \
        6 "IN_865_867" \
        7 "KR_920_923" \
        8 "RU_864_870" \
        9 "US_902_928" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        do_main_menu
    elif [ $RET -eq 0 ]; then
        case "$FUN" in
            1) do_copy_global_conf $1 "as_923" $2;;
            2) do_copy_global_conf $1 "au_915_928" $2;;
            3) do_copy_global_conf $1 "cn_470_510" $2;;
            4) do_copy_global_conf $1 "eu_433" $2;;
            5) do_copy_global_conf $1 "eu_863_870" $2;;
            6) do_copy_global_conf $1 "in_865_867" $2;;
            7) do_copy_global_conf $1 "kr_920_923" $2;;
            8) do_copy_global_conf $1 "ru_864_870" $2;;
            9) do_copy_global_conf $1 "us_902_928" $2;;
        esac
    fi
}
 
do_setup_channel_plan() {
    # $1: concentrator type
    # $2: config suffix, eg ".gps"
    FUN=$(dialog --title "Server-plan configuration" --menu "Select the Server-plan:" 15 60 3 \
        1 "Server is TTN" \
        2 "Server is LoRaServer" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        do_main_menu
    elif [ $RET -eq 0 ]; then
        case "$FUN" in
            1) do_setup_ttn_channel_plan $1 "ttn";;
            2) do_setup_lora_server_channel_plan $1 "lora_server";;
        esac
    fi
}

do_prompt_concentrator_reset_pin() {
    PIN=$(dialog --inputbox "To which pin is the concentrator reset connected: " 8 60 \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        do_setup_concentrator_shield
    elif [ $RET -eq 0 ]; then
        do_set_concentrator_reset_pin $PIN
    fi
}

do_set_concentrator_reset_pin() {
#    sed -i "s/^\(CONCENTRATOR_RESET_PIN=\).*$/\1$1/" /etc/default/lora-packet-forwarder
    echo "aaa"
}

restart_loraserver_service()
{
    systemctl restart loraserver
    systemctl restart lora-app-server.service
}

do_copy_global_conf() {
    cp /etc/lora_conf/serial/global_conf.$2.json /opt/ttn-gateway/packet_forwarder/lora_pkt_fwd/global_conf.json

    cp /etc/loraserver/loraserver.$2.toml /etc/loraserver/loraserver.toml

    if [ $3 != "ttn" ]; then
        set_lora_server_ip
    else
        dialog --title "Server-plan configuration" --msgbox "Server-plan configuration has been copied." 5 60
        do_set_gateway_id
    fi
}

do_set_gateway_id() {
#    /opt/lora-packet-forwarder/update_gwid.sh /etc/lora-packet-forwarder/global_conf.json
#    RET=$?
#    if [ $RET -eq 0 ]; then
#        dialog --title "Set Gateway ID" --msgbox "The Gateway ID has been set." 5 60
        do_restart_packet_forwarder
#    fi
        restart_loraserver_service
}

do_setup_concentrator_shield_rak_rak831() {
    FUN=$(dialog --title "RAK - Rak Gateway configuration" --menu "Select configuration:" 15 60 4 \
        eu868.json     "EU868 band" \
        eu868.json.gps "EU868 band + GPS" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        do_setup_concentrator_shield
    elif [ $RET -eq 0 ]; then
        return 1
    fi
}


do_restart_packet_forwarder() {
    systemctl restart ttn-gateway
    RET=$?
    if [ $RET -eq 0 ]; then
        dialog --title "Restart packet-forwarder" --msgbox "The packet-forwarder has been restarted." 5 60
        do_main_menu
    fi

}

enable_ap_mode()
{
    systemctl enable create_ap

    if [ -f "/sbin/wpa_supplicant" ]; then
        mv /sbin/wpa_supplicant /sbin/wpa_supplicant_bak
    fi
    echo "ap" > /usr/local/rak/gateway-config-info/wifi_mode
    dialog --title "Enable AP Mode" --msgbox "The AP mode will active after the operating system reboot." 5 70
}

enable_wifi_mode()
{
    systemctl disable create_ap

    if [ -f "/sbin/wpa_supplicant_bak" ]; then
        mv /sbin/wpa_supplicant_bak /sbin/wpa_supplicant
    fi

    echo "wifi" > /usr/local/rak/gateway-config-info/wifi_mode
    dialog --title "Enable Wifi Mode" --msgbox "The Wifi mode will active after the operating system reboot." 5 70
}

modify_ssid_for_ap()
{
    echo "aaa"
    rm /tmp/rak_ssid -rf
    mkfifo /tmp/rak_ssid
    rm /tmp/rak_ap_pwd -rf
    mkfifo /tmp/rak_ap_pwd
    
    # get old ssid
    if [ -f /usr/local/rak/gateway-config-info/ap_ssid ]; then
        old_ap_ssid="$( cat /usr/local/rak/gateway-config-info/ap_ssid  )"
    else
        echo "Rakwireless_XXXX" > /usr/local/rak/gateway-config-info/ap_ssid
    fi
    old_ap_ssid="$( cat /usr/local/rak/gateway-config-info/ap_ssid  )"
    
    # get old pwd
    if [ -f /usr/local/rak/gateway-config-info/ap_pwd ]; then
        old_ap_pwd="$( cat /usr/local/rak/gateway-config-info/ap_pwd  )"
    else
        echo "rakwireless" > /usr/local/rak/gateway-config-info/ap_pwd
    fi
    old_ap_pwd="$( cat /usr/local/rak/gateway-config-info/ap_pwd  )"
    
    # dialog ip
    dialog --title "AP SSID" --nocancel --inputbox "SSID:" 10 40 "$old_ap_ssid" 2> /tmp/rak_ssid & 
    RET=$?

    if [ $RET -eq 1 ]; then
        clear
    elif [ $RET -eq 0 ]; then
        new_ap_ssid="$( cat /tmp/rak_ssid  )"
        rm /tmp/rak_ssid
        ssid_len=${#new_ap_ssid}
    fi
    
    #dialog pwd
    dialog --title "AP Password" --nocancel --inputbox "Password:" 10 40 "$old_ap_pwd"  2> /tmp/rak_ap_pwd &
    if [ $RET -ne 0 ]; then
    	clear
    else
        new_ap_pwd="$( cat /tmp/rak_ap_pwd  )"
        pwd_len=${#new_ap_pwd}
        rm /tmp/rak_ap_pwd
        
    fi
    
    if [ $ssid_len -eq 0 ] || [ $pwd_len -eq 0 ] || [ $pwd_len -lt 8 ] ||[ $pwd_len -gt 63 ]; then
        if [ $ssid_len -eq 0 ]; then
            dialog --title "Configure AP SSID" --msgbox "SSID cannot be empty." 5 28
        elif [ $pwd_len -eq 0 ] || [ $pwd_len -lt 8 ] ||[ $pwd_len -gt 63 ]; then
            dialog --title "Configure AP Password" --msgbox "Invalid passphrase length ${pwd_len} (expected: 8..63)." 5 52
        else
            clear
        fi
    else
        sed -i "26c SSID=$new_ap_ssid" /usr/local/rak/ap/create_ap.conf
        sed -i "27c PASSPHRASE=$new_ap_pwd" /usr/local/rak/ap/create_ap.conf
    
        echo $new_ap_ssid > /usr/local/rak/gateway-config-info/ap_ssid
        echo $new_ap_pwd > /usr/local/rak/gateway-config-info/ap_pwd

        dialog --title "Configute AP info" --msgbox "Modify AP info success" 5 42
	fi
    
#    do_main_menu
}

add_new_ssid()
{
    rm /tmp/wifi_ssid -rf
    mkfifo /tmp/wifi_ssid
    rm /tmp/wifi_pwd -rf
    mkfifo /tmp/wifi_pwd
    dialog --title "Configure WIFI" --nocancel --inputbox "SSID:" 10 40  2> /tmp/wifi_ssid &
    if [ $RET -ne 0 ]; then
    	do_main_menu
    fi
    dialog --title "Configure WIFI" --nocancel --inputbox "Password:" 10 40  2> /tmp/wifi_pwd &
    if [ $RET -ne 0 ]; then
    	do_main_menu
    fi
    
    linenum=`sed -n '/update_config/=' /etc/wpa_supplicant/wpa_supplicant.conf`
    let linenum=linenum+1
    
    wifi_ssid="$( cat /tmp/wifi_ssid  )"
    wifi_pwd="$( cat /tmp/wifi_pwd  )"
    ssid_len=${#wifi_ssid}
    pwd_len=${#wifi_pwd}
    
    if [ $ssid_len -eq 0 ]; then
    	dialog --title "Configure WIFI" --msgbox "SSID cannot be empty." 5 28
    	do_main_menu
    fi
    
    if [ $pwd_len -eq 0 ] || [ $pwd_len -lt 8 ] ||[ $pwd_len -gt 63 ]; then
    	dialog --title "Configure WIFI" --msgbox "Invalid passphrase length ${pwd_len} (expected: 8..63)." 5 52
 #   	do_main_menu
    else
        sed -i "${linenum}inetwork={\nssid=\"${wifi_ssid}\"\nkey_mgmt=WPA-PSK\npsk=\"${wifi_pwd}\"\n}" /etc/wpa_supplicant/wpa_supplicant.conf
    fi
}

do_configure_wifi() {
    # get old ip
    if [ -f /usr/local/rak/gateway-config-info/wifi_mode ]; then
        wifi_mode="$( cat /usr/local/rak/gateway-config-info/wifi_mode  )"
    else
        echo "ap" > /usr/local/rak/gateway-config-info/wifi_mode
    fi
    
    default_item=2
    if [ $wifi_mode != "ap" ]; then
		let default_item=2
	fi

    FUN=$(dialog --title "Configure wifi" --cancel-label "Cancel" --default-item $default_item --menu "Configuration options:" 12 60 20 \
    	1 "Enable AP Mode/Disable Client Mode"	\
        2 "Enable Client Mode/Disable AP Mode" \
        3 "Modify SSID and pwd for AP Mode"	\
        4 "Add New SSID for Client" \
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 0 ]; then
        case "$FUN" in
            1) enable_ap_mode;;
            2) enable_wifi_mode;;
            3) modify_ssid_for_ap;;
            4) add_new_ssid;;
        esac
    fi

    do_main_menu
}

configure_lan() {
    rm /tmp/eth0_ip -rf
    mkfifo /tmp/eth0_ip

    rm /tmp/eth0_gw -rf
    mkfifo /tmp/eth0_gw

    # get old ip
    if [ -f /usr/local/rak/gateway-config-info/eth0_ip ]; then
        old_eth0_ip="$( cat /usr/local/rak/gateway-config-info/eth0_ip  )"
    else
        echo "192.168.10.10" > /usr/local/rak/gateway-config-info/eth0_ip
    fi
    old_eth0_ip="$( cat /usr/local/rak/gateway-config-info/eth0_ip  )"

    # dialog ip
    dialog --title "Set eth0 IP" --nocancel --inputbox "IP:" 10 40 "$old_eth0_ip" 2> /tmp/eth0_ip & 
    RET=$?

    if [ $RET -eq 1 ]; then
        echo "do_main_menu"
    elif [ $RET -eq 0 ]; then
        new_eth0_ip="$( cat /tmp/eth0_ip  )" 
        check_ipaddr $new_eth0_ip
        RET_IP=$?
        rm /tmp/eth0_ip
    fi

    # get old gw
    if [ -f /usr/local/rak/gateway-config-info/eth0_gw ]; then
        old_eth0_gw="$( cat /usr/local/rak/gateway-config-info/eth0_gw  )"
    else
        echo "192.168.10.1" > /usr/local/rak/gateway-config-info/eth0_gw
    fi
    old_eth0_gw="$( cat /usr/local/rak/gateway-config-info/eth0_gw  )"

    # dialog eth0 gw
    dialog --title "Set eth0 gateway IP" --nocancel --inputbox "Gateway IP:" 10 40 "$old_eth0_gw" 2> /tmp/eth0_gw & 
    RET=$?

    if [ $RET -eq 1 ]; then
        echo "do_main_menu"
    elif [ $RET -eq 0 ]; then
        new_eth0_gw="$( cat /tmp/eth0_gw  )" 
        check_ipaddr $new_eth0_gw
        RET_GW=$?
        rm /tmp/eth0_gw
    fi
    
    if [ $RET_IP -eq 1 ]; then
        dialog --title "Configure LAN" --msgbox "Invalid IP address." 5 50
    elif [ $RET_GW -eq 1 ]; then
        dialog --title "Configure LAN" --msgbox "Invalid Gateway IP address." 5 50
    else
        echo $new_eth0_ip > /usr/local/rak/gateway-config-info/eth0_ip
        echo $new_eth0_gw > /usr/local/rak/gateway-config-info/eth0_gw

        linenum=`sed -n '/RAK_eth0_IP/=' /etc/dhcpcd.conf`
        let line_ip=linenum+2
        let line_gw=linenum+3

        sed -i "${line_ip}cstatic ip_address=${new_eth0_ip}" /etc/dhcpcd.conf
        sed -i "${line_gw}cstatic routers=${new_eth0_gw}" /etc/dhcpcd.conf
        dialog --title "Configure LAN" --msgbox "Configure LAN success." 5 50
    fi

    do_main_menu
}


do_main_menu() {
    GATEWAY_EUI_NIC="eth0"
    if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
        GATEWAY_EUI_NIC="wlan0"
    fi

    if [[ `grep "$GATEWAY_EUI_NIC" /proc/net/dev` == "" ]]; then
       echo "ERROR: No network interface found. Cannot set gateway ID."
    fi
    GATEWAY_EUI=$(ip link show $GATEWAY_EUI_NIC | awk '/ether/ {print $2}' | awk -F\: '{print $1$2$3"FFFE"$4$5$6}')
    GATEWAY_EUI=${GATEWAY_EUI^^}

    VER=`cat /usr/bin/gateway-version.txt`
    FUN=$(dialog --title "Rak Gateway (Gateway ID:$GATEWAY_EUI  Version: $VER)" --cancel-label "Quit" --menu "Configuration options:" 16 80 20 \
        1 "Set pi password" \
        2 "Setup RAK Gateway LoRa concentrator" \
        3 "Edit packet-forwarder config" \
        4 "Restart packet-forwarder" \
        5 "Configure WIFI" \
        6 "Configure LAN"	\
        3>&1 1>&2 2>&3)
    RET=$?
    if [ $RET -eq 1 ]; then
        clear
        return 0
    elif [ $RET -eq 0 ]; then
        case "$FUN" in
            1) do_setup_admin_password;;
            2) do_setup_concentrator_shield;;
            3) nano /opt/ttn-gateway/packet_forwarder/lora_pkt_fwd/global_conf.json && do_main_menu;;
            4) do_restart_packet_forwarder;;
            5) do_configure_wifi;;
            6) configure_lan;;
        esac
    fi
}


if [ $(id -u) -ne 0 ]; then
  printf "Script must be run as root. Try 'sudo gateway-config'\n"
  exit 1
fi

do_main_menu
