#!/bin/sh
#
# install GMS
#
# Usage: install   - installs or upgrades GMS servers
#

. ./functions

user=`id | cut -c5`
if [ $user != "0" ]
then
    echo "You must logon as root to run this script"
    exit
fi
if [ $# != "0" ]
then
    echo "Usage: install"
else
    disableMailapp=0
    rm url.txt 2>>/dev/null 1>>/dev/null
    if [ -d /etc/rc.d ]
    then
        if [ -L /etc/rc.d ]
        then
            etcbase=/etc/init.d
            script=/etc/init.d/glmail
            mailapp=sendmail
            mailscript=/etc/init.d/sendmail
            mailbinary=/usr/lib/sendmail
        else
            etcbase=/etc/rc.d
            script=/etc/rc.d/init.d/glmail
            if [ -f /etc/rc.d/init.d/qmail ]
            then
                mailapp=qmail
                mailscript=/etc/rc.d/init.d/qmail
                mailbinary=/usr/lib/qmail
            elif [ -f /etc/rc.d/init.d/postfix ]
            then
                mailapp=postfix
                mailscript=/etc/rc.d/init.d/postfix
                mailbinary=/usr/lib/postfix/postfix
            else
                mailapp=sendmail
                mailscript=/etc/rc.d/init.d/sendmail
                mailbinary=/usr/lib/sendmail
            fi
        fi
    else
        etcbase=/etc/init.d
        script=/etc/init.d/glmail
        if [ -f /etc/init.d/exim ]
        then
            mailapp=exim
            mailscript=/etc/init.d/exim
            mailbinary=/usr/sbin/exim
        else
            mailapp=sendmail
            mailscript=/etc/init.d/sendmail
            mailbinary=/usr/lib/sendmail
        fi
    fi

# check if products are installed 1 - yes, 0 - no    
    ./glminst >> install.txt
    if [ $? != "0" ]
    then
        echo "upgrading GMS" >> install.txt
        echo "upgrading GMS"
        
        if [ -f $script  ]
        then
            echo "stopping GMS services" >> install.txt
            echo "stopping GMS services"
            $script stop
        fi

        ./glmupgrade ./
        if [ $? != "0" ]
        then
            echo "upgrade failed ($?)" >>install.txt
            echo "upgrade failed ($?)"
        else
# get installed directory
            path=`./glmupgrade -b`

            echo "updating startup scripts" >>install.txt
            echo "updating startup scripts"
            ./glmupgrade -m >> install.txt
# check if GMS installed 1 - yes, 0 - no            
            if [ $? != "1" ]
            then
                ./makescript $path 0 $script
            else
                ./makescript $path 1 $script
            fi

            echo "updating GMS install scripts etc in GMS directory" >>install.txt
            echo "updating GMS install scripts etc in GMS directory"
            mv install $path/install 2>>/dev/null 1>>/dev/null
            mv uninstall $path/uninstall 2>>/dev/null 1>>/dev/null
            mv glminst $path/glminst 2>>/dev/null 1>>/dev/null
            mv glmupgrade $path/glmupgrade 2>>/dev/null 1>>/dev/null

            echo "creating link to startup script" >>install.txt
            echo "creating link to startup script"
            ln -s $script $path/bin/glmail 2>>/dev/null 1>>/dev/null

            install_mysql $path

            install_gateways $path

            if [ -f $script ]
            then
                echo "starting GMS services" >> install.txt
                echo "starting GMS services"
                $script start
            fi
            echo "upgrade completed OK" >> install.txt
            echo "upgrade completed OK"

            mv install.txt $path/install.txt 2>>/dev/null 1>>/dev/null
            rm libelf.so 2>>/dev/null 1>>/dev/null
            rm makescript 2>>/dev/null 1>>/dev/null
            rm startup 2>>/dev/null 1>>/dev/null
            rm functions 2>>/dev/null 1>>/dev/null
       fi
    else
        echo "installing GMS" >> install.txt
        echo "installing GMS"
        
        if [ -f ${mailbinary} ] || [ -d ${mailbinary} ]
        then
            echo "This script needs to disable ${mailapp} if it is installed"
            echo "Enter YES to disable ${mailapp} or NO to continue"
            read reply
            if [ -z $reply ]
            then
                reply="no"
            fi
            echo ""
            if [ $reply != "YES" ] && [ $reply != "yes" ] && [ $reply != "Y" ] && [ $reply != "y" ]
            then
                echo ""
                echo "${mailapp} will not be disabled at user request"
                echo "${mailapp} will not be disabled at user request" >> install.txt
                disableMailapp=0
            else
                echo ""
                echo "${mailapp} will be disabled at user request"
                echo "${mailapp} will be disabled at user request" >> install.txt
                disableMailapp=1
            fi
        fi

# install products    
        ./glminst -i install.txt
# check install happened OK        
        ./glminst
        if [ $? != "0" ]
        then
            if [ -f /usr/lib/libelf.so ]
            then
                echo "no need to install ELF library" >>install.txt
           else
                echo "installing ELF library in /usr/lib" >>install.txt
                mv libelf.so /usr/lib
                chmod 0755 /usr/lib/libelf.so
            fi

            if [ -f /usr/lib/libelf.so.0 ]
            then
                echo "no need to install ELF library link" >>install.txt
            else
                echo "installing ELF library link in /usr/lib" >>install.txt
                ln -s /usr/lib/libelf.so /usr/lib/libelf.so.0
                chmod 0755 /usr/lib/libelf.so.0
            fi
            ./glmupgrade -n
            if [ $? != "0" ]
            then
                echo "upgrade failed"
                echo "upgrade failed" >> install.txt
                exit 2
             fi
# get installed directory
            path=`./glmupgrade -b`
# Install MySQL
            install_mysql $path

            install_gateways $path

# products installed OK        
            echo "GMS installed in ${path}" >>install.txt
            echo "GMS installed in ${path}"
            echo "GMS installed - updating startup scripts" >>install.txt
            echo "GMS installed - updating startup scripts"
            ./glmupgrade -m >> install.txt
# check if GMS installed 1 - yes, 0 - no            
            if [ $? != "1" ]
            then
                echo "GLList installed - updating startup scripts" >>install.txt
                echo "updating configuration for use with GLList list server" >>install.txt
                echo "creating default GLList startup script" >>install.txt
                ./makescript $path 0 $script

# update /etc/rcx.d files to install sxxglmail
                found="0"
                for i in $etcbase/rc*.d
                do
                    for j in $i/S*${mailapp}
                    do
                        if [ -f $j ]
                        then 
                            j="0"
                            while [ $j != "100" ]
                            do
                                k=${i}/S${j}${mailapp}
                                if [ -f $k ]
                                then
                                    echo "creating symlink ${i}/S${j}glmail" >>install.txt
                                    ln -s $script ${i}/S${j}glmail
                                    j="100"
                                    found="1"
                                else
                                    k=${i}/.S${j}${mailapp}
                                    if [ -f $k ]
                                    then
                                        echo "creating symlink ${i}/S${j}glmail" >>install.txt
                                        ln -s $script ${i}/S${j}glmail
                                        j = "100"
                                        found="1"
                                    else
                                        j=`expr $j + 1`
                                    fi
                                fi
                            done
                        fi 
                    done

                    for j in $i/K*${mailapp}
                    do
                        if [ -f $j ]
                        then 
                            j="0"
                            while [ $j != "100" ]
                            do
                                k=${i}/K${j}${mailapp}
                                if [ -f $k ]
                                then
                                    echo "creating symlink ${i}/K${j}glmail" >>install.txt
                                    ln -s $script ${i}/K${j}glmail
                                    j="100"
                                    found="1"
                                else
                                    j=`expr $j + 1`
                                fi
                            done
                        fi 
                    done
                done

                if [ $found != "1" ]
                then
                    for i in 0 1 6
                    do
                        if [ -d $etcbase/rc${i}.d ]
                        then
                            echo "creating symlink $etcbase/rc${i}.d/K30glmail" >>install.txt
                            ln -s $script $etcbase/rc${i}.d/K30glmail
                        fi
                    done
                    for i in 2 3 4 5
                    do
                        if [ -d $etcbase/rc${i}.d ]
                        then
                            echo "creating symlink $etcbase/rc${i}.d/S80glmail" >>install.txt
                            ln -s $script $etcbase/rc${i}.d/S80glmail
                        fi
                    done
                fi
            else
                echo "installing GMS" >>install.txt
                echo "installing GMS"

# update $etcbase/rcx.d files to disable sendmail and to create the Sxxglmail script

                echo "creating default GMS startup script ${script}" >>install.txt
                ./makescript ${path} 1 ${script}

                found="0"
                for i in $etcbase/rc*.d
                do
                    for j in $i/S*${mailapp}
                    do
                        if [ -f $j ]
                        then 
                            j="0"
                            while [ $j != "100" ]
                            do
                                k=${i}/S${j}${mailapp}
                                if [ -f $k ]
                                then
                                    echo "updating startup scripts to disable ${mailapp}" >>install.txt
                                    echo "renaming $k to ${i}/.S${j}${mailapp}" >> install.txt
                                    mv $k ${i}/.S${j}${mailapp}
                                    echo "creating symlink ${i}/S${j}glmail" >>install.txt
                                    ln -s $script ${i}/S${j}glmail
                                    j="100"
                                    found="1"
                                else
                                    k=${i}/.S${j}${mailapp}
                                    if [ -f $k ]
                                    then
                                        echo "creating symlink ${i}/S${j}glmail" >>install.txt
                                        ln -s $script ${i}/S${j}glmail
                                        j="100"
                                        found="1"
                                    else
                                        j=`expr $j + 1`
                                    fi
                                fi
                            done
                        fi 
                    done

                    for j in $i/K*${mailapp}
                    do
                        if [ -f $j ]
                        then 
                            j="0"
                            while [ $j != "100" ]
                            do
                                k=${i}/K${j}${mailapp}
                                if [ -f $k ]
                                then
                                    echo "creating symlink ${i}/K${j}glmail" >>install.txt
                                    ln -s $script ${i}/K${j}glmail
                                    j="100"
                                    found="1"
                                else
                                    j=`expr $j + 1`
                                fi
                            done
                        fi 
                    done
                done

                if [ $found != "1" ]
                then
                    for i in 0 1 6
                    do
                        if [ -d $etcbase/rc${i}.d ]
                        then
                            echo "creating symlink $etcbase/rc${i}.d/K30glmail" >>install.txt
                            ln -s $script $etcbase/rc${i}.d/K30glmail
                        fi
                    done
                    for i in 2 3 4 5
                    do
                        if [ -d $etcbase/rc${i}.d ]
                        then
                            echo "creating symlink $etcbase/rc${i}.d/S80glmail" >>install.txt
                            ln -s $script $etcbase/rc${i}.d/S80glmail
                        fi
                    done
                fi

# update inetd.conf to disable finger server
                if [ -f /etc/inted.conf ]
                then
                    echo "disabling internal mail server" >>install.txt
                    sed -e "s/^smtp/#smtp/" /etc/inetd.conf >/etc/inetd.new
                    mv /etc/inetd.conf /etc/inetd.org
                    mv /etc/inetd.new /etc/inetd.conf
                fi
                if [ disableMailapp != "0" ]
                then
# kill sendmail process
                    $mailscript stop
                fi
            fi

            echo "moving GMS install scripts etc to GMS directory" >>install.txt
            echo "moving GMS install scripts etc to GMS directory"
            mv install $path/install 2>>/dev/null 1>>/dev/null
            mv uninstall $path/uninstall 2>>/dev/null 1>>/dev/null
            mv glminst $path/glminst 2>>/dev/null 1>>/dev/null
            mv glmupgrade $path/glmupgrade 2>>/dev/null 1>>/dev/null

            echo "creating link to startup script" >>install.txt
            ln -s $script $path/bin/glmail 2>>/dev/null 1>>/dev/null

            echo "starting GMS services" >>install.txt
            echo "starting GMS services"
            $script start >>install.txt
            echo "installation completed OK" >>install.txt
            echo "installation completed OK"
            
            if [ -f url.txt ]
            then
                echo ""
                cat url.txt
                rm url.txt
            fi

            mv install.txt $path/install.txt 2>>/dev/null 1>>/dev/null
            rm libelf.so 2>>/dev/null 1>>/dev/null
            rm makescript 2>>/dev/null 1>>/dev/null
            rm startup 2>>/dev/null 1>>/dev/null
            rm functions 2>>/dev/null 1>>/dev/null
        else
            echo "failed to install GMS ($?)" >>install.txt
            echo "failed to install GMS ($?)"
        fi
    fi
fi
