#!/bin/bash

# Copyright (C) 2010 Oregon State University
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# This is an example script that configures your network settings after
# installation. By default it sets it up to use dhcp.

set -e

. common.sh

debug set -x

FQDN="${INSTANCE_NAME}"
SHORT_NAME="$(echo ${INSTANCE_NAME} | cut -d . -f 1)"
STATIC=""

if [ -z "${TARGET}" -o ! -d "${TARGET}" ] ; then
    log_error "Missing target directory"
    exit 1
fi

if [ -z "${NIC_COUNT}" ] ; then
    log_error "Missing NIC_COUNT"
    exit 1
fi

if [ -f "${NETWORKS_DIR}/instances/${FQDN}" ] ; then
    STATIC="yes"
    source ${NETWORKS_DIR}/instances/${FQDN}
    if [ -f "${NETWORKS_DIR}/subnets/${SUBNET}" ] ; then
        source ${NETWORKS_DIR}/subnets/${SUBNET}
    else
        echo "No subnet file for subnet ${SUBNET}!"
        exit 1
    fi
fi

resolvconf() {
    nl=$'\n'
    if [ -n "${DNS_SERVERS}" ] && [ -n "${DNS_SEARCH}" ] && [ -n "${DNS_DOMAIN}" ]; then
        for server in $DNS_SERVERS ; do
            nameservers="nameserver ${server}${nl}${nameservers}"
        done

        cat > ${TARGET}/etc/resolv.conf << EOF
${nameservers}
domain ${DNS_DOMAIN}
search ${DNS_SEARCH}
EOF
    fi
}

# Functions
debian_setup() {
    if [ ! -d "${TARGET}/etc/network" ] ; then
        log_error "Missing target network directory"
        exit 1
    fi

    if [ -z "${STATIC}" ] ; then
        cat > ${TARGET}/etc/network/interfaces << EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

EOF
    else
        cat > ${TARGET}/etc/network/interfaces << EOF
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address ${ADDRESS}
    netmask ${NETMASK}
    gateway ${GATEWAY}
EOF
        if [ -n "${DNS_SERVERS}" ] && [ -n "${DNS_SEARCH}" ] && [ -n "${DNS_DOMAIN}" ]; then
            cat >> ${TARGET}/etc/network/interfaces << EOF
    dns-nameservers ${DNS_SERVERS}
    dns-domain ${DNS_DOMAIN}
    dns-search ${DNS_SEARCH}

EOF
        fi
    fi

    if [ -n "${FQDN}" ] ; then
        echo "${SHORT_NAME}" > ${TARGET}/etc/hostname
    fi
}

redhat_setup() {
    if [ ! -d "${TARGET}/etc/sysconfig/network-scripts" ] ; then
        log_error "Missing target network directory"
        exit 1
    fi
    if [ -z "${STATIC}" ] ; then
        cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
EOF
    else
        cat > ${TARGET}/etc/sysconfig/network-scripts/ifcfg-eth0 << EOF
DEVICE=eth0
BOOTPROTO=static
IPADDR=${ADDRESS}
NETMASK=${NETMASK}
ONBOOT=yes
EOF
    fi

    if [ -n "${FQDN}" ] ; then
        cat > ${TARGET}/etc/sysconfig/network << EOF
NETWORKING=yes
HOSTNAME=${FQDN}
GATEWAY=${GATEWAY}
EOF
    else
        cat > ${TARGET}/etc/sysconfig/network << EOF
NETWORKING=yes
GATEWAY=${GATEWAY}
EOF
    fi

    resolvconf
}

gentoo_setup() {
    if [ ! -f "${TARGET}/etc/conf.d/net" ] ; then
        log_error "Missing target network file"
        exit 1
    fi
    if [ -z "${STATIC}" ] ; then
        cat > ${TARGET}/etc/conf.d/net << EOF
config_eth0=( "dhcp" )
EOF
    else
        cat > ${TARGET}/etc/conf.d/net << EOF
config_eth0=( "${ADDRESS} netmask ${NETMASK}" )
routes_eth0=( "default gw ${GATEWAY}" )
EOF
    fi

    resolvconf

    chroot ${TARGET} ln -sf /etc/init.d/net.lo /etc/init.d/net.eth0
    chroot ${TARGET} rc-update add net.eth0 default

    if [ -n "${FQDN}" ] ; then
        # baselayout-2.x
        if [ -d "${TARGET}/usr/share/openrc/" ] ; then
            cat > ${TARGET}/etc/conf.d/hostname << EOF
hostname="${SHORT_NAME}"
EOF
        else
            cat > ${TARGET}/etc/conf.d/hostname << EOF
HOSTNAME="${SHORT_NAME}"
EOF
        fi
    fi
}

suse_setup() {
    if [ ! -d ${TARGET}/etc/sysconfig/network ] ; then
        log_error "Missing target network directory"
        exit 1
    fi
    cat > ${TARGET}/etc/sysconfig/network/ifcfg-eth0 << EOF
BOOTPROTO='dhcp4'
STARTMODE='auto'
NAME='Ethernet Card 0'
EOF
    if [ -n "${FQDN}" ] ; then
        echo "${FQDN}" > ${TARGET}/etc/HOSTNAME
    fi

    resolvconf
}

# Main
get_os_type $TARGET

if [ "${NIC_COUNT}" -gt 0 -a -n "${OS_TYPE}" ] ; then
    ${OS_TYPE}_setup
else
    log_error "Unsupported OS_TYPE"
fi


exit 0
