#! /bin/sh

# Copyright (C) 2007 John A. Schmidt
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


DO_INSTALL="false"
DO_REMOVE="false"
DO_SETUP="false"
USER_NAME="jas"
CHROOT_NAME=""
CHROOT_VERSION="etch"
EVERGREEN_INSTALL_SRC=/home/jas/alphag/scripts

usage () {

    echo "This installs a chroot in the directory specified by the -c option."
    echo "Usage: -i -- install"
    echo "Usage: -r -- remove"
    echo "Usage: -u user specify the user that you want the chroot setup for"
    echo "Usage: -s -- setup chroot"
    echo "Usage: -c chroot_name -- specify the chroot name."
    echo "Usage: -v chroot_version -- i.e. lenny, etch"
    echo "Typical usage: evergreen_chroot -u jas -c evergreen -v lenny -i"
    
    exit
}

while getopts "i r u: s c: v: h" options; do
    case $options in
        i)
            DO_INSTALL="true"
            ;;
        r)
            DO_REMOVE="true"
            ;;
        u)
            USER_NAME=$OPTARG
            ;;
        s)
            DO_SETUP="true"
            ;;
        c)
            CHROOT_NAME=$OPTARG
            ;;
        v)
            CHROOT_VERSION=$OPTARG
            ;;
        h)
            usage
            ;;
        *) 
            echo "$options not valid"
            exit 1
            ;;
    esac
done


create_chroot () {
    cd /
    
    echo "Making a chroot in $CHROOT_NAME for $USER_NAME . . ."

    mkdir -p /$CHROOT_NAME

    if [ ! -e /$CHROOT_VERSION.tar ]; then
        echo "Didn't find /$CHROOT_VERSION.tar . . ."
        debootstrap --make-tarball /$CHROOT_VERSION.tar $CHROOT_VERSION /$CHROOT_NAME \
            http://mirrors.xmission.com/debian/
    fi
      
    debootstrap --unpack-tarball /$CHROOT_VERSION.tar $CHROOT_VERSION /$CHROOT_NAME
    
    echo "proc /$CHROOT_NAME/proc proc none 0 0" >> /etc/fstab
    mount proc /$CHROOT_NAME/proc -t proc
    cp /etc/hosts /$CHROOT_NAME/etc/hosts.orig

    cd /$CHROOT_NAME/etc
    sed -e 's/puppy.mech.utah.edu//;/192/d' hosts.orig > hosts
    cd -
    ## Must edit /etc/hosts to get rid of all the stuff and just leave the
    ## 127.0.0.1       localhost
    ## 127.0.1.1       puppy
    
    echo "$CHROOT_NAME" > /$CHROOT_NAME/etc/debian_chroot
   
}

setup_chroot () {

    echo "Enter the password for the root user . . ."
    sudo chroot /$CHROOT_NAME passwd

    if [ $? -gt 0 ]; then
        echo "Try again, please enter the password for the root user . . ."
        sudo chroot /$CHROOT_NAME passwd
    fi

    sudo chroot /$CHROOT_NAME useradd -m $USER_NAME

    echo "Enter the password for $USER_NAME . . ."
    sudo chroot /$CHROOT_NAME passwd $USER_NAME

    if [ $? -gt 0 ]; then
        echo "Try again, please enter the password for $USER_NAME . . ."
        sudo chroot /$CHROOT_NAME passwd $USER_NAME
    fi


    sudo chroot /$CHROOT_NAME cp /etc/apt/sources.list /etc/apt/sources.list.orig
    
    sudo chroot /$CHROOT_NAME aptitude update

    sudo chroot /$CHROOT_NAME aptitude -y install debian-keyring sudo
    sudo chroot /$CHROOT_NAME apt-get -y remove nano

    sudo chroot /$CHROOT_NAME visudo


    cp $EVERGREEN_INSTALL_SRC/evergreen.sh /$CHROOT_NAME/home/$USER_NAME
    chmod ugo+rwx /$CHROOT_NAME/home/$USER_NAME/evergreen.sh

}


remove_chroot () {

    if [ -d /$CHROOT_NAME ]; then
        umount -l /$CHROOT_NAME/proc
        cd /
        rm -rf $CHROOT_NAME
        cd -
    fi

    cd /etc

    sed -e '/'$CHROOT_NAME'/d' fstab > fstab.new
    mv fstab.new fstab

}

install () {
    echo "Installing $CHROOT_NAME chroot . . ."
    create_chroot
    setup_chroot
}

remove () {
    echo "Removing $CHROOT_NAME chroot . . ."
    remove_chroot
}

setup () {
    echo "Doing chroot setup . . ."
    setup_chroot
}


if [ -z $CHROOT_NAME ]; then
    echo "Must specify directory with -d, exiting . . ."
    exit
fi

if [ $DO_INSTALL == "true" ]; then
    install
fi

if [ $DO_REMOVE == "true" ]; then
    remove
fi

if [ $DO_SETUP == "true" ]; then
    setup
fi



exit
