#!/bin/sh

HOST_NAME="your_hostname"
DOMAIN_NAME="your_domain_and.TLD"
FQDN=${HOST_NAME}.${DOMAIN_NAME}
FQDN_INTRA=${HOST_NAME}-intra.${DOMAIN_NAME}

PKG_LIST="build-essential locales locate lynx make net-tools ntp screen ssh sudo vim"

# The non-root user you set up during the Debian install
OS_USER=""

# Choose either mysql or mariadb
DATABASE="mysql"
APT_OPTIONS="-y"
# Choose stable, oldstable, oldoldstable, unstable (devs only), or version (e.g. 19.11, 19.05)
KOHA_BRANCH="stable"

LIBRARY_NAME=$HOST_NAME


#### MANUAL PRE-SCRIPT STEPS
#### 1) Install debian; for pkg options at end choose only ssh server and system utilities.
#### 2) If using a caching apt proxy, put 
####      'Acquire::http { Proxy "http://<PROXY_ADDRESS>:3142"; }' in /etc/apt/apt.conf.d/proxy
#### 3) As root: apt-get install sudo
#### 4) As root: adduser <non-root_user> sudo
#### 5) Use scp from another machine to copy this script to non-root_user's home dir
#### 6) Log in as non-root_user and run this script.

step01()
{
    echo "#######################################################"
    echo "# Step 01 -- Basic Debian utilities and configuration #"
    echo "#######################################################"

    sudo apt-get $APT_OPTIONS install ${PKG_LIST}

    sudo dpkg-reconfigure locales

    # sudo adduser $OS_USER sudo

    echo -n "Please take snapshot \"001 Debian Installed (no Guest Additions)\", then press ENTER."
    read dummyarg
}

step02()
{
    echo "#################################################"
    echo "# Step 02 -- Install VirtualBox Guest Additions #"
    echo "#################################################"

    sudo apt-get $APT_OPTIONS install linux-headers-`uname -r`

    echo "Please insert Guest Additions CD into virtual CD drive (use the"
    echo -n "Devices > Optical Drives menu), then press ENTER."
    sudo mount -t iso9660 /dev/cdrom /mnt
    sudo sh /mnt/VBoxLinuxAdditions.run
    sudo umount /mnt

    echo "Guest Additions installed. Remove Guest Additions CD from the virtual"
    echo -n "CD drive (you can force unmount if necessary), then press ENTER."
    read dummyarg

    echo -n "Please take snapshot \"002 Guest Additions installed\", then press ENTER."
    read dummyarg
}

step03()
{
    echo "###############################"
    echo "# Step 03 -- Install database #"
    echo "###############################"

    sudo apt-get $APT_OPTIONS install ${DATABASE}-server
    sudo mysql_secure_installation

    echo -n "Please take snapshot \"003 Database ${DATABASE} installed\", then press Enter."
    read dummyarg
}

step04()
{
    echo "###########################"
    echo "# Step 04 -- Install Koha #"
    echo "###########################"

    wget -q -O- https://debian.koha-community.org/koha/gpg.asc | sudo apt-key add -
    echo "deb http://debian.koha-community.org/koha $KOHA_BRANCH main" \
        | sudo tee /etc/apt/sources.list.d/koha.list > /dev/null

    echo "deb http://deb.debian.org/debian stretch-backports main" \
        | sudo tee /etc/apt/sources.list.d/stretch-backports.list > /dev/null

    sudo apt-get $APT_OPTIONS update
    sudo apt-get $APT_OPTIONS install koha-common
    sudo mv /etc/koha/koha-sites.conf /etc/koha/koha-sites.conf.ORIG
    sed "s/^DOMAIN=.*/DOMAIN=\".${DOMAIN_NAME}\"/" /etc/koha/koha-sites.conf.ORIG \
        | sudo tee /etc/koha/koha-sites.conf > /dev/null 

    echo -n "Please take snapshot \"004 Koha installed (needs configuration)\", then press Enter."
    read dummyarg
}

step05()
{
    echo "##################################"
    echo "# Step 05 -- Instantiate library #"
    echo "##################################"

    sudo a2enmod rewrite; sudo systemctl restart apache2
    sudo a2enmod cgi; sudo systemctl restart apache2

    sudo koha-create --create-db $LIBRARY_NAME

    echo -n "Please take snapshot \"005 Koha site '${LIBRARY_NAME}' instantiated\", then press Enter."
    read dummyarg
}

step06()
{
    echo "################################"
    echo "# Step 06 -- Configure library #"
    echo "################################"

    sudo cp --preserve /etc/hosts /etc/hosts.ORIG
    sudo sed -i -e "s/^127\.0\.1\.1.*$/127.0.1.1\t${HOST_NAME}\t${FQDN}\t${FQDN_INTRA}/" /etc/hosts

    echo "Press ENTER to get the password for koha account 'koha_${LIBRARY_NAME}'."
    echo -n "Make a note of it; you will need it in a moment."
    read dummyarg
    sudo koha-passwd ${LIBRARY_NAME}

    echo "Use the id 'koha_${LIBRARY_NAME}' and the password you just noted to configure koha."
    echo -n "Press Enter to begin configuring koha through the web interface."
    read dummyarg
    lynx http://${FQDN_INTRA}
}

###
### MAIN PROGRAM
###
#if [ "$1" = "" ]; then
#    # Make it look like user asked for step 1
#    set -- 1
#fi

case $1 in
    help|--help)
        echo "Entering 1 through 6 will (re)start installation at that step:"
        echo "1 : Basic Debian utilities and configuration"
        echo "2 : Install VirtualBox Guest Additions"
        echo "3 : Install database"
        echo "4 : Install Koha"
        echo "5 : Instantiate library"
        echo "6 : Configure library"
        ;;
    1|01|001)
        step01
        step02
        step03
        step04
        step05
        step06
        ;;
    2|02|002)
        step02
        step03
        step04
        step05
        step06
        ;;
    3|03|003)
        step03
        step04
        step05
        step06
        ;;
    4|04|004)
        step04
        step05
        step06
        ;;
    5|05|005)
        step05
        step06
        ;;
    6|06|006)
        step06
        ;;
    *)
        echo "Unrecognized or missing argument. Please use 'help', or 1 through 6."
        exit 
esac
