#!/bin/sh -e

##########################################################################
#   Script description:
#       Configure FreeBSD WIFI interface as a failover for another
#       interface.  This is most commonly used to facilitate plugging
#       a laptop in for better network performance without disrupting
#       any network connections.
#
#   Arguments:
#       None.
#
#   History:
#   Date        Name        Modification
#   2016-01-01  J Bacon     Begin
##########################################################################

##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}

usage()
{
    printf "Usage: $0 [wired-interface wireless-interface]\n"
    printf "e.g. $0 re0 ath0\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################
    
case $(auto-ostype) in
FreeBSD)
    case $# in
    0)
	#ifconfig | grep -A 4 flags
	printf "Network interfaces via pciconf -lv:\n\n"
	pciconf -lv | grep -B 3 -A 1 'class.*network'
	wired=''
	while [ 0$wired = 0 ]; do
	    printf "\nWired interface? "
	    read wired
	done
	
	# FIXME: Can we get this from ifconfig instead of rc.conf?
	wireless_default=$(awk -F '[_=]' '$1 == "wlans" { print $2 }' /etc/rc.conf | head -n 1)
	printf "\nWireless interface (raw, not wlan0)? [$wireless_default] "
	read wireless
	if [ 0"$wireless" = 0 ]; then
	    wireless=$wireless_default
	fi
	
	ifconfig wlan0 destroy || true
	ifconfig wlan0 create wlandev $wireless
	;;
    
    2)
	wired=$1
	wireless=$2
	;;
    
    *)
	usage
	;;
    
    esac
    
    wifi_mac=`ifconfig wlan0 | awk '$1 == "ether" { print $2 }'`
    printf "Using MAC $wifi_mac...\n"
    
    sed -i '' -E "s|^wlans_$wireless.*|# &|" /etc/rc.conf
    sed -i '' -E "s|^ifconfig_$wired.*|# &|" /etc/rc.conf
    sed -i '' -E "s|^ifconfig_wlan.*|# &|" /etc/rc.conf
    sed -i '' -E "s|^ifconfig_lagg.*|# &|" /etc/rc.conf
    sed -i '' -E "s|^create_args_wlan.*|# &|" /etc/rc.conf
    sed -i '' -E "s|^cloned_interfaces.*|# &|" /etc/rc.conf
    
    # Note: Not all wireless interfaces allow changing their MAC, so configure
    # the Ethernet MAC instead.
    # e.g. ath0 would not take a new MAC on my Toshiba Satellite
    cat << EOM >> /etc/rc.conf
# Added by $0
ifconfig_$wired="ether $wifi_mac"
wlans_$wireless="wlan0"
ifconfig_wlan0="WPA"
create_args_wlan0=""
# Set country code here if needed
# create_args_wlan0="country FR"
cloned_interfaces="lagg0"
ifconfig_lagg0="up laggproto failover laggport $wired laggport wlan0 DHCP"
ifconfig_lagg0_ipv6="inet6 accept_rtadv"
# End $0 addition
EOM
    
    # Leave current network config intact until reboot unless there's a
    # way to successfully activate lagg before then
    # kldload if_lagg || true
    auto-append-line if_lagg_load="YES" /boot/loader.conf $0

    printf "Old network configurations have been commented out in rc.conf.\n"
    printf "Reboot to activate the new network configuration.\n"
    pause
    ;;
    
*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
