#!/bin/bash

if [ ! -e buildtools.conf ]; then
    cp buildtools.conf.delivered buildtools.conf
fi
source buildtools.conf

function buildclient() {
    echo "buildrescuebru: building client iso image..."
    cp /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg.client /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg
    cp /mnt/custom/customcd/isoroot/boot/grub/grub-522.cfg.client /mnt/custom/customcd/isoroot/boot/grub/grub-522.cfg
    dobuild "client"
}

function buildserver() {
    echo "buildrescuebru: building server iso image..."
    cp /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg.server /mnt/custom/customcd/isoroot/isolinux/isolinux.cfg
    cp /mnt/custom/customcd/isoroot/boot/grub/grub-522.cfg.server /mnt/custom/customcd/isoroot/boot/grub/grub-522.cfg
    dobuild "server"
}

function dobuild() {
    echo "buildrescuebru: creating squashfs for $1..."
    /usr/sbin/sysresccd-custom squashfs
    echo "buildrescuebru: removing stale sysresccd files from isofile directory..."
    rm -fv /mnt/custom/customcd/isofile/sysresccd*
    echo "buildrescuebru: generating new iso/md5 for $volname-$1..."
    /usr/sbin/sysresccd-custom isogen $volname-$1
    echo "buildrescuebru: moving sysresccd iso/md5 files to volume name files in xfer directory..."
    origiso=`ls /mnt/custom/customcd/isofile/sysresccd-*.iso`
    origmd5=`ls /mnt/custom/customcd/isofile/sysresccd-*.md5`
    mv -v $origiso /mnt/custom/isoxfer/$volname-$1.iso
    mv -v $origmd5 /mnt/custom/isoxfer/$volname-$1.md5
}   

# ------

echo -n "Is this a development system or a build machine? [d/b] : "
read systype
echo -n "Will this be a client CD or server CD or both or no CD? [c/s/b/n] : "
read buildtype
if [ "$buildtype" != "n" ]; then
    echo -n "Enter CD volume name : "
    read volname
fi
echo -n "Would you like to extract a new customcd? [y/n] : "
read ext
echo -n "Do you need to chroot into the customcd filesystem? [y/n] : "
read chrt
if [ "$ext" = "y" ]; then
    echo "Moving existing customcd to customcd-`date +%Y%m%d%H%M%S`..."
    mv /mnt/custom/customcd /mnt/custom/customcd-`date +%Y%m%d%H%M%S`
    echo "Extracting new customcd..."
    /usr/sbin/sysresccd-custom extract
fi
if [ "$buildtype" != "n" ]; then
    mkdir -p /mnt/custom/isoxfer
    rm -f /mnt/custom/isoxfer/*
fi
/mnt/custom/buildtools/get_distributables $systype
if [ "$REL_TYPE" = "p" ]; then
	/mnt/custom/buildtools/get_documentation $systype
fi
/mnt/custom/buildtools/fixpermissions
/mnt/custom/buildtools/setsymlinks
if [ "$chrt" = "y" ]; then
    /mnt/custom/buildtools/customchroot
fi
mkdir -p /mnt/custom/customcd/isoroot/rescuebru-src
rm -rf /mnt/custom/customcd/isoroot/rescuebru-src/*
cp -R /mnt/custom/rescuebru-src/* /mnt/custom/customcd/isoroot/rescuebru-src

mkdir -p /mnt/custom/customcd/isoroot/buildtools
rm -rf /mnt/custom/customcd/isoroot/buildtools/*
cp -R /mnt/custom/buildtools/* /mnt/custom/customcd/isoroot/buildtools
case $buildtype in
    "c" )
    buildclient
    ;;
    
    "s" )
    buildserver
    ;;
    
    "b" )
    buildclient
    buildserver
    ;;
    "n" )
    echo "This was a file update only. Not building a new iso."
    ;;
esac
if [ "$buildtype" != "n" ]; then
    if [ "$systype" = "b" ]; then
        echo "buildrescuebru: transferring iso/md5 files back to development system..."
        /mnt/custom/buildtools/putiso
    else
        echo "iso/md5 file generation complete. Files are in /mnt/custom/isoxfer"
    fi
else
    echo "File update complete."
fi

