#!/bin/bash

#----------------------------------------------------------------------------#
#   Qmail The Easy Way v1.3 - distributed under the General Public License   #
#                                README                                      #
#                          September 5, 2001                                 #
#----------------------------------------------------------------------------#


#Declaring global Variables
ans1=y
ans2=Y
ans3=n
ans4=N

#Declaring global variables for app download locations
daemontools_download=http://qmailtheeasyway.com/downloads/daemontools-0.76.tar.gz
ucspi_tcp_download=http://qmailtheeasyway.com/downloads/ucspi-tcp-0.88.tar.gz
qmail_download=http://qmailtheeasyway.com/downloads/qmail-1.03.tar.gz
rblsmtpd_download=http://qmailtheeasyway.com/downloads/rblsmtpd-0.70.tar.gz
fastforward_download=http://qmailtheeasyway.com/downloads/fastforward-0.51.tar.gz
djbdns_download=http://qmailtheeasyway.com/downloads/djbdns-1.05.tar.gz
checkpassword_download=http://qmailtheeasyway.com/downloads/checkpassword-0.90.tar.gz

#Declaring global variables for Sqwebmail app download locations
tcl_download=http://qmailtheeasyway.com/downloads/tcl8.4a3.tar.gz
expect_download=http://qmailtheeasyway.com/downloads/expect.tar.gz
courier_download=http://qmailtheeasyway.com/downloads/courier-imap-1.3.10.20010830.tar.gz
sqwebmail_download=http://qmailtheeasyway.com/downloads/sqwebmail-3.0.0.20010827.tar.gz

start ( )
 {
  clear
  echo " "
  echo " "
  echo "                 +++++++++++++++++++++++++++++++++++++"
  echo "                      Welcome to Qmail The Easy Way      "
  echo "                 +++++++++++++++++++++++++++++++++++++"
  echo "  "
  echo "  --> This script is distributed under the auspices of the GNU/GPL."
  echo " "
  echo "  --> My prime reason for writing this script is to make it easier"
  echo "      for people to install DJB's tools from Source....."
  echo " "
  echo "  --> This may not be the best DJB installer around, but i guess "
  echo "      with enough of review from you Open Source guys...i'll improve" 
  echo "      it as much as i can....'-)"
  echo " "
  echo "  --> Pliss untar the file in the /tmp and run this script from there."
  echo " "
  echo " "
  echo  "                                   Trevor Warren "
  echo  "                               thefreeosguy@yahoo.com    "
  echo " " 
  echo " "
  sleep 10
 }

stop ( )
 {
  echo " "
  echo " "
  echo "          +++++++++++++++++++++++++++++++++++++++"
  echo "            Thanks for using Qmail The Easy Way     "
  echo "          +++++++++++++++++++++++++++++++++++++++"
  echo " "
  echo " "
 }

check_wget ( )
 {
  
  echo " "
  echo "  --> Testing for a wget installation"
  test_wget=`which wget`
  if [ $test_wget = /usr/bin/wget ]
    then
     echo " "
     echo "  --> Your Wget installation seems perfect. "
     echo " "
    else
    echo " "
    echo "  --> You do not seem to have a Wget Installation on your"
    echo "      machine. Hence, you will not be able to download"
    echo "      any qmail dependencies from the Internet."
    exit
    echo " "
  fi

 }

check_daemontools ( )
 {

#Starting to check for Daemontools

echo "  --> Checking for the daemontools tarball"
daemontools_there=`ls | grep -i daemontools*.gz`
if [ -z $daemontools_there ]
 then
  echo " "
  echo "  --> Couldn't locate the daemontools Tarball"
  echo " "
  echo "  --> Would you like to download the daemontools Tarball."
  echo "      Enter either  y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $daemontools_download
      d_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the daemontools tarball,"
      echo "      this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
      d_there=n
    fi
 else
  echo " "
  echo "  --> Located the daemontools Tarball as $daemontools_there"
  echo " "
  echo " "
  d_there=y
fi

# End of Daemontools test

}

check_ucspi_tcp ( )
{

#Start of ucspi-tcp check

echo "  --> Checking for the ucspi-tcp tarball"
ucspi_tcp_there=`ls | grep -i ucspi-tcp*.gz`
if [ -z $ucspi_tcp_there ]
 then
  echo " "
  echo "  --> Couldn't locate the ucspi-tcp Tarball"
  echo " "
  echo "  --> Would you like to download the ucspi-tcp Tarball."
  echo "      Enter either y or n."
  read answer6
    if [ $answer6 = $ans1 -o $answer6 = $ans2 ]
     then
      echo " "
      wget -c -v -t0  $ucspi_tcp_download
      u_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the ucspi-tcp tarball,"
      echo "      this  will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
      u_there=n
    fi
 else
  echo " "
  echo "  --> Located the ucspi-tcp Tarball as $ucspi_tcp_there"
  echo " "
  echo " "
  u_there=y

fi

#End of uscpi-tcp check

 }

check_qmail ( )
 {
#Starting Check for qmail tarball

echo "  --> Checking for the qmail tarball"
qmail_there=`ls | grep -i qmail-1*.gz`
if [ -z $qmail_there ]
 then
  echo " "
  echo "  --> Couldn't locate the qmail Tarball"
  echo " "
  echo "  --> Would you like to download the qmail Tarball."
  echo "      Enter either y or n."
   read answer1
    if [ $answer1 = $ans1 -o $answer1 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $qmail_download
      q_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the qmail tarball,"
      echo "      this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
      q_there=n
     fi
else
  echo " "
  echo "  --> Located the qmail Tarball as $qmail_there"
  echo " "
  echo " "
  q_there=y  

fi
#Ending check of qmail tarball

 }

check_rblsmtpd ( )
 {

#Starting rblsmtpd check

echo "  --> Checking for the rblsmtpd tarball"
rblsmtpd_there=`ls | grep -i rblsmtpd*.gz`
if [ -z $rblsmtpd_there ]
 then
  echo " "
  echo "  --> Couldn't locate the rblsmtpd Tarball"
  echo " "
  echo "  --> Would you like to download the rblsmtpd Tarball."
  echo "      Enter either y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $rblsmtpd_download
      r_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the rblsmtpd tarball,"
      echo "      this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
      r_there=n
     fi
 else
  echo " "
  echo "  --> Located the rblsmtpd Tarball as $rblsmtpd_there"
  echo " "
  echo " "
  r_there=y

fi 

#Ending rblsmtpd check
 }

check_fastforward ( )
 {

# Start fastforward check
echo "  --> Checking for the fastforward tarball"
fastforward_there=`ls | grep -i fastforward*.gz`
if [ -z $fastforward_there ]
 then
  echo " "
  echo "  --> Couldn't locate the fastforward Tarball"
  echo " "
  echo "  --> Would you like to download the fastforward Tarball."
  echo "      Enter either yor n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $fastforward_download
      f_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the fastforward tarball,"
      echo "      this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory "
      echo "      and run this script again."
      echo " "
      f_there=n
    fi
 else
  echo " "
  echo "  --> Located the fastforward Tarball as $fastforward_there"
  echo " "
  echo " "
  f_there=y
fi

#End of fastforward check
 }

check_djbdns ( )
 {

#Start of djb dns check

echo " "
echo " "
echo "  --> Checking for the djbdns tarball"
djbdns_there=`ls | grep -i djbdns-1`
if [ -z $djbdns_there ]
 then
  echo " "
  echo "  --> Couldn't locate the djbdns Tarball"
  echo " "
  echo "  --> Would you like to download the djbdns Tarball."
  echo "      Enter either y or n."
  read answer2
    if [ $answer2 = $ans1 -o $answer2 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $djbdns_download
      j_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the djbdns tarball,"
      echo "      this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
     j_there=n
    fi
 else
  echo " "
  echo "  --> Located the djbdns Tarball as $djbdns_there"
  echo " "
  echo " "
  j_there=y
fi

#End of djb dns check

 }

check_checkpassword ( )
 {

#Start of checkpassword check

echo "  --> Checking for the Checkpassword tarball"
checkpassword_there=`ls | grep -i checkpassword*.gz`
if [ -z $checkpassword_there ]
 then
  echo " "
  echo "  --> Couldn't locate the Checkpassword Tarball"
  echo " "
  echo "  --> Would you like to download the Checkpassword Tarball."
  echo "      Enter either y or n."
  read answerCP
   if [ $answerCP = $ans1 -o $answerCP = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $checkpassword_download
      c_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the Checkpassword"
      echo "      tarball, this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball"
      echo "      from the designated site and place it in this directory"
      echo "      and run this script again."
      echo " "
      c_there=n
    fi
 else
  echo " "
  echo "  --> Located the Checkpassword Tarball as $checkpassword_there"
  echo " "
  echo " "
  c_there=y
fi

#End of checkpassword check

}


daemontools_install ( )
 {

if [ $d_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the daemontools Source"
  sleep 5
  tar -xvzf `ls | grep -i daemontools*.gz`
  cd admin
  cd `ls | grep daemon`

 # Starting Compilation of daemontools
  ./package/install
  cd ../..
  #rm -fr ./admin

fi


}

ucspi_tcp_install ( )
 {

if [ $u_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the ucspi_tcp Source"
  sleep 5
  tar -xvzf `ls | grep -i ucspi-tcp*.gz`
  cd `ls | grep -i ucspi*.gz | cut -d . -f1,2` 

 # Starting Compilation of daemontools
  make
  make setup check
  cd ..
  rm -fr ./`ls | grep -i ucspi*.gz | cut -d . -f1,2`

 fi

}



qmail_install ( )
 { 

if [ $q_there=y ]
 then

#Starting compilation and Installation from source  

#Creating the home directory for qmail
  mkdir /var/qmail
#Creating non system user accounts for qmail daemons
    groupadd nofiles
    useradd alias -g nofiles -d /var/qmail/alias -s /bin/false
    useradd qmaild -g nofiles -d /var/qmail -s /bin/false
    useradd qmaill -g nofiles -d /var/qmail -s /bin/false
    useradd qmailp -g nofiles -d /var/qmail -s /bin/false
    groupadd qmail
    useradd qmailq -g qmail -d /var/qmail -s /bin/false
    useradd qmailr -g qmail -d /var/qmail -s /bin/false
    useradd qmails -g qmail -d /var/qmail -s /bin/false

  echo "  --> Untarring the qmail Source"
  sleep 5
  tar -xvzf `ls | grep -i qmail-1*.gz`
  cd ./`ls | grep -i qmail-1*.gz | cut -d . -f1,2`
  
 # Starting Compilation of qmail
  make 
  make setup check
  echo " "
  echo "  --> Enter your hostname+domain name eg geeky.freeos.com."
  read d_name
  ./config-fast $d_name
  cd ..
  rm -fr ./`ls | grep -i qmail-1*.gz | cut -d . -f1,2`

 # Starting with configuration of qmail

  if [ -d /etc/rc.d/init.d ]
    then
     echo "  --> /etc/rc.d/init.d found, continuing installation normally. " 
    else 
     echo "  --> /etc/rc.d/init.d not found, symlinking init.d to /etc/rc.d."
     ln -s /etc/rc.d /etc/rc.d/init.d     
  fi    

  cp ./qmail_needed/rc /var/qmail
  chmod 755 /var/qmail/rc
 
  cp ./qmail_needed/.qmail /etc/skel
  echo ./Maildir/ >/var/qmail/control/defaultdelivery
  /var/qmail/bin/maildirmake /etc/skel/Maildir 
  
  cp ./qmail_needed/qmailctl /var/qmail/bin 
  chmod 755 /var/qmail/bin/qmailctl
  ln -s /var/qmail/bin/qmailctl /usr/bin
  ln -s /var/qmail/bin/qmailctl /etc/rc.d/init.d/qmail

if [ -d /etc/rc.d/init.d/rc0.d ]
 then 
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc0.d/K30qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc1.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc2.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc3.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc4.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc5.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/init.d/rc6.d/K30qmail
 else
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc0.d/K30qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc1.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc2.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc3.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc4.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc5.d/S80qmail
  ln -s /etc/rc.d/init.d/qmail  /etc/rc.d/rc6.d/K30qmail
fi 

  mkdir -p /service/qmail-send
  mkdir -p /service/qmail-send/log
  mkdir -p /service/qmail-smtpd
  mkdir -p /service/qmail-smtpd/log
  
  chmod +t /service/qmail-send
  chmod +t /service/qmail-send/log
  chmod +t /service/qmail-smtpd
  chmod +t /service/qmail-smtpd/log

  cp ./qmail_needed/qsend_run  /service/qmail-send/run
  chmod 755 /service/qmail-send/run
  cp ./qmail_needed/qsendlog_run  /service/qmail-send/log/run
  chmod 755 /service/qmail-send/log/run
  
  cp ./qmail_needed/qsmtpd_run  /service/qmail-smtpd/run
  chmod 755 /service/qmail-smtpd/run
  cp ./qmail_needed/qsmtpdlog_run  /service/qmail-smtpd/log/run
  chmod 755 /service/qmail-smtpd/log/run

  mkdir /var/qmail/log
  mkdir /var/qmail/log/smtpd
  chown qmaill /var/qmail/log
  chown qmaill /var/qmail/log/smtpd

  echo 20 > /var/qmail/control/concurrencyincoming
  chmod 644 /var/qmail/control/concurrencyincoming
  
  echo '127.:allow,RELAYCLIENT=""' >>/etc/tcp.smtp
  echo '$part1.$part2.:allow,RELAYCLIENT=""' >>/etc/tcp.smtp
  qmailctl cdb

  mv /usr/lib/sendmail /usr/lib/sendmail.old                  # ignore errors
  mv /usr/sbin/sendmail /usr/sbin/sendmail.old                # ignore errors
# chmod 0 /usr/lib/sendmail.old /usr/sbin/sendmail.old        # ignore errors
  ln -s /var/qmail/bin/sendmail /usr/lib
  ln -s /var/qmail/bin/sendmail /usr/sbin
 
  echo "  --> Enter the email id of the person who'll recieve mail for root."
  read root_alias
  echo $root_alias > /var/qmail/alias/.qmail-root
  echo $root_alias > /var/qmail/alias/.qmail-postmaster
  echo $root_alias > /var/qmail/alias/.qmail-mailer-daemon
  chmod 644 /var/qmail/alias/.qmail-root
  chmod 644 /var/qmail/alias/.qmail-postmaster
  chmod 644 /var/qmail/alias/.qmail-mailer-daemon

# Adding the qmail-pop3d daemon
   mkdir  /service/qmail-pop3d
   mkdir  /service/qmail-pop3d/log
   chmod +t /service/qmail-pop3d/
   chmod +t /service/qmail-pop3d/log/  

   cp ./qmail_needed/qpop3d_run /service/qmail-pop3d/run
   chmod 755 /service/qmail-pop3d/run
   cp ./qmail_needed/qpop3dlog_run  /service/qmail-pop3d/log/run 
   mkdir /var/qmail/log/pop3d
   chown qmaill /var/qmail/log/pop3d/

fi

}

rblsmtpd_install ( )
 {
 
if [ $r_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the rblsmtpd Source"
  sleep 5
  tar -xvzf `ls | grep -i rblsmtpd*.gz`
  cd `ls | grep -i rblsmtpd*.gz | cut -d . -f1,2`

 # Starting Compilation of rblsmtpd
  make
  make setup check
  cd ..
  rm -fr ./`ls | grep -i rblsmtpd*.gz | cut -d . -f1,2`

fi

 }

fastforward_install ( )
 {

if [ $f_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the fastforward Source"
  sleep 5
  tar -xvzf `ls | grep -i fastforward*.gz`
  cd `ls | grep -i fastforward*.gz | cut -d . -f1,2`

 # Starting Compilation of rblsmtpd
  make
  make setup check
  cd ..
  rm -fr ./`ls | grep -i fastforward*.gz | cut -d . -f1,2`

fi

 }


djbdns_install ( )
 {

if [ $j_there=y ]
 then
#Starting compilation and Installation from source

  echo "  --> Untarring the djbdns Source"
  sleep 5
  tar -xvzf `ls | grep -i djbdns-1`
  cd ./`ls | grep -i djbdns-1 | cut -d . -f1,2`

 # Starting Compilation of djb dns
  make
  make setup check
  cd ..
  rm -fr ./`ls | grep -i djbdns-1 | cut -d . -f1,2`


dns_configure ( )
 {

  groupadd dns
  useradd tinydns -g dns -d /etc/tinydns -s /bin/false
  useradd dnslog -g dns -d /etc/tinydns -s /bin/false
  echo " "
  echo " "
  echo "  --> Enter the IP address of your machine."
  read answer3
  tinydns-conf tinydns dnslog /etc/tinydns $answer3
  export answer3
  if [ -d /service ]
   then
   echo " "
   echo "  --> /service exists."
   ln -s /etc/tinydns tinydns
   mv tinydns /service
   echo " "
  else
    echo "  --> /service doesn't exist."
    echo " "
    echo "  --> creating /service."
    echo " "
    mkdir /service
    ln -s /etc/tinydns tinydns
    mv tinydns /service
  fi

 }

dns_postconfigure ( )
 {

  echo " "
  echo "  --> Enter the hostname+domain name(bom.freeos.com) of your machine."
  read answer5

#Separating the 32 bit IP address into 4 8 bit parts
  part1=`echo $answer3 | cut -d . -f1`
  part2=`echo $answer3 | cut -d . -f2`
  part3=`echo $answer3 | cut -d . -f3`
  part4=`echo $answer3 | cut -d . -f4`

  temp_dir=`pwd`
  cd /etc/tinydns/root
  ./add-ns $answer5 $answer3
  ./add-ns $part3.$part2.$part1.in-addr.arpa $answer3
  ./add-host $answer5 $answer3
  make
  echo " "
  echo " "
  cd $temp_dir
 
# Exporting IP address for use in qmail installation
  export part1
  export part2
  export part3
  export part4

# Moving existing /etc/resolv.conf to /etc/resolv/conf.bak
mv /etc/resolv.conf /etc/resolv.conf.bak
touch /etc/resolv.conf
echo "nameserver $answer3" > /etc/resolv.conf
cat /etc/resolv.conf.bak >> /etc/resolv.conf

}

dns_configure
dns_postconfigure

fi

}

checkpassword_install ( )
 {

if [ $c_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the Checkpassword Source"
  sleep 5
  tar -xvzf `ls | grep -i checkpassword*.gz`
  cd `ls | grep -i checkpassword*.gz | cut -d . -f1,2`

 # Starting Compilation of daemontools
  make
  make setup check
  cd ..
  rm -fr ./`ls | grep -i checkpassword*.gz | cut -d . -f1,2`

fi

}

# Starting Web Based Mail installation.

check_tcl ( )
 {

#Starting to check for TCL

echo "  --> Checking for the TCL tarball"
tcl_there=`ls | grep -i tcl*.gz`
if [ -z $tcl_there ]
 then
  echo " "
  echo "  --> Couldn't locate the TCL Tarball"
  echo " "
  echo "  --> Would you like to download the TCL Tarball. Enter either
 y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $tcl_download
      tl_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the TCL tarball, this will cause an incomplete installation of qmail Web Mail."
      echo "  --> Another option for you is to download the source tarball from
the designated site and place it in this directory and run this script again."
      echo " "
      tl_there=n
    fi
 else
  echo " "
  echo "  --> Located the TCL Tarball as $tcl_there"
  echo " "
  echo " "
  tl_there=y
fi

# End of TCL test


 }
 
check_expect ( )
  {

#Starting to check for Expect
echo "  --> Checking for the Expect tarball"
expect_there=`ls | grep -i expect*.gz`
if [ -z $expect_there ]
 then
  echo " "
  echo "  --> Couldn't locate the Expect Tarball"
  echo " "
  echo "  --> Would you like to download the Expect Tarball. Enter either
 y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0  $expect_download
      ex_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the Expect tarball, this will cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball from
the designated site and place it in this directory and run this script again."
      echo " "
      ex_there=n
    fi
 else
  echo " "
  echo "  --> Located the Expect Tarball as $expect_there"
  echo " "
  echo " "
  ex_there=y
fi

# End of Expect test

  }


check_cimap ( )
 {

#Starting to check for Courier Imap

echo "  --> Checking for the Courier Imap tarball"
cimap_there=`ls | grep -i courier*.gz`
if [ -z $cimap_there ]
 then
  echo " "
  echo "  --> Couldn't locate the Courier Imap Tarball"
  echo " "
  echo "  --> Would you like to download the Courier Imap Tarball. Enter either
 y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 $courier_download
      ci_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the Courier Imap tarball, thiswill cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball from
the designated site and place it in this directory and run this script again."
      echo " "
      ci_there=n
    fi
 else
  echo " "
  echo "  --> Located the Courier Tarball as $cimap_there"
  echo " "
  echo " "
  ci_there=y
fi

# End of Courier Imap test

 }

 check_sqwebmail ( )
 {

#Starting to check for Sqwebmail

echo "  --> Checking for the Sqwebmail tarball"
sqb_there=`ls | grep -i sqwebmail*.gz`
if [ -z $sqb_there ]
 then
  echo " "
  echo "  --> Couldn't locate the Sqwebmail Tarball"
  echo " "
  echo "  --> Would you like to download the Sqwebmail Tarball. Enter either
 y or n."
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0  $sqwebmail_download
      sq_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the Sqwebmail tarball, thiswill cause an incomplete installation of qmail."
      echo "  --> Another option for you is to download the source tarball from
the designated site and place it in this directory and run this script again."
      echo " "
      sq_there=n
    fi
 else
  echo " "
  echo "  --> Located the Sqwebmail Tarball as $sqb_there"
  echo " "
  echo " "
  sq_there=y
fi

# End of Sqwebmail test

 }

tcl_install ( )
 {

if [ $tl_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the TCL Source"
  sleep 5
  tar -xvzf `ls | grep -i tcl*.gz`
  cd `ls | grep -i tcl*.gz | cut -d . -f1,2`
  cd unix
 
 # Starting Compilation of TCL
  ./configure
  make
  make install
  cd ../..

# Deleting the TCL source directory after installation of Expect
# rm -fr ./`ls | grep -i tcl*.gz | cut -d . -f1,2`

fi

 }

expect_install ( )
 {

if [ $ex_there=y ]
 then
#Starting compilation and Installation from source
  tcl_location=`ls | grep -i tcl*.gz | cut -d . -f1,2`
  echo "  --> Untarring the Expect Source"
  sleep 5
  tar -xvzf `ls | grep -i expect*.gz`
  cd expect-*

 # Starting Compilation of expect
  ./configure --with-tcl=../$tcl_location/unix
  make
  make install
  cd ..

  rm -fr ./`ls | grep -i fastforward*.gz | cut -d . -f1,2`
  rm -fr ./`ls | grep -i tcl*.gz | cut -d . -f1,2`

fi

 }

courier_install ( )
 {

if [ $ci_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the Courier Imap Source"
  sleep 5
  tar -xvzf `ls | grep -i courier*.gz`
  cd courier-imap*

# Starting Compilation of Courier Imap
  echo " "
  echo " --> The installation of Courier Imap requires either GDBM/DB - either #the GDBM or the Berkeley DB library is required."
  echo " --> If the configure script screams and aborts abruptly, pliss install either the gd library or gdbm and re-run this script."
  echo " "
  echo "                      V.V.IMP "
  echo " --> Courier Imap cannot be built as ROOT, pliss supply a normal user id on this machine to cont
inue. If you have downloaded the scripts to say e.g. /home/geekybabe or /home/geekybabe/xyz/ , then give
 the username as geekybabe. If you are running this script from /tmp then any username will do."
  read username
  su $username -c ./configure;make
  make install     
  make install-configure 
  cd ..
  rm -fr ./courier-imap*
  
if [ -f /etc/rc.d/boot.local ]
  then
   echo "/usr/lib/courier-imap/libexec/imapd.rc start" >> /etc/rc.d/boot.local
  else 
   echo "/usr/lib/courier-imap/libexec/imapd.rc start" >> /etc/rc.d/rc.local
fi

fi

}


sqwebmail_install ( )
 {

if [ $sq_there=y ]
 then
#Starting compilation and Installation from source
  echo "  --> Untarring the Sqwebmail Source"
  sleep 5
  tar -xvzf `ls | grep -i sqwebmail*.gz`
  cd sqwebmail-*

 # Starting Compilation of Sqwebmail
 if [ -d /var/www ]
  then
  ./configure --with-cachedir=/var/cache/sqwebmail --enable-imagedir=/var/www/html/webmail --enable-cgibindir=/var/www/html/webmail --with-module=authpam
# ./configure --with-cachedir=/var/cache/sqwebmail --enable-imagedir=/var/www/html/webmail --enable-cgibindir=/var/www/html/webmail 
  else
    ./configure --with-cachedir=/var/cache/sqwebmail --enable-imagedir=/usr/local/httpd/htdocs/webmail --enable-cgibindir=/usr/local/httpd/htdocs/webmail --with-module=authpam
 fi
  make
  make install
  make install-configure
  cd ..
  rm -fr ./sqwebmail-*

 if [ -f /etc/rc.d/boot.local ]
  then
   echo "/usr/local/share/sqwebmail/libexec/authlib/authdaemond start
" >> /etc/rc.d/boot.local
  mv /usr/local/share/sqwebmail/libexec/authlib/authdaemond.ldap  /usr/local/share/sqwebmail/libexec/authlib/authdaemond.ldap_bak 
 else
   echo "/usr/local/share/sqwebmail/libexec/authlib/authdaemond start
" >> /etc/rc.d/rc.local
     mv /usr/local/share/sqwebmail/libexec/authlib/authdaemond.ldap  /usr/local/share/sqwebmail/libexec/authlib/authdaemond.ldap_bak
fi

if [ -f /etc/httpd/httpd.conf ]
 then
  mkdir /usr/local/httpd/htdocs/webmail_cgi
  cp  /usr/local/httpd/htdocs/webmail/sqwebmail /usr/local/httpd/htdocs/webmail_cgi
  echo " " >> /etc/httpd/httpd.conf
  echo " " >> /etc/httpd/httpd.conf
  echo "<Directory "/usr/local/httpd/htdocs/webmail_cgi">" >> /etc/httpd/httpd.conf
  echo "AllowOverride None" >> /etc/httpd/httpd.conf
  echo "Options None" >> /etc/httpd/httpd.conf
  echo "Order allow,deny" >> /etc/httpd/httpd.conf
  echo "Allow from all" >> /etc/httpd/httpd.conf
  echo "Options +ExecCGI -Includes" >> /etc/httpd/httpd.conf
  echo "SetHandler cgi-script" >> /etc/httpd/httpd.conf
  echo "</Directory>" >> /etc/httpd/httpd.conf
  /etc/rc.d/init.d/apache restart
 else
  echo " " >> /etc/httpd/conf/httpd.conf
  echo " " >> /etc/httpd/conf/httpd.conf
  mkdir /var/www/html/webmail_cgi
  cp  /var/www/html/webmail/sqwebmail /var/www/html/webmail_cgi
    echo "<Directory "/var/www/html/webmail_cgi">" >> /etc/httpd/conf/httpd.conf
  echo "AllowOverride None" >> /etc/httpd/conf/httpd.conf
  echo "Options None" >> /etc/httpd/conf/httpd.conf
  echo "Order allow,deny" >> /etc/httpd/conf/httpd.conf
  echo "Allow from all" >> /etc/httpd/conf/httpd.conf
  echo "Options +ExecCGI -Includes" >> /etc/httpd/conf/httpd.conf
  echo "SetHandler cgi-script" >> /etc/httpd/conf/httpd.conf
  echo "</Directory>" >> /etc/httpd/conf/httpd.conf
  /etc/rc.d/init.d/httpd restart

fi

echo " "
echo " "
echo "  --> To Use Web Mail just connect to http://localhost/webmail_cgi/sqwebmail " 
echo " "
echo " " 


fi

}

big_warn ( )
 {
echo " "
echo " "
echo "                         PLEASE NOTE  "
echo "  --> Continuing with this installation will require you to have the following tools in the current directory on your disk else it will prompt for downloading them from the net. Also ensure that you have Apache Installed."
echo " "
echo "  -->  Courier-imap  1.14MB"
echo "  -->  Expect         494KB"
echo "  -->  Sqwebmail     1.19MB"
echo "  -->  tcl Source     2.7MB"
echo " "
echo "  --> You can manually download these tools from the links provided in the README file and prevent the script from downloading the sources from the Internet."
echo "  --> !!! Else are u ready for a Bandwidth resource hogger. If no press CTRL+C to ABORT !!!! "          
sleep 15
echo " "
echo " " 
}


#calling the installation greeting message
start

#testing for a wget installation
check_wget

#calling the daemontools installation
check_daemontools
daemontools_install

#installing djb dns
check_djbdns
djbdns_install

#calling ucspi-tcp installation
check_ucspi_tcp
ucspi_tcp_install

#calling the rblsmtpd installation
check_rblsmtpd
rblsmtpd_install

#calling the fastforward installation
check_fastforward
fastforward_install

#calling the checkpassword installation
check_checkpassword
checkpassword_install

#Calling the qmail installation
check_qmail
qmail_install
init q

#Calling  TCL installation
big_warn
check_tcl
tcl_install

#Calling the Expect Installation
check_expect
expect_install

#Calling the Imap installation
check_cimap
courier_install

#Calling the SQwebmail installtion
check_sqwebmail
sqwebmail_install

#calling the installation termination message
stop
