#! /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


# Evergreen Installer for Debian
# Currently can deal with Etch or Lenny

# Account Creation Information
#
# There are 3 'accounts' that are set up:
# 1.  opensrf user account
#
# 2.  2 jabber accounts called
#     a) opensrf
#     b) router
#
# 3.  Postgresql database with a database name and a database user
#     a) database name
#     b) database user
#


## Following accounts are created

##  Account Name               Purpose                    Password

##    postgres (S)      owns the DB  infrastructure      
##    opensrf  (S)      owns the OpenILS machinery          opensrf

##    evergreen         DB user -- createuser               everPass
##    evergreen         DB name that holds data             everPass

##    router            account within ejabber              everPass
##    opensrf           account within ejabber              everPass

##  Key  S = system account created either by package, i.e. postgresql, or by
##           adduser
##



HOST_NAME=""
IP=""
DISTRIBUTION=etch

OPENSRF_USER_PW="opensrf"
OPENSRF_JAB_PW="everPass"
ROUTER_JAB_PW="everPass"
PG_DB_NAME="evergreen"
PG_DB_USER="evergreen"
PG_DB_USER_PW="everPass"


DO_INSTALL=false
DO_UNINSTALL=false
DO_RESUME=false

INSTALL_FLAGS="BUILD_PERL_MODULES INSTALL_DBD SETUP_POSTGRESQL INSTALL_OPENSRF \
INSTALL_EVERGREEN SETUP_APACHE SETUP_EJABBERD CONFIGURE_EVERGREEN \
FINALIZE_OPENSRF"

initialize_flags () {

    i=0;
    for fl in $INSTALL_FLAGS; do
        FLAG[i]=$fl;
        FLAG_STATUS[i++]=false;
    done

}


set_postgresql_version () {

    case $DISTRIBUTION in
        etch)
            PGSQL_VER=8.1
            DBVER=81
            ;;
        lenny)
            PGSQL_VER=8.2
            DBVER=82
            ;;
        *)
            echo "Unknown distribution '$DISTRIBUTION', aborting . . . "
            exit 1
            ;;
    esac

}

create_packages () {

    set_postgresql_version

    DEFAULT_PKGS="sudo gcc w3m psmisc xulrunner less whois"
    
## Install postgresql
    
    POSTGRESQL_PKGS="postgresql-${PGSQL_VER} postgresql-contrib-${PGSQL_VER} \
    postgresql-doc-${PGSQL_VER} postgresql-plperl-${PGSQL_VER} \
    postgresql-server-dev-${PGSQL_VER}"
    
    
## Install apache and modperl
    
    APACHE_PKGS="apache2-mpm-prefork apache2-prefork-dev \
    libapache2-mod-perl2 apache2-utils"
    
    
##  Install Perl modules
    
    PERL_PKGS="libcache-memcached-perl libcgi-perl \
    libclass-dbi-abstractsearch-perl libclass-dbi-perl libdatetime-perl \
    libdatetime-format-builder-perl libdatetime-timezone-perl libdbd-pg-perl \
    libdbd-sqlite3-perl libdbi-perl liberror-perl libexception-class-perl \
    libgd-graph3d-perl libmarc-record-perl libmd5-perl libnet-server-perl \
    libnet-z3950-perl libspreadsheet-writeexcel-perl libtemplate-perl \
    libtext-aspell-perl libtext-csv-perl libunix-syslog-perl libwww-perl \
    libxml-libxml-perl libxml-libxslt-perl libxml-perl libxml-sax-perl \
    libxml-simple-perl libmarc-charset-perl  libuniversal-require-perl \
    liblog-log4perl-perl libemail-send-perl"

    PERL_BLD_PKGS="dh-make-perl devscripts libmodule-build-perl \
    liblog-log4perl-perl libmozjs0d libmozjs-dev"

## Install libxml2
    
    LIBXML2_PKGS="libxml2 libxml2-dev"
    
    
## Install libxslt
    
    LIBXSLT_PKGS="libxslt1.1 libxslt1-dev"
    
## Install memcached and libmemcache
    
    MEMCACHE_PKGS="memcached libevent-dev libmemcache0 \
    libmemcache-dev"
    
## Install yaz-2.0.x
    
    YAZ_PKGS="yaz libyaz2 libyaz2-dev"
    
    
## Install aspell + dictionary
    
    ASPELL_PKGS="aspell libaspell-dev aspell-en"
    
    
## Install GD Graphics Library
    
    GD_PKGS="libgd2-xpm libgd2-xpm-dev"
    
    
## Install libreadline
    
    READLINE_PKGS="libreadline5-dev"
    
    
## Install automake
    
    AUTOMAKE_PKGS="automake"
    
    
## Install ejabberd
    
    EJABBERD_PKGS="ejabberd"

    EXTRA_PKGS="build-essential ntpdate postgresql-client libexpat1-dev \
libperl-dev libclass-dbi-pg-perl libclass-dbi-sqlite-perl libnet-jabber-perl \
libtest-pod-perl libfile-find-rule-perl librpc-xml-perl libpq-dev \
libemail-send-perl libtool libtie-ixhash-perl"
    
    PKGS="${DEFAULT_PKGS} ${POSTGRESQL_PKGS} ${APACHE_PKGS} ${PERL_PKGS} \
${PERL_BLD_PKGS} ${LIBXML2_PKGS} ${LIBXSLT_PKGS} ${MEMCACHE_PKGS} ${YAZ_PKGS} \
${ASPELL_PKGS} ${GD_PKGS} ${READLINE_PKGS} ${AUTOMAKE_PKGS} ${EJABBERD_PKGS} \
${EXTRA_PKGS}"
    

}

install_dependencies () {

    echo "Installing a subset of packages . . ."
    
    create_packages

    echo $PKGS

    apt-get -y install $PKGS
    
}

find_flag_id () {

    for ((i=0; i<${#FLAG[*]};i++)); do
        if [ $1 = ${FLAG[i]} ]; then
            id=$i;
            return 0;
        fi
    done
    id=-1;
    return -1;

}

build_install_perl_modules () {

    
    PERL_MODULES="DateTime::Format::ISO8601 Javascript::SpiderMonkey \
     MARC::File::XML Time::HiRes"

    find_flag_id "BUILD_PERL_MODULES"

    echo "id = $id"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Installing ${PERL_MODULES} . . ."

        apt-get -y install $PERL_BLD_PKGS
        
        for m in $PERL_MODULES; do
            dh-make-perl --build --install --notest --cpan $m
        done
        
        FLAG_STATUS[$id]=true
    else
        echo "Perl modules have been built and installed . . ."
    fi
}

remove_perl_modules () {

    cd $HOME
    
    dpkg -P libdatetime-format-iso8601-perl \
        libjavascript-spidermonkey-perl \
        libmarc-xml-perl

    rm -rf DateTime-Format-ISO8601*
    rm -rf JavaScript-SpiderMonkey*
    rm -rf MARC-XML*
    rm -f *.deb
    
}



## Install libdbi  must use unstable repository  add to /etc/apt/sources.list

install_apt_source_unstable () {
    
    cd $HOME
    echo "Installing apt-source-unstable . . ."

    cd /etc/apt/
    cp sources.list sources.list.orig

    echo "deb http://ftp.us.debian.org/debian unstable main" >> /etc/apt/sources.list


    echo "Package: *" >> /etc/apt/preferences
    echo "Pin: release lenny" >> /etc/apt/preferences
    echo "Pin-Priority: 900" >> /etc/apt/preferences
    echo "" >> /etc/apt/preferences
    echo "" >> /etc/apt/preferences


    echo "Package: *" >> /etc/apt/preferences
    echo "Pin: release unstable" >> /etc/apt/preferences
    echo "Pin-Priority: 600" >> /etc/apt/preferences

    apt-get update
}


remove_apt_source_unstable () {

    cd $HOME
    mv /etc/apt/sources.list.orig /etc/apt/sources.list

    rm /etc/apt/preferences
    
    apt-get update
}


## Install libdbd

build_install_dbi_from_sid () {

    cd /etc/apt
    echo "deb-src http://ftp.debian.org/debian sid main" >> sources.list
    apt-get update
    cd -

    LIBDBI_PKGS="libdbi0 libdbd-pgsql"

    for i in $LIBDBI_PKGS; do
        apt-get -y build-dep $i
        apt-get -y source --compile $i
        dpkg -i $i*deb
    done

}

install_dbd_pkgs () {

    DBD_PKGS="libdbi0-dev libdbi0 libdbd-pgsql"

    find_flag_id "INSTALL_DBD"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Installing dbd . . ."
        if [ $DISTRIBUTION = "etch" ]; then
            build_install_dbi_from_sid
            
        elif [ $DISTRIBUTION = "lenny" ]; then
            install_apt_source_unstable
            apt-get -y install $DBD_PKGS
            remove_apt_source_unstable
        fi
                
        FLAG_STATUS[$id]=true
    else
        echo "DBD packages have been installed . . ."
    fi
}

remove_dbd_pkgs () {

    apt-get -y remove $DBD_PKGS
}



setup_postgresql () {

    find_flag_id "SETUP_POSTGRESQL"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Configuring postgresql database . . ."
        cd /etc/postgresql/${PGSQL_VER}/main
        
        if [ ! -e pg_hba.conf.orig ]; then
            cp pg_hba.conf pg_hba.conf.orig
        fi
        
        sed -e '79,79s/ident sameuser/md5/' -e '/127/a\host    all         all         '$IP'/32          md5' pg_hba.conf.orig > pg_hba.conf
        
        if [ ! -e postgresql.conf.orig ]; then
            cp postgresql.conf postgresql.conf.orig
        fi
        
        sed -e '/#listen_address/{s/#listen_address/listen_address/; s/localhost/localhost,'$HOSTNAME'/}' postgresql.conf.orig > postgresql.conf
        
        cd -
        
        /etc/init.d/postgresql-${PGSQL_VER} restart
        
        su -c "psql template1 < /usr/share/postgresql/${PGSQL_VER}/contrib/tsearch2.sql" - postgres
        
        su -c "psql template1 < /usr/share/postgresql/${PGSQL_VER}/contrib/tablefunc.sql" - postgres
        
        su -c "createlang plpgsql -d template1" - postgres

        if [ -z $PG_DB_NAME ]; then 
            read -p "Enter the name for the Evergreen database: "
            PG_DB_NAME=$REPLY
        fi

        if [ -z $PG_DB_USER ]; then
            read -p "Enter the name of the database user: "
            PG_DB_USER=$REPLY
        fi
        
        su -c "createdb -E UNICODE $PG_DB_NAME" - postgres
        su -c "createlang plperl $PG_DB_NAME" - postgres
        su -c "createlang plperlu $PG_DB_NAME" - postgres
        su -c "createuser -U postgres -s -P $PG_DB_USER" - postgres
        
        FLAG_STATUS[$id]=true
    else
        echo "Postgresql has been set up and configured . . ."
    fi
}

remove_setup_postgresql () {

    cd $HOME
    cd /etc/postgresql/${PGSQL_VER}/main

    mv pg_hba.conf.orig pg_hba.conf
    mv postgresql.conf.orig postgresql.conf
}

install_opensrf () {

    find_flag_id "INSTALL_OPENSRF"

    if [ ${FLAG_STATUS[$id]} = false ]; then
        
        cd $HOME
        echo "Installing opensrf . . ."
        
    # setup the opensrf user on the system
        
        if [ ! -z $OPENSRF_USER_PW ]; then
            useradd -m opensrf -p `mkpasswd $OPENSRF_USER_PW`
        
        else
            useradd -m opensrf
            echo "Enter the password for the 'opensrf' account . . ."
            passwd opensrf
            if [ $? -gt 0 ]; then
                echo "Try again, enter the password for the 'opensrf' account . . ."
                passwd opensrf
            fi
        fi
        
        
    # set up the PATH, the PER5LIB env variable, and LD_CONF
        
        cd ~opensrf
        
        echo "export PATH=$PATH:/openils/bin" >> .bashrc
        echo "export PERL5LIB=/openils/lib/perl5:$PERL5LIB" >> .bashrc
        echo "export LD_LIBRARY_PATH=/openils/lib" >> .bashrc
        
        cd -
        
        wget http://www.open-ils.org/downloads/OpenSRF-0.9.tar.gz
        
        sudo tar xfz OpenSRF-0.9.tar.gz
        cd OpenSRF-0.9/ 
        
        if [ ! -e Makefile.orig ]; then
            cp Makefile Makefile.orig
        fi
        
        sed -e '1,1i\SHELL=/bin/bash' Makefile.orig > Makefile 
        
        cd /etc/apache2/
        if [ ! -e httpd.conf.orig ]; then
            cp httpd.conf httpd.conf.orig
        fi
        
        echo "# This is here for apxs compatibility (and this comment is \
required)" >> httpd.conf
        echo "#LoadModule mod_placeholder \
 /usr/lib/apache2/modules/mod_placeholder.so" >> httpd.conf
        
        cd -
        
        CFLAGS=-D_LARGEFILE64_SOURCE make all
        
    ## Gets installed in /openils directory
        sudo make install  
        
        FLAG_STATUS[$id]=true
    else
        echo "Opensrf has been installed and configured . . ."
    fi
}

install_evergreen () {

    find_flag_id "INSTALL_EVERGREEN"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        
        echo "Installing evergreen . . ."
        
        wget http://www.open-ils.org/downloads/Evergreen-ILS-1.2.1.0.tar.gz
        
        sudo tar zxf Evergreen-ILS-1.2.1.0.tar.gz 
        
        cd Evergreen-ILS-1.2.1.0/ 
        
        if [ ! -e install.conf.default.orig ]; then
            cp install.conf.default install.conf.default.orig
        fi
        
        sed -e '35,35s/openils_core/openils_all/;35,35s/openils_web/evergreen_core/;35,35s/"openils_db"//;s/\/usr\/local\/lib\/dbd/\/usr\/lib\/dbd/;115,115s/127.0.0.1/'$HOSTNAME'/;118,118s/postgres/'$PG_DB_USER'/;119,119s/82/'$DBVER'/;120,120s/postgres/'$PG_DB_USER_PW'/' install.conf.default.orig > install.conf.default 
        
        make config
        
        cd Open-ILS/src/c-apps/
        
        if [ ! -e Makefile.orig ]; then
            cp Makefile Makefile.orig
        fi
        
#    sed -e 's/dbdpgsql/pgsql/g' Makefile.orig > Makefile 
        
        cd -
        
        CFLAGS=-D_LARGEFILE64_SOURCE make
        
        sudo make install
        
    # Build the client
        
#    cd Open-ILS/xul/staff_client
        
#    make STAFF_CLIENT_BUILD_ID='12345'
#    mkdir -p /openils/var/web/xul/12345/
#    cd build/
#    cp -R server/ /openils/var/web/xul/12345/
        
        cd -
        
        FLAG_STATUS[$id]=true
    else
        echo "Evergreen has been built and configured . . ."
    fi
}


remove_evergreen () {
    cd /
    rm -rf openils
    cd -

    rm *.tar.gz
    rm -rf Evergreen*
    rm -rf OpenSRF*

    deluser --remove-all-files --group opensrf
    rm -rf /etc/apache2
    rm -rf /etc/postgresql
    rm -rf /var/log/postgresql

    rm -rf /var/lib/postgresql
}

setup_apache () {

    find_flag_id "SETUP_APACHE"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Setting up apache . . ."
        
        cd Evergreen-ILS-1.2.1.0/ 
        sudo cp Open-ILS/examples/apache/*.conf /etc/apache2/sites-available/
        sudo cp Open-ILS/examples/apache/startup.pl /etc/apache2/
        
        cd /etc/apache2/sites-available/
        
        if [ ! -e eg.conf.orig ]; then
            cp eg.conf eg.conf.orig
        fi
        
        sed -e '60,60s/10.0.0.0\/8/127.0.0.0\/16/;/ServerName/s/localhost/'$HOSTNAME'/g;s/eg_vhost.conf/sites-available\/eg_vhost.conf/g' eg.conf.orig > eg.conf
        
        cd /etc/apache2
        if [ ! -e apache2.conf.orig ]; then
            cp apache2.conf apache2.conf.orig
        fi
        
        sed -e 's/www-data/opensrf/g' apache2.conf.orig > apache2.conf
        
        sudo chmod 755 /etc/apache2/startup.pl
        
        sudo a2ensite eg.conf
        sudo a2dissite 000-default
        
        
#    Enable the needed apache2 modules:
        
        APACHE_MODULES="deflate expires include rewrite ssl proxy disk_cache"
        
        for module in $APACHE_MODULES; do
            sudo a2enmod $module
        done
        
#    sudo /etc/init.d/apache2 force-reload
        
        
#    Create a basic ssl certificate:
        sudo mkdir /etc/apache2/ssl
        cd /etc/apache2/ssl
        sudo openssl req -new -x509 -nodes -out server.crt -keyout server.key
        
    # Create a file for telling about the shared libraries for evergreen
        
        cd /etc/ld.so.conf.d
        echo "/usr/lib/dbd" >> evergreen.conf
        echo "/openils/lib" >> evergreen.conf
        
        sudo ldconfig
        
        
    # remove the 443 info in the /etc/apache2/ports.conf
        cd /etc/apache2/
        
        if [ -e ports.conf ]; then
            mv ports.conf ports.conf.orig
            echo "Listen 80" > ports.conf
        fi
        
        FLAG_STATUS[$id]=true
    else
        echo "Apache has been setup and configured . . . "
    fi
    
}


setup_ejabberd () {


    find_flag_id  "SETUP_EJABBERD"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Setting up ejabberd . . ."
        
        cd /etc/ejabberd
        
        if [ ! -e ejabberd.cfg.orig ]; then
            cp ejabberd.cfg ejabberd.cfg.orig
        fi
        
        
        sed -e '/^{acl/s/"",[[:space:]]"localhost"/"router"/;/^{acl/a\{acl, admin, {user, "opensrf"}}.' -e 's/max_user_sessions,[[:space:]]10/max_user_sessions, 1000/; s/hosts,[[:blank:]]\["localhost"\]/hosts, \["localhost", "'$HOSTNAME'"\]/g;s/max_stanza_size,[[:blank:]][[:digit:]]*/max_stanza_size, 2000000/g;s/maxrate,[[:blank:]][[:digit:]]*/maxrate, 500000/g;s/  {mod_offline/% {mod_offline/' ejabberd.cfg.orig > ejabberd.cfg
        
        /etc/init.d/ejabberd restart
        
        sleep 5
        
        if [ -z $ROUTER_JAB_PW ]; then
            read -p "Enter the password for the 'router' account: "
            ROUTER_JAB_PW=$REPLY
        fi
        ejabberdctl register router $HOSTNAME $ROUTER_JAB_PW
        
        sleep 5

        if [ -z $OPENSRF_JAB_PW ]; then
            read -p "Enter the password for the 'opensrf' account: "
            OPENSRF_JAB_PW=$REPLY
        fi
        
        ejabberdctl register opensrf $HOSTNAME $OPENSRF_JAB_PW
        
        sleep 5
        
        ejabberdctl restart
        
        sleep 5
        
        FLAG_STATUS[$id]=true
    else
        echo "Ejabberd has been setup and 2 user accounts installed . . ."
    fi
}

configure_evergreen () {

    find_flag_id  "CONFIGURE_EVERGREEN"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        echo "Configuring Evergreen . . . "
        
        cd /openils/

        cp conf/opensrf_core.xml.example conf/opensrf_core.xml
        cp conf/opensrf.xml.example conf/opensrf.xml
        
        cd conf
        
        if [ ! -e opensrf_core.xml.orig ]; then
            cp opensrf_core.xml opensrf_core.xml.orig
        fi
        if [ ! -e opensrf.xml.orig ]; then
            cp opensrf.xml opensrf.xml.orig
        fi
        
        
    #  In the opensrf.xml, the username = evergreen, password = everPass
        
        sed -e '50,50s/localhost/'$HOSTNAME'/;s/<host>localhost<\/host>/<host>'$HOSTNAME'<\/host>/g;s/<user>postgres<\/user>/<user>'$PG_DB_USER'<\/user>/g;s/<password>postgres<\/password>/<password>'$PG_DB_USER_PW'<\/password>/g;s/<pw>postgres<\/pw>/<pw>'$PG_DB_USER_PW'<\/pw>/g;s/evergreen.example.org/'$HOSTNAME'/g' opensrf.xml.orig > opensrf.xml
        
    #  In the opensrf_core.xml, the username = opensrf, passwd = everPass
        
        sed -e '15,15s/localhost/'$HOSTNAME'/;21,21s/localhost/'$HOSTNAME'/;51,51s/localhost/'$HOSTNAME'/;74,74s/localhost/'$HOSTNAME'/;s/<username>client<\/username>/<username>opensrf<\/username>/;s/<passwd>mypass<\/passwd>/<passwd>'$OPENSRF_JAB_PW'<\/passwd>/;s/mylogin/opensrf/;s/<passwd>mypassword<\/passwd>/<passwd>'$OPENSRF_JAB_PW'<\/passwd>/;100,137s/localhost/'$HOSTNAME'/g;125,125s/mypassword/'$OPENSRF_JAB_PW'/' opensrf_core.xml.orig > opensrf_core.xml
        
        mkdir -p /openils/var/reporter
        
        chmod 755 /openils/var/cgi-bin/*.cgi
        
        cd /openils/bin
        
        if [ ! -e autogen.sh.orig ]; then
            cp autogen.sh autogen.sh.orig
        fi
        if [ ! -e osrf_ctl.sh.orig ]; then
            cp osrf_ctl.sh osrf_ctl.sh.orig
        fi
        
        for f in autogen.sh osrf_ctl.sh; do
            sed -e '1,1a\export PATH=$PATH:/openils/bin' -e '1,1a\export PERL5LIB=/openils/lib/perl5' -e '1,1a\export LD_LIBRARY_PATH=/openils/lib'  $f.orig > $f
        done
        
        
        sed -e 's/127.0.0.1/'$HOSTNAME'/' -e 's/myusername/opensrf/;s/mypassword/'$OPENSRF_JAB_PW'/' -e 's#/path/to/log#'$HOME'#' /openils/conf/srfsh.xml.example > $HOME/.srfsh.xml
        
        sed -e 's/127.0.0.1/'$HOSTNAME'/' -e 's/myusername/opensrf/;s/mypassword/'$OPENSRF_JAB_PW'/' -e 's#/path/to/log/#/home/opensrf/#' /openils/conf/srfsh.xml.example > /home/opensrf/.srfsh.xml
        
        chown -R opensrf.opensrf /home/opensrf
        chown -R opensrf.opensrf /openils
        
        FLAG_STATUS[$id]=true
    else
        echo "Evergreen has been configured . . ."
    fi
}
    
finalize_opensrf () {

    find_flag_id  "FINALIZE_OPENSRF"

    if [ ${FLAG_STATUS[$id]} = false ]; then

        cd $HOME
        
        echo "Finalizing opensrf . . . "
        
        sudo -u opensrf /openils/bin/osrf_ctl.sh -c /openils/conf/opensrf_core.xml -a start_router
        sudo -u opensrf /openils/bin/osrf_ctl.sh -c /openils/conf/opensrf_core.xml -a start_perl
        sudo -u opensrf /openils/bin/osrf_ctl.sh -c /openils/conf/opensrf_core.xml -a start_c
        
        cd /openils/bin/
        sudo -u opensrf ./autogen.sh /openils/conf/opensrf_core.xml
        cd -
        
        sudo /etc/init.d/apache2 restart
        
## check the evergreen database with the following command:
        
##   update actor.usr set passwd = 'open-ils' where usrname = 'admin';
        
        FLAG_STATUS[$id]=true
    else
        echo "Opensrf has been started . . . "
    fi
}


install () {

    initialize_flags

    setup_hostname

    install_dependencies

    build_install_perl_modules

    install_dbd_pkgs

    ## Configure the packages

    setup_postgresql
    install_opensrf
    install_evergreen
   
    setup_apache

    setup_ejabberd
    configure_evergreen
    finalize_opensrf

    check_install_status

}

uninstall () {
    echo "Removing the test subset of packages . . ."

    create_packages

    sudo -u opensrf /openils/bin/osrf_ctl.sh -c /openils/conf/opensrf_core.xml -a stop_all 

    su -c "dropdb $PG_DB_NAME" - postgres

    apt-get remove -y $PKGS
    remove_perl_modules

#    remove_apt_source_unstable

    remove_dbd_pkgs

    remove_evergreen

    dpkg --get-selections | grep deins | cut -f 1 | xargs dpkg -P

    apt-get install sudo

}




setup_hostname () {

    if [ -z $HOST_NAME ]; then
        read -p "Enter hostname = "
        
        if [ ! -z $REPLY ]; then
            HOSTNAME=$REPLY
        else
            HOSTNAME=`hostname`
            
        fi
    fi
    IP=`hostname -i`
    echo $IP
}


setup_accounts () {

    echo "The complete install of Evergreen requires the creation of several"
    echo "accounts that require passwords."
    echo "\n"
    echo ""


}

resume () {
    
    echo "Resume not implemented . . . exiting . . . ."
    exit 1;

}

check_install_status () {
    
    echo "Checking the status of the install . . ."

    for ((i=0; i< ${#FLAG[*]};i++)); do
        echo "${FLAG[i]} = ${FLAG_STATUS[i]} "
    done
    
    echo "Please execute the following: '/openils/bin/srfsh' "
    echo "At the 'srfsh#' type the following: login admin open-ils"
    echo "You should see a successful login."
    echo "Type 'exit' when you are done."

}

while getopts "i u r p b d o e a j c f h: v:" options; do
    case $options in
        i)
            DO_INSTALL=true
            ;;
        u)
            DO_UNINSTALL=true
            ;;
        r)
            DO_RESUME=true
            ;;
        p)
            BUILD_PERL_MODULES=true
            ;;
        b)
            INSTALL_DBD=true
            ;;
        d)
            SETUP_POSTGRESQL=true
            setup_postgresql;
            ;;
        o)
            INSTALL_OPENSRF=true
            install_opensrf
            ;;
        e)
            INSTALL_EVERGREEN=true
            install_evergreen
            ;;
        a)
            SETUP_APACHE=true
            setup_apache
            ;;
        j)
            SETUP_EJABBERD=true
            setup_ejabberd
            ;;
        c)
            CONFIGURE_EVERGREEN=true
            configure_evergreen
            ;;
        f)
            FINALIZE_OPENSRF=true
            finalize_opensrf
            ;;
        h)
            HOST_NAME=$OPTARG
            echo $HOST_NAME
            ;;
        v)
            DISTRIBUTION=$OPTARG
            ;;
        *)
            echo "$arg not valid"
            exit 1
            ;;
    esac
done


if [ $DO_INSTALL = true ]; then
    install
fi

if [ $DO_UNINSTALL = true ]; then
    uninstall
fi

if [ $DO_RESUME = true ]; then
    resume
fi
    
exit


### TODO

## 0.  Usage line
## 1.  Install Log
## 2.  Resume installation
## 4.  Specify version of opensrf -- stable or development
## 5.  Specify the password for postgres -- not use default password
## 7.  Figure out how to use apt-cacher or something to hold the deb packages.
## 10. Assign evergreen a password in the database.





## Deb ins Bill Erickson's script -- not found in John Schmidt's script

EXTRA_PKGS="build-essential syslog-ng ntpdate postgresql-client libexpat1-dev \
libperl-dev libclass-dbi-pg-perl libclass-dbi-sqlite-perl libnet-jabber-perl \
libtest-pod-perl libfile-find-rule-perl librpc-xml-perl libpq-dev \
libemail-send-perl libtool libtie-ixhash-perl"


