#!/bin/sh

pkgs_to_uninstall="NOVLam"
export pkgs_to_uninstall
current_version=2.2.0
log_file=/var/nam-uninstall.log
backup_dir=/var/.nam"current_version"_uninstall
PKG_INSTALLED=1
PKG_NOT_INSTALLED=2
installed_version=0

param=$1

write_log()
{
	if [ -n "$log_file" ]
	then 
        	echo $* >> $log_file
	fi
}

removePkg()
{
	pkg=$1
	str1=`gettext install "Removing"`
	echo "$str1 $pkg..."
        if rpm -q $pkg >/dev/null
        then
        	if rpm -e --nodeps $pkg >> /dev/null 2>&1
                then
                         write_log "$pkg removed successfully"
                else
			str1=`gettext install "ERROR : Failed to remove"`
			str2=`gettext install "package."`
			echo "$str1 $pkg $str2"
			str1=`gettext install "ERROR : Failed to remove"`
                        str2=`gettext install "package."`
			write_log "$str1 $pkg $str2"
			str1=`gettext install "%% Please remove"`
			str2=`gettext install "package manually"`
			echo "$str1 $pkg $str2"
                fi
         fi
}

validateScriptExecution()
{
	id=`id | awk '{print $1}'|awk -F"=" '{print $2}'|awk -F"(" '{print $1}'`
        if [ $id != 0 ]
        then
                echo `gettext install "%% You should have root permissions to execute this script.
"`
               exit 1
        fi	
}

checkForExistenceInLinux()
{
	local pkg=$1
	if rpm -q $pkg >/dev/null 2>&1
	then
		installed_version=`rpm -qi $pkg 2>/dev/null | grep "Version" | awk '{print $3}'`
		case $installed_version in
			8.1|8.1.1|8.1-1|8.5B5|8.5.*|8.6*|$current_version|8.5|8.7*)  
				return $PKG_INSTALLED
			;;
			*) return $PKG_NOT_INSTALLED
			;;
		esac
	else
	    return $PKG_NOT_INSTALLED
       	fi
}

ckyorn()
{
        shift
        ckyornstr="$@"

        ans=""
        while [ -z "$ans" ] || [ "$ans" == "ERRVAL" ]
        do
                echo -n "$ckyornstr '[y/n/q] ? '"
                read ans
                ans=`echo $ans | tr "[:upper:]" "[:lower:]"`
                case $ans in
                        y|yes) return 1 ;;
                        n|no) return 0 ;;
                        q|quit) exit 1 ;;
                        *) echo `gettext install "Invalid option : "`$ans
                           ans="ERRVAL" ;;
                esac
        done
}

restoreConfFilesOnPrompt()
{
	if [ -d /etc/pam.d.nambkp -o -f /etc/nsswitch.conf.nambkp ]
	then
		if [ "$1" == "" ]
		then
 			ResConfFileStr=`gettext install "Restore configuration files from backup"`
        		ckyorn -p "$ResConfFileStr"
        		if [ "$ans" == "n" ] || [ "$ans" == "N" ]
        		then
               			echo `gettext install "Please copy the backup of configuration files (/etc/nsswitch.conf.nambkp & /etc/pam.d.nambkp) to their default locations for normal system functioning."`
               			return 1
        		fi
		fi
	else
		return 0
	fi

#restore the *.nambkp files to the original locations

	backup_flg=0
        if test -r /etc/nsswitch.conf.nambkp
        then
                cp -f /etc/nsswitch.conf /tmp
                cp -f /etc/nsswitch.conf.nambkp /etc/nsswitch.conf
                rm -f /etc/nsswitch.conf.nambkp
                echo `gettext install "/etc/nsswitch.conf restored..."`
		backup_flg=1
        fi

        if test -d /etc/pam.d.nambkp
        then
                cp -rf /etc/pam.d /tmp
                cp -f /etc/pam.d.nambkp/* /etc/pam.d/
                rm -rf /etc/pam.d.nambkp
                echo `gettext install "/etc/pam.d/ files restored..."`
		backup_flg=1
        fi
	if [ "$backup_flg" = "1" ]
	then
       		echo `gettext install "The current files are backed up at /tmp"`
	fi

}

main()
{
	if [ -x /usr/bin/uname ]
	then
        	osname=`/usr/bin/uname -s`
	else
		if [ -x /bin/uname ]
		then
			osname=`/bin/uname -s`
		else
			echo `gettext install "%% uname not supported on your machine"`
			exit 1
		fi
	fi

#Check for correct version of the script
	if test ! "$osname" = Linux
	then
		echo `gettext install "%% This script is only for Linux."`
		exit 1
	fi
	validateScriptExecution

	if [ "$1" == "" ]
	then
		clear
		echo `gettext install "%% Welcome to uninstallation of NAM 2.2.0"`
		ckyornstr=`gettext install "Do you wish to proceed with the uninstallation"`
        	ckyorn -p "$ckyornstr"
	else
		ans=""
	fi

        if [ "$ans" == "n" ]
        then
              echo `gettext install "Quitting NAM 2.2.0 uninstallation"`
              exit 1
        fi
	if rpm -q NDSuam >/dev/null 2>&1
	then
		echo `gettext install "Earlier version (UAM) detected. This is not the appropriate uninstall script for that version"`
		exit 1
	fi

	if [ "$1" == "" ]
	then
		if test -r /etc/nam.conf
		then
			echo `gettext install "NAM is not deconfigured. Please run 'namconfig rm' before uninstallation"`
			exit 1
		fi
	else
		if test -r /etc/nam.conf
		then
			rm -f /etc/nam.conf
		fi
	fi

	checkForExistenceInLinux NDSadmutl
	if [ "$?" -eq $PKG_NOT_INSTALLED ]
	then
	    checkForExistenceInLinux NOVLc1
	    if [ "$?" -eq $PKG_NOT_INSTALLED ]
	    then
	    	checkForExistenceInLinux NOVLice
	    	if [ "$?" -eq $PKG_NOT_INSTALLED ]
	    	then
			installed_version=0
	    		checkForExistenceInLinux NDSserv
	    		if [ "$?" -eq $PKG_NOT_INSTALLED ]
	    		then
	        		pkgs_to_uninstall="$pkgs_to_uninstall NLDAPbase"
			elif [ "$installed_version" != "8.7.*" ]
			then
	        		pkgs_to_uninstall="$pkgs_to_uninstall NLDAPbase"
			fi
		fi
	    fi
	fi
	for pkg in $pkgs_to_uninstall
	do
	    removePkg $pkg
	done

  	if [ -d "$backup_dir" ]
        then
		rm -rf $backup_dir
        fi

	rm -f /var/nam-install.log
	write_log "Clean Up Post Install."

	if grep "ERROR" $log_file >/dev/null 2>&1
	then
		str1=`gettext install "There were some errors while uninstalling. Please see the log :"`
		echo "$str1 $log_file"
	else

		echo `gettext install "%% NAM 2.2.0 packages removed successfully"`
        fi
	restoreConfFilesOnPrompt $param
	if test -d /var/nam
	then
		rm -rf /var/nam
	fi

	if test -r /etc/pam.d/pam_nam_sample
	then
		rm -f /etc/pam.d/pam_nam_sample
	fi

	rm -f /usr/sbin/nam-uninstall
	rpm -q NOVLamsn >/dev/null 2>&1
	if [ $? == 0 ]
	then
		/usr/sbin/c1-amsn-uninstall
	fi
}
main $param
