#!/bin/sh

DYNAMIPS_BUILD=yes
XENOMIPS_FS_BUILD=yes
CONFIG_FS_BUILD=yes
IOS_FS_BUILD=yes

XENOMIPS_DIR="files/"
XENOMIPS_IMAGES_DIR="../xenomips-images/"
IOS_DIR="ios/"
SSH_OPEN_KEY=files/id_dsa.pub

#Xenomips FS
XENOMIPS_FS_SIZE=2048k                  # size in 1k blocks
XENOMIPS_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips1.img

# Config FS
CONFIG_FS_SIZE=1000k
CONFIG_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips-configXXX.img  # XXX will be automagically substitued with 
CONFIG_FS_NUMBER=5                                            # the number of the Xenomips instance

# IOS FS
IOS_FS_SIZE=2048k
IOS_FS_NAME=${XENOMIPS_IMAGES_DIR}/xenomips-ios1.img

DEBIAN_MIRROR=http://debian.org.ua/debian
DISTRO=sid
PACKAGES="openssh-server libc6-xen libelf1 libpcap0.8 screen nfs-common quagga tcpdump"
DYNAMIPS_SOURCES=http://www.ipflow.utc.fr/dynamips/dynamips-0.2.7.tar.gz
DYNAMIPS_BUILD_DIR=dynamips/
KERNEL_VERSION=`uname -r`


WORK_DIR=${XENOMIPS_FS_NAME%%.img}
WORK_DIR2=/tmp/xenomips-build

myecho()
{
    echo '**************************'
    echo "$@"
    echo '**************************'
}

build_and_install_dynamips()
{
    OLD_PWD="`pwd`"
    DESTDIR=${WORK_DIR}/usr

    if echo $DYNAMIPS_BUILD | grep -qi ^y
    then
        rm -rf ${DYNAMIPS_BUILD_DIR}/
        mkdir -p ${DYNAMIPS_BUILD_DIR}/
        cd ${DYNAMIPS_BUILD_DIR}/
        wget $DYNAMIPS_SOURCES
        tar xfz dynamips*tar*
        cd dynamips*/
        perl -p -i -e s@^PCAP_LIB=.*@PCAP_LIB=-lpcap@ Makefile
        make
    else
        cd ${DYNAMIPS_BUILD_DIR}/dynamips*/
    fi
    make install DESTDIR=${DESTDIR}
    cd ${OLD_PWD}
}

myecho Preparing the Xenomips filesystem
dd if=/dev/zero of=${XENOMIPS_FS_NAME} seek=${XENOMIPS_FS_SIZE} bs=1024 count=1
mkfs.ext3 -F ${XENOMIPS_FS_NAME}
mkdir -p ${WORK_DIR} ${WORK_DIR2}
mount -o loop ${XENOMIPS_FS_NAME} ${WORK_DIR}
debootstrap  ${DISTRO} ${WORK_DIR} ${DEBIAN_MIRROR} 
cat <<SCRIPT | chroot ${WORK_DIR} sh -s
unset LANG
apt-get -y --force-yes install ${PACKAGES}
/etc/init.d/ssh stop
cat <<FSTAB > /etc/fstab
proc            /proc           proc            defaults                0       0
tmpfs           /tmp            tmpfs           defaults,nosuid,nodev   0       0

/dev/hda1       /                       ext3    ro              0       0
/dev/hda2       /xenomips/ios           ext3    ro              0       0
/dev/hda3       /xenomips/config        ext3    rw              0       0
FSTAB
mkdir -p /data/Cisco_IOS /xenomips

cat <<'INTERFACES' > /etc/network/interfaces
auto lo eth0

iface lo inet loopback 
        up for i in \`ifconfig -a | grep 'eth.*Link encap' | awk '{print \$1}'\`; do ifconfig \$i up ; done || true

#iface eth0 inet dhcp
INTERFACES

echo /usr/local/bin/xenomips.sh > /etc/rc.local
cd /etc/rcS.d; ln -s ../init.d/xenomips-init S35xenomips-init
rm /etc/rc2.d/S*quagga

#perl -i -p -e s@C_PATH=/etc/quagga@C_PATH=/xenomips/etc/quagga@ /etc/init.d/quagga
mv /etc/quagga /etc/quagga.ORIG
ln -s /xenomips/config/etc/quagga /etc/quagga

mkdir -p /xenomips/{ios,config,dynamips} /var/lib/xenomips /root/.ssh

cat <<PROFILE >> /root/.profile
TERM=linux
screen -ls | grep -q Attached && screen -x || screen -ls | grep -q Detached && screen -r
PROFILE
SCRIPT

for kernel_version in ${KERNEL_VERSION} 2.6.18-without-PAE-4-xen-686
do
    cp -R /lib/modules/${KERNEL_VERSION} ${WORK_DIR}/lib/modules
done

cp ${XENOMIPS_DIR}/xenomips-init ${WORK_DIR}/etc/init.d/
cp ${XENOMIPS_DIR}/xenomips.sh ${WORK_DIR}/usr/local/bin
sudo chmod +x  ${WORK_DIR}/usr/local/bin/xenomips.sh ${WORK_DIR}/etc/init.d/xenomips-init

cat ${SSH_OPEN_KEY} > ${WORK_DIR}/root/.ssh/authorized_keys
chmod 600  ${WORK_DIR}/root/.ssh/authorized_keys

build_and_install_dynamips

mv ${WORK_DIR}/etc/quagga.ORIG ${WORK_DIR2}/quagga
perl -i -p -e 's/=no/=yes/' ${WORK_DIR2}/quagga/daemons
touch ${WORK_DIR2}/quagga/{zebra,bgpd,ospfd,ripd,ripngd,ospf6d,isisd}.conf
umount ${WORK_DIR}

if  echo $CONFIG_FS_BUILD | grep -qi ^y
then
    myecho Preparing the Xenomips config filesystem
    dd if=/dev/zero of=${CONFIG_FS_NAME} bs=1k count=${CONFIG_FS_SIZE}
    mkfs.ext3 -F ${CONFIG_FS_NAME}

    mount -o loop ${CONFIG_FS_NAME} ${WORK_DIR}
    mkdir -p ${WORK_DIR}/etc
    mv ${WORK_DIR2}/quagga ${WORK_DIR}/etc
    umount ${WORK_DIR}

    myecho Creating $CONFIG_FS_NUMBER copies of the Xenomips config filesystem
    for i in `seq 1 $CONFIG_FS_NUMBER`
    do
        cp ${CONFIG_FS_NAME} `echo ${CONFIG_FS_NAME} | sed s/XXX/$i/`
    done
fi


if echo $IOS_FS_BUILD | grep -qi ^y
then
    myecho Preparing the filesystem with the IOSes
    dd if=/dev/zero of=${IOS_FS_NAME} bs=1k count=1 seek=${CONFIG_FS_SIZE}
    mkfs.ext3 -F ${IOS_FS_NAME}
    mount -o loop ${IOS_FS_NAME} ${WORK_DIR}
    cp ${IOS_DIR}/* ${WORK_DIR}
    umount ${WORK_DIR}
fi