#!/bin/sh
#
# uninstall GMS
#
# Usage: uninstall   - uninstalls GMS servers
#

user=`id | cut -c5`
if [ $user != "0" ]
then
    echo "You must logon as root to run this script"
    exit
fi
if [ $# != "0" ] && [ $# != "1" ]
then
    echo "Usage: uninstall [now]"
    echo "       if the optional param now is absent, just log actions"
else
    if [ -d /etc/rc.d ]
    then
        if [ -L /etc/rc.d ]
        then
            etcbase=/etc/init.d
            script=/etc/init.d/glmail
            sendmailapp=sendmail
            sendmailscript=/etc/init.d/sendmail
        else
            etcbase=/etc/rc.d
            script=/etc/rc.d/init.d/glmail
            if [ -f /etc/rc.d/init.d/postfix ]
            then
                sendmailapp=postfix
                sendmailscript=/etc/rc.d/init.d/postfix
            else
                sendmailapp=sendmail
                sendmailscript=/etc/rc.d/init.d/sendmail
            fi
        fi
    else
        etcbase=/etc/init.d
        script=/etc/init.d/glmail
        sendmailapp=sendmail
        sendmailscript=/etc/init.d/sendmail
    fi

    if [ $# != "1" ]
    then
        echo "By default, this script produces a preview of the GMS uninstall process."

        echo ""

        echo "Enter "NOW" to proceed with the uninstallation "

        read reply

        if [ $reply != "NOW" ]

        then

            immediate=no
            echo ""

            echo "Running a preview of the GMS uninstallation process"

            echo "Logging actions to uninstall.txt"

        else

            immediate=yes

            echo ""

            echo "Bypassing preview of the GMS uninstallation process"

        fi

    else
        if [ $1 != "now" ]
        then
            echo "invalid option - aborting"
            exit 1
        else
            immediate=yes
            echo "uninstalling GMS"
        fi
    fi
# check if products are installed 1 - yes, 0 - no    
    if [ -f ./glminst ]
    then
        ./glminst >> uninstall.txt
        if [ $? != "0" ]
        then
# get installed directory
            path=`./glminst -b`
# stop GMS services
            if [ $path != "" ]
            then
                if [ -f ${path}/mail ]
                then
                    echo "GMS is installed in ${path}"
                    if [ $immediate != "no" ]
                    then
                        echo ""
                        echo "Warning - You are about to uninstall GMS"
                        echo "This will remove all GMS files and messages"
                        echo "and cannot be undone."
                        echo ""
                        echo "Enter "YES" to proceed with the uninstallation "
                        read reply
                        echo ""
                        if [ $reply != "YES" ]
                        then
                            echo "Uninstallation aborted at user request"
                            exit 0
                        else
                            echo "Uninstallation continued at user request"
                        fi                 
                    fi
                else
                    echo "invalid path - mail directory not found"
                    exit 1
                fi
            else
                echo "invalid path - install path not found"
                exit 1
            fi                    
# stop GMS services
            if [ -f $script ]
            then
                echo "stopping GMS services" >> uninstall.txt
                if [ $immediate != "no" ]
                then
                    $script stop
                else
                    echo "$script stop" >>uninstall.txt
                fi
           fi

# update /etc/rcx.d files to disable GMS and to restore the Sxxsendmail script
            found="0"
            for i in $etcbase/rc*.d
            do
                for j in $i/S*glmail
                do
                    if [ -f $j ]
                    then 
                        j="0"
                        while [ $j != "100" ]
                        do
                            if [ -f ${i}/.S${j}${sendmailapp} ]
                            then
                                echo "restoring  ${sendmailapp} startup script" >>uninstall.txt
                                if [ $immediate != "no" ]
                                then
                                    mv ${i}/.S${j}${sendmailapp} ${i}/S${j}${sendmailapp}
                                else
                                    echo "mv ${i}/.S${j}${sendmailapp} ${i}/S${j}${sendmailapp}" >>uninstall.txt
                                fi

                                startscript=${i}/S${j}glmail
                                if [ -f $startscript ]
                                then
                                    echo "removing GMS startup script $startscript" >>uninstall.txt
                                    if [ $immediate != "no" ]
                                    then
                                        rm $startscript 2>>/dev/null 1>>/dev/null
                                    else
                                        echo "rm $startscript" >>uninstall.txt
                                    fi
                                fi
                                j="100"
                                found="1"
                            else
                                j=`expr $j + 1`
                            fi
                        done
                    fi 
                done

                for j in $i/K*glmail
                do
                    if [ -f $j ]
                    then 
                        j="0"
                        while [ $j != "100" ]
                        do
                            stopscript=${i}/K${j}glmail
                            if [ -f $stopscript ]
                            then
                                echo "removing GMS shutdown script $stopscript" >>uninstall.txt
                                if [ $immediate != "no" ]
                                then
                                    rm $stopscript 2>>/dev/null 1>>/dev/null
                                else
                                    echo "rm $stopscript" >>uninstall.txt
                                fi
                            fi
                            j=`expr $j + 1`
                        done
                    fi 
                done
            done

            if [ $found != "1" ]
            then
                for i in 0 1 6
                do
                    stopscript=$etcbase/rc${i}.d/K30glmail
                    echo "removing default GMS shutdown script ${i}" >>uninstall.txt
                    if [ $immediate != "no" ]
                    then
                        rm $stopscript 2>>/dev/null 1>>/dev/null
                    else
                        echo "rm $stopscript" >>uninstall.txt
                    fi
                done
                for i in 2 3 4 5
                do
                    startscript=$etcbase/rc${i}.d/S80glmail
                    echo "removing default GMS startup script ${i}" >>uninstall.txt
                    if [ $immediate != "no" ]
                    then
                        rm $startscript 2>>/dev/null 1>>/dev/null
                    else
                        echo "rm $startscript" >>uninstall.txt
                    fi
                done
            fi

            if [ -f $script ]
            then
                echo "removing common GMS startup script" >>uninstall.txt
                if [ $immediate != "no" ]
                then
                    rm $script 2>>/dev/null 1>>/dev/null
                else
                    echo "rm $script" >>uninstall.txt
                fi
            fi

            if [ -f /usr/lib/libglmail.so ]
            then
                echo "removing GMS library" >>uninstall.txt
                if [ $immediate != "no" ]
                then
                    rm /usr/lib/libglmail.so 2>>/dev/null 1>>/dev/null
                else
                    echo "rm /usr/lib/libglmail.so" >>uninstall.txt
                fi
            fi
        
            if [ $path != "" ]
            then
                if [ -f $path/ntmunix.so ]
                then
                    cd $path
                    cd ..
                    path=`pwd`
                    echo "removing GMS files in $path" >>uninstall.txt
                    if [ $immediate != "no" ]
                    then
                        rm -r $path    
                    else
                        echo "rm -r $path" >>uninstall.txt
                    fi
                    if [ $immediate != "no" ]
                    then
                        echo "GMS uninstall completed"
                    else
                        echo "The GMS uninstall preview is complete"

                    fi
                else
                    echo "incorrect path $path - ntmunix.so not found"
                fi
            fi    
        fi
    else
        echo "incorrect path - glminst not found"
    fi
fi
