# $Id: vserver-setup.functions,v 1.19 2006/01/02 23:50:21 ensc Exp $	--*- sh -*--

# Copyright (C) 2003,2004,2005,2006 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# 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; version 2 of the License.
#  
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

SETUP_HOSTNAME=
SETUP_NETDEV=
SETUP_NETMASK=
SETUP_NETPREFIX=
SETUP_NETBCAST=
SETUP_LOCKFILE=
SETUP_CONFDIR=
SETUP_CONTEXT=
SETUP_INITSTYLE=

declare -a SETUP_INTERFACES=()
declare -a SETUP_FLAGS=()

declare -r SETUP_OPTIONS="confdir:,lockfile:,hostname:,netdev:,netmask:,netprefix:,netbcast:,interface:,flags:,context:,initstyle:"
declare -r SETUP_HELPMSG=$"
    --context   ...  the static context of the vserver [default: none; a dynamic
                     context will be assumed]
    --confdir   ...  [default: $__CONFDIR/<name>]
    --lockfile <filename>
		...  [default: $__RUNDIR/<name>]
    --hostname <hostname>
    --netdev   <device>
    --netbcast <broadcast>
    --netmask <netmask>|--netprefix <prefixlen>
                ...  sets the  default netmask  (a.b.c.d quadruple)  or prefixlen
		     (length of the interface)
    --interface [<name-suffix>=][<device>:]<ip>[/<mask|prefixlen>]
                ...  declares an network-interface;  this option can be specified
		     multiple times
    --flags <flags>+
                ...  sets comma-separated list of flags; possible flags are
		     lock:  Prevent the vserver from setting new security context
		     sched: Merge  scheduler priority  of all processes in the
		            vserver so that it acts a like a single
		            one (kernel 2.4 only).
                     nproc: Limit the number of processes in the vserver
		            according to ulimit  (instead of a per user limit,
			    this becomes a per vserver limit)
		     private: No other process can join this security context.
		            Even root
    --initstyle <style>
                ...  configures the initstyle (e.g. minit,sysv,plain)
"

function setup_setOption2
{
    case "$1" in
	(--context)	SETUP_CONTEXT=$2;;
	(--confdir)	SETUP_CONFDIR=$2;;
	(--lockfile)	SETUP_LOCKFILE=$2;;
	(--hostname)	SETUP_HOSTNAME=$2;;
	(--netdev)	SETUP_NETDEV=$2;;
	(--netmask)	SETUP_NETMASK=$2;;
	(--netprefix)	SETUP_NETPREFIX=$2;;
	(--netbcast)	SETUP_NETBCAST=$2;;
	(--interface)	SETUP_INTERFACES=( "${SETUP_INTERFACES[@]}" "$2" );;
	(--initstyle)   SETUP_INITSTYLE=$2;;
	(--flags)	old_IFS=$IFS
			IFS=,
			set -- $2
			SETUP_FLAGS=( "${SETUP_FLAGS[@]}" "$@" )
			IFS=$old_IFS
			;;
	(*)		return 1;;
    esac

    return 0
}

function _setup_writeSingleOption
{
    test -z "$1" || echo "$1" >"$2"
}

function _setup_writeInterface
{
    local vdir=$1
    local idx=$2
    local tmp=$3

    local name=${tmp%%=*}
    test "$name" != "$tmp" || name=

    tmp=${tmp##${name}=}
    local dev=${tmp%%:*}
    test "$dev" != "$tmp" || dev=

    tmp=${tmp##${dev}:}
    local mask=${tmp##*/}
    test "$mask" != "$tmp"  || mask=

    local ip=${tmp%%/${mask}}

    local prefix=
    test "${mask%%.*}" != "$mask" || {
	prefix=$mask
	mask=
    }

    d=$vdir/interfaces/$idx
    mkdir "$d"
    
    _setup_writeSingleOption "$name"   $d/name
    _setup_writeSingleOption "$dev"    $d/dev
    _setup_writeSingleOption "$ip"     $d/ip
    _setup_writeSingleOption "$mask"   $d/mask
    _setup_writeSingleOption "$prefix" $d/prefix

    test -n "$dev" -o -n "$SETUP_NETDEV" || \
	echo $"No device specified for interface '$idx'; do not forget to set the 'nodev' option" >&2
}

function setup_setDefaults
{
    : ${SETUP_CONFDIR:=$__CONFDIR/$1}
    : ${SETUP_LOCKFILE:=$__RUNDIR/$1}
    findFile SETUP_FSTAB "$__CONFDIR"/.defaults/fstab "$__PKGLIBDEFAULTDIR"/fstab
}

function setup_writeOption
{
    local name=$1
    local cfgdir=${SETUP_CONFDIR:?}
    local i

    mkdir -p "$cfgdir"/interfaces "$cfgdir"/apps/init "$cfgdir"/uts

    _setup_writeSingleOption "$name"            "$cfgdir"/name
    _setup_writeSingleOption "$SETUP_CONTEXT"   "$cfgdir"/context
    _setup_writeSingleOption "$SETUP_HOSTNAME"  "$cfgdir"/uts/nodename
    _setup_writeSingleOption "$SETUP_NETDEV"    "$cfgdir"/interfaces/dev
    _setup_writeSingleOption "$SETUP_NETMASK"   "$cfgdir"/interfaces/mask
    _setup_writeSingleOption "$SETUP_NETPREFIX" "$cfgdir"/interfaces/prefix
    _setup_writeSingleOption "$SETUP_NETBCAST"  "$cfgdir"/interfaces/bcast
    _setup_writeSingleOption "$SETUP_INITSTYLE" "$cfgdir"/apps/init/style

    local idx=0
    for i in "${SETUP_INTERFACES[@]}"; do
	_setup_writeInterface "$cfgdir" $idx "$i"
	let ++idx
    done

    test -z "$SETUP_FLAGS" || for i in "${SETUP_FLAGS[@]}"; do
	echo "$i"
    done >"$cfgdir"/flags

    ln -s "$SETUP_LOCKFILE"   "$cfgdir"/run
}

function setup_writeInitialFstab
{
    cat "${SETUP_FSTAB:?}" >"${SETUP_CONFDIR:?}"/fstab
}

function setup_test
{
    SETUP_INTERFACES=()

    setup_setOption2 --interface foo0=eth0:1.2.3.4/1
    setup_setOption2 --interface foo1=eth0:1.2.3.4/255.255.248.0
    setup_setOption2 --interface foo2=eth0:1.2.3.4
    setup_setOption2 --interface foo3=1.2.3.4
    setup_setOption2 --interface foo4=1.2.3.4/1
    setup_setOption2 --interface eth0:1.2.3.4
    setup_setOption2 --interface eth0:1.2.3.4/1
    setup_setOption2 --interface 1.2.3.4
    setup_setOption2 --interface 1.2.3.4/1

    setup_writeOption xx
}
