#!/bin/bash -x
#
# Install XS custom networking configuration
#
# Parse options
opt_upgrade_only=
if ! options=$(getopt -o u -l upgrade-only -- "$@"); then
	exit 1
fi

eval set -- $options
while [ $# -gt 0 ]; do
	case $1 in
		-u|--upgrade-only) opt_upgrade_only=1 ;;
		(--) shift; break;;
		(-*) echo "$0: error - unrecognized option $1" >&2; exit 1;;
		(*) break;;
	esac
	shift
done

# The user can request that we only perform setup steps if setup had been
# previously performed by an admin. This is useful for automated upgrades
# (e.g. RPM post scripts).
if [ -n "$opt_upgrade_only" ]; then
	echo "$0: doing nothing, XS standard networking is not active."
	exit 0
fi

# Determine the number of interfaces
num_ifaces=`ls /sys/class/net | wc | gawk '{print $1}'`

# Is this an XO
is_xo=`[ -f /proc/device-tree/mfg-data/MN ] && echo 1 || echo 0`
echo $is_xo
CFG_TEMPLATES="sysconfig/dhcpd sysconfig/iptables-config sysconfig/ip6tables-config"
CONFIG_LIST="/etc/hosts"
UPLINK=`find /etc/NetworkManager/system-connections -maxdepth 1 -type f -name "*" -not -name static -not -name "Sugar *"`

#new code to use NetworkManager to set up devices
function write_nm_connection(){
    # receives connection_filename, hwaddr,  device_name as parameters
    cp "/usr/share/xs-config/cfg/etc/NetworkManager/system-connections/$1" \
        "/etc/NetworkManager/system-connections/$1"
    #removed -i to try to get rid of sed intermediate files 9/24/2012
    sed -i  "s/\@\@MAC\@\@/$2/" "/etc/NetworkManager/system-connections/$1"
    sed -i  "s/\@\@DEVICE\@\@/$3/" "/etc/NetworkManager/system-connections/$1"
    chmod 600 "/etc/NetworkManager/system-connections/$1" 
}

#========================================================
# all xo's have a wifi and a lo interface
if [[ $is_xo = "1" ]]; then
    num_ifaces=$(( $num_ifaces - 1 ))
    if [[ $num_ifaces  = 1 ]]; then
        ipaddr=ifconfig eth0| awk -F ' *|:' '/inet addr/{print $4}'
    fi
    if [[ $num_ifaces  > 1 ]]; then
        mac=`ifconfig | gawk 'BEGIN { eth="";} (/^.*: flags=/) {eth = $1;} 
            /^ *ether / { if (eth == "eth1:") print( $2);}'`
        write_nm_connection "static" "$mac" "eth1"
        ipaddr=172.18.96.1
    fi
    # if we have 2 ethernet dongles, lo, )
    if [[ $num_ifaces = 3 ]] ; then
        mac=`ifconfig | gawk 'BEGIN { eth="";} (/^.*: flags=/) {eth = $1;} 
            /^ *ether / { if (eth == "eth2:") print( $2);}'`
        write_nm_connection "dhcp" "$mac" "eth2"
        echo "eth2" > /etc/sysconfig/xs_wan_device
    else
        echo "eth0" > /etc/sysconfig/xs_wan_device
	# based on observed created files
        echo "ignore-auto-dns=true" >> $UPLINK
    fi
else
    mac=`ifconfig | gawk 'BEGIN { eth="";} (/^.*: flags=/) {eth = $1;} 
        /^ *ether / { if (eth == "eth0:") print( $2);}'`
    write_nm_connection "static" "$mac" "eth0"
    ipaddr=172.18.96.1
    mac=`ifconfig | gawk 'BEGIN { eth="";} (/^.*: flags=/) {eth = $1;} 
        /^ *ether / { if (eth == "eth1:") print( $2);}'`
    write_nm_connection "dhcp" "$mac" "eth1"
    echo "eth1" > /etc/sysconfig/xs_wan_device
fi

pushd /etc
for i in $CFG_TEMPLATES; do
	cp -p $i.in $i
	#etckeeper commit -m "Made from $i.in"
done

short_host=`hostname -s`
new_name=`hostname -f`

#systemctl stop NetworkManager.service

#  This is the suffix which original versions of modified files will have
BACKUP_SUFFIX=old

for config in $CONFIG_LIST;
do
    if [ -e $config.in ]; then
	if [ -e $config ]; then
	    cp $config $config.$BACKUP_SUFFIX ;
	fi
	cp $config.in $config.tmp ;
	sed -i -e s/@@BASEDNSNAME2@@/$new_name/ $config.tmp ;
	sed -i -e s/@@BASEDNSNAME3@@/$short_host/ $config.tmp ;
	sed -i -e s/@@IPADDR@@/$ipaddr/ $config.tmp ;
	cat $config.tmp > $config ;
	rm $config.tmp ;
	#etckeeper commit -m "set /etc/hosts to $ipaddr $new_name $short_host"
    else
	echo WARNING: Skipped $config - template file is missing!
    fi
done
popd
echo "XS standard networking configured"
# systemctl start NetworkManager.service

sleep 5

