#!/bin/bash

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

start ( )
 {
  clear
  echo " "
  echo " "
  echo "          +++++++++++++++++++++++++++++++++++++"
  echo "            Welcome to Trevor's qmail Install"
  echo "          +++++++++++++++++++++++++++++++++++++"
  echo " " 
  echo " "
 }

stop ( )
 {
  echo " "
  echo " "
  echo "          +++++++++++++++++++++++++++++++++++++++"
  echo "            Thx for using Trevor's qmail Install"
  echo "          +++++++++++++++++++++++++++++++++++++++"
  echo " "
  echo " "
 }

check_wget ( )
 {
  echo " "
  echo "  --> Testing for a wget installation"
  wget_there=`rpm -qa | grep -i wget`
  if [ -z $wget_there ]
   then
    echo " "
    echo "  --> You do not seem to have a Wget Installation on your machine. Hence, you will not be able to download any qmail dependencies from the Internet."
    echo " "
   else
    echo " "
    echo "  --> Your Wget installation seems perfect. "
    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. Enter either
 y or n." 
  read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/daemontools/daemontools-0.76.tar.gz
      d_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the daemontools 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 " "
      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. Enter either y 
or n."
  read answer6
    if [ $answer6 = $ans1 -o $answer6 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/ucspi-tcp/ucspi-tcp-0.88.tar.gz
      u_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the ucspi-tcp 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 " "
      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. Enter either y or 
n."
   read answer1
    if [ $answer1 = $ans1 -o $answer1 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/software/qmail-1.03.tar.gz
      q_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the qmail 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 " "
      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. Enter either y 
or n."
read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/software/rblsmtpd-0.70.tar.gz
      r_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the rblsmtpd 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 " "
      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. Enter either yor n."
read answer4
    if [ $answer4 = $ans1 -o $answer4 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/software/fastforward-0.51.tar.gz
      f_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the fastforward 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 " "
      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. Enter either y or 
n."
  read answer2
    if [ $answer2 = $ans1 -o $answer2 = $ans2 ]
     then
      echo " "
      wget -c -v -t0 http://cr.yp.to/djbdns/djbdns-1.05.tar.gz
      j_there=y
     else
      echo " "
      echo "  --> You have choosen not to download the djbdns 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 " "
     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

 }


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
  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

  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

  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




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 ( )
 {

mv /etc/resolv.conf /etc/resolv.conf.bak
touch /etc/resolv.conf
echo "nameserver 192.168.0.1" > /etc/resolv.conf
cat /etc/resolv.conf.bak >> /etc/resolv.conf

  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
}

dns_configure
dns_postconfigure

fi

}

#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 qmail installation
check_qmail
qmail_install
init q

#calling the installation termination message
stop
