#!/bin/bash -x
# initialization specific to Trimslice hardware


# for debugging we need to identify use of uninitialized variables
set -u

FEDORA=`rpm -q fedora-release|gawk 'BEGIN {FS="-";}{print $3}'`
XSARCH=`uname -i`
ISTRIMSLICE=1  # we need to set up AP mode in xs-setup-network
 
# bring in packages unique to trimslice install
yum_cmd="yum -y install "
$yum_cmd  audit avahi dhcpd hostapd xs-config usbmount

# assign an ip address to the wifi hardware
ifconfig wlan0 172.18.96.1

# stop the firewall so that wifi can be internal lan
systemctl stop firewalld.service
systemctl disable firewalld.service

# load dhcpd and attach it to the wifi adapter, 
#+ so that it can be administered headless
sed -i  "s/@@BASEDNSNAME@@/local/" /etc/dhcp/dhcpd-ts.conf.in > \
        /etc/dhcp/dhcpd.conf
sed -i  "s/@@LAN_IF@@/wlan0/" /etc/sysconfig/dhcpd.in > \
        /etc/sysconfig/dhcpd
systemctl enable dhcpd.service
systemctl start dhcpd.service

# load avahi so that the wan ip address can be discovered
cp /etc/avahi/avahi-daemon.conf.in /etc/avahi/avahi-daemon.conf

# Access Point host software
systemctl enable hostapd.service
cp -f /etc/hostapd/hostapd.conf.in /etc/hostapd/hostapd.conf
systemctl start hostapd.service

# accomodate a fixed ip for the wan interface, passed as parm $1
if [ $# -gt 0 ]; then
    if [ $# -ne 3 ]; then
        echo "3 Parameters needed for wan fixed ip: ip, gateway, dns "
        exit 1
    fi
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
NETMASK=255.255.224.0
IPADDR=$1
GATEWAY=$2
DNS1=$3
EOF
else
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=auto
DEVICE=eth0
ONBOOT=yes
EOF
fi

# set up the wlan0 device
cat << EOF > /etc/sysconfig/network-scripts/ifcfg-wlan0
BOOTPROTO=static
DEVICE=wlan0
ONBOOT=yes
NETMASK=255.255.224.0
IPADDR=172.18.96.1
NM_CONTROLLED=yes
EOF

# fix usbmount as /media is on tmpfs use /mnt instead
#  maybe we should move this out of bootstrap and into startup -gh
#  we need -p in order to re-execute this code
mkdir -p /mnt/usb0
mkdir -p /mnt/usb1
mkdir -p /mnt/usb2
mkdir -p /mnt/usb3
mkdir -p /mnt/usb4
mkdir -p /mnt/usb5
mkdir -p /mnt/usb6
mkdir -p /mnt/usb7
sed -i -e 's:media:mnt:g' /etc/usbmount/usbmount.conf

# check for other schoolservers on the local network
tryname="schoolserver"
if [[ `avahi-browse -t _workstation._tcp | grep schoolserver` ]]; then
    x=1
    tryname="schoolserver.$x"
    while [ $x -le 9 ]; do
        if [[ ! `avahi-browse -t _workstation._tcp | grep "$tryname"` ]]; then
            break
        fi
        x=$(( "$x" + 1 ))
        tryname="schoolserver.$x"
    done
else
    echo "no otherschoolservers found"
fi

if [ ! `grep "ifconfig wlan0" /etc/rc.d/rc.local` ]; then
    cat << EOF >> /etc/rc.d/rc.local

# the following lines configure wifi Access Point
ifconfig wlan0 172.18.96.1
systemctl restart dhcpd.service
systemctl restart named.service
systemctl restart hostapd.service

# end of wifi Access Point config

EOF
fi

echo $tryname.local > /etc/hostname
echo "HOSTNAME=$tryname.local" >> /etc/sysconfig/network

# xs-setup not written to protect from uninitialized variables
set +u

# chain to the reqular non trimslice XSCE setup process
source xs-setup
