#!/bin/bash

# Global variables
unset conf
unset network
unset type
interface="wlp4s0"

# Print helptext
function usage() {
    echo Usage: $0 '[-i interface] -f conf_name'
    echo "       $0 -a        # Connect to any known secured network"
    echo "       $0 -l        # List networks and arp"
    echo "       $0 -o SSID   # Connect to open network "
    echo "       $0 -u        # Bring up interface" 
    echo "       $0 -d        # Stop the interface"
    echo "       $0 -I        # Info "
    echo "       $0 -h        # Usage"
    echo 
    echo Add -v to increase verbosity. 
    echo "Add -t TYPE to override type guessing, where type is eth or wifi"
    echo Default interface is "${interface}"
}

# Try to guess if we're ether or wifi
function guessType() { 
    if [ "$(echo "${interface}" | egrep -c '^enp|^eth|^eno')" -ne 1 ]; then
        echo "wifi"
    else
        echo "ether"
    fi
}

# Clean up running processes 
function cleanUp() { 
    killall wpa_supplicant dhcpcd 2>&1
    ip link set "$interface" down
}

# Connect to a given open network
# param network: an open ESSID
function connectOpen() { 
    network="$1"
    cleanUp
    ip link set "$interface" up
    if [ "$type" == "wifi" ]; then
        iw dev "$interface" connect "$network"
    fi
}

# Connect to any known secured access point
function connectAny() { 
    cleanUp
    ip link set "$interface" up
    if [ "$type" == "wifi" ]; then
        for i in `iwlist "$interface" scanning | grep ESSID | cut -f 2 -d \" | tr '[:upper:]' '[:lower:]'`; do
            if [ -f /etc/wpa_supplicant/"$i".conf ]; then
                /root/bin/wifi "$i" "$interface"
                if [ $? -eq 0 ]; then
                    exit 0;
                fi
            fi
        done
    fi
}

# Connect using the WPA Conf file saved 
# param conf: the conf file
function connect() { 
    conf="$1"
    cleanUp
    ip link set "${interface}" up
    if [ "$type" == wifi ]; then 
        wpa_supplicant -i "$interface" -c "$conf" -B
    fi
}

# Get the information on current interfaces
function netInfo() { 
    ip addr list
    ip route list
    iwlist scanning
}


### MAIN FUNCTION ### 
if [ `echo "$0" | egrep -c '(^|/)netcli$'` -eq 1 ]; then
    type=`guessType`
    while getopts 'adf:hIi:lo:t:uv' OPTION 2>/dev/null; do
        case "${OPTION}" in
            a) connectAny ;;
            f) connect "/etc/wpa_supplicant/${OPTARG}.conf" ;;
            d) cleanUp; exit 0 ;; 
            h) usage; exit 0 ;;
            i) interface="${OPTARG}"; type=`guessType` ;;
            I) netInfo; exit 0; ;;
            l) if [ "$type" == "wifi" ]; then echo SSID Broadcasts:; iwlist "${interface}" scanning | egrep Encryption\|ESSID | sed 's/ *//' | sed 's/ESSID://' ; fi; echo; echo ARP list:; arp -a; exit $? ;;
            o) connectOpen "${OPTARG}" ;;
            t) if [ "$OPTARG" != "wifi" ] && [ "$OPTARG" != "eth" ]; then usage; exit 1; fi; type="${OPTARG}" ;;
            u) if ! [ "$(ip link list "${interface}" | grep -m 1 -c \ UP\ )" -ne 1 ]; then ip link set "${interface}" up; fi ;;
            v) set -x ;;
            *) usage; exit 1 ;; 
        esac
    done

    # Attempt DHCP Lease -- if this fails, static routing will need to be added TODO

    sleep 3
    dhcpcd "$interface"
    exit $?
fi