#!/bin/bash
# Initial script to convert existing OS installation into an OLPC XS.
# Configures fundamental XS items.
# Usage: xs-setup [hostname]

set -e

DESTDIR=/
CFGDIR=/usr/share/xs-config/cfg
MARKER=/.olpcxs-configured
ISXO=`[ -f /proc/device-tree/mfg-data/MN ] && echo 1 || echo 0`
DOMAIN=local
# Determine the number of interfaces
num_ifaces=`ls /sys/class/net | wc | gawk '{print $1}'`

# 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, upgrade-only on cmdline."
	exit 0
fi

# for public demo, disable any functions that would change the machine state
if [ -f /etc/sysconfig/xs-disable-config ]; then
    echo ""
    echo "======================================================"
    echo
    echo "This public demonstration server cannot be configured remotely"
    echo
    echo "======================================================"
    exit 0
fi

if [ -d /var/run/powerd-inhibit-dim ]; then
    touch /var/run/powerd-inhibit-dim/$$
fi

# seed low-level network conf and domain
if [ x$1 != x ]; then
    xs-domain-config $1
else
    if [ -f /etc/sysconfig/xs_domain_name ]; then
        DOMAIN_SET=`cat /etc/sysconfig/xs_domain_name`
	xs-domain-config $DOMAIN_SET
    else
        xs-domain-config $DOMAIN
    fi
fi

#systemd change
echo '-y' > /fsckoptions
# enable no-fsck-questions
#chkconfig --add no-fsck-questions

# beginning of interface with setup functions
#
# for testing, let's load all the available options
source /usr/bin/xs-setup-functions

# prep the XO
do_once
get_usb_repo

#do-first function has had its name changed to startup
startup

sendmessage=
if [[ $num_ifaces < 4 && $ISXO = 1 ]]; then
    # Try to figure out which interface is connected to a gateway
    gw_iface=`route -n | awk '{if($4=="UG")print $8}'`
    if [ "gw_iface" == "eth1" ]; then
        set +x
        echo
        echo  "====================================================================="
        echo  "You are using a USB network adapter as a connection to the internet"
        sendmessage="true"
    fi
fi
if [[ $num_ifaces < 3 && $ISXO = 1 ]] || [[ ! -z "$sendmessage" ]]; then
    set +x
    echo
    echo  "====================================================================="
    echo  "No lan adapter was found. If you want the XS to act as a content filter"
    echo  "   and a gateway to the internet, you need to stop now and add another"
    echo  "   network adapter. If you add another adapter, at a later time, you must  "
    echo  "   rerun xs-setup."
    echo  "====================================================================="
    echo
    echo  "   INSERT ETHERNET DONGLE NOW. (this is for local XO connections)."
    echo  " Do you want to continue with setup? (y/N):"
    read resp
    case $resp in
    [Yy] | [Yy][Ee][Ss] )
        sleep 5
    ;;
    *)
        # anythin else
        exit 1
    ;;
    esac
fi

# Determine the number of interfaces again since it may have changed
num_ifaces=`ls /sys/class/net | wc | gawk '{print $1}'`
if [[ $num_ifaces > 2 ]]; then
    named yes
    squid yes
    gateway yes
    dhcpd yes
    xs-setup-network
    touch /.olpcxs-net-configured
fi

if [ -e /etc/xs-setup.conf ]; then
    source /etc/xs-setup.conf
else
    source /usr/share/xs-config/cfg/etc/xs-setup.conf
fi

# xs-runansible

# Finalize install
yum-etckeeper yes
create-usb-repo2
do-last

echo "XS configured; ready to reboot and use."
#echo "please check the configured services below"
#xs-services-check
