#!/bin/sh
####################################################
# @(#) sminst unix plattforms
####################################################

LOTUS="/opt/lotus"
LOTUSPKG="vlotus.tz"
AGREEMENT="license"
ECHO="Echo"
READ="read"
MORE="More"
NUMBER_MAILBOXES=0
NUMBER_TMMSCANS=0
TMMSCANS=""
TMP_NAME_BAK="notes_ini_smln_bak"
TMP_NAME="tmp_notes_ini_$(date '+%d_%m_%y_%T')"

# ex. Echo -n "Echo"
# -n: supress new line character
Echo()
{
    if [ "$1" = "-n" ] ; then
        shift 1

        case $arch in
            linux) echo -e -n "$@" ;;
            *)     echo "$@\c" ;;
        esac
    else
        case $arch in
            linux) echo -e "$@" ;;
            *)     echo "$@" ;;
        esac
    fi

    return 0
}

More()
{
    if [ "$arch" = linux ] ; then
        if [ -x /usr/bin/less ] ; then
            # Use less to display Japanese characters
            less -E "$@"
            return 0
        fi
    fi
    if [ "$arch" != "os390" ] ; then
      more -d "$@"
    else
      more "$@"
    fi

    return 0
}

# Patch the install folder
OSPatch()
{
    if [ "$arch" = sol ] ; then
	ver=`uname -r`
        if [ "$ver" = "5.5.1" ] ; then
	    tar xf $ospatchfile
	fi
    fi	
}

#
Determine_mail_number_of_mailboxes()
{

   NUMBER_MAILBOXES=`ls -l $INIPATH/mail[1-3].box 2>/dev/null |wc -l`
   if [ $NUMBER_MAILBOXES -eq 0 ] ; then
       NUMBER_MAILBOXES=1
   fi

   NUMBER_TMMSCANS=`expr $NUMBER_MAILBOXES + 1`
   if [ $NUMBER_TMMSCANS -eq 2 ] ; then
      TMMSCANS="tmmscan,tmmscan"
   fi

   if [ $NUMBER_TMMSCANS -eq 3 ] ; then
      TMMSCANS="tmmscan,tmmscan,tmmscan"
   fi

   if [ $NUMBER_TMMSCANS -gt 3 ] ; then
      TMMSCANS="tmmscan,tmmscan,tmmscan,tmmscan"
   fi
}

# function to set unique parameter in text file
# parameter format: param=value
# e.g.
# SetParamIni Temp $INIPATH/smln $INIPATH/$emini_file
# SetParamIni Shell $INIPATH/seserver.cfg $INIPATH/$emini_file
#
SetParamIni()
{
    if [ -f $iniscript ] ; then
        \rm $iniscript
    fi
    
    par=$1
    value=$2
    ini=$3
    item=`grep -i "^$par=" $ini |cut -d= -f1`
    if [ "$item" != "" ] ;then
        Echo "ed -s $ini << T_EOF > /dev/null" >>$iniscript
        Echo "/^$item=" >>$iniscript
        Echo "c" >>$iniscript
        Echo "$item=$value" >>$iniscript
        Echo "." >>$iniscript
        Echo "w" >>$iniscript
        Echo "q" >>$iniscript
        Echo "T_EOF" >>$iniscript
    else
        # Add whole line
        grep -i "^$par=" $ini |cut -d= -f1 /dev/null 2>&1
        if [ $? -eq 0 ] ; then
            Echo "ed -s $ini << T_EOF > /dev/null" >>$iniscript
            Echo "?^.[em_core]" >>$iniscript
            Echo ".a" >>$iniscript
            Echo "$par=$value" >>$iniscript
            Echo "." >>$iniscript
            Echo "w" >>$iniscript
            Echo "q" >>$iniscript
            Echo "T_EOF" >>$iniscript
        fi
    fi

    if [ -f $iniscript ] ; then
        lines=`cat $iniscript | wc -l`
        if [ $lines -gt 5 ] ; then
            sh $iniscript
        fi
    fi

    return 0
}

# $1 - a/d/r   $2 - Item   $3 - Value
# $2 is case insensitive, $3 is case sensitive in this funtion
# ex. SetIni a ServerTasks tmmscan
SetIni()
{
    if [ -f $iniscript ] ; then
        \rm $iniscript
    fi

    item=`grep -i "^$2=" $NOTESINI |cut -d= -f1`
    if [ "$item" != "" ] ;then
        Echo "ed -s $NOTESINI << T_EOF > /dev/null" >>$iniscript
        Echo "/^$item=" >>$iniscript
        case $1 in
        a) # Add specific value
            value=`grep -i "^$2=" $NOTESINI |cut -d= -f2`
            if [ "$value" = "" ] ; then
                # No value exists. Add newly.
                Echo "s/$/$3" >>$iniscript
            else
                thisvalue=`Echo "$value" | grep $3`
                if [ "$thisvalue" = "" ] ; then
                    # Add to the end.
                    Echo "s/$/,$3" >>$iniscript
                fi
            fi
            ;;
        d) # Delete whole line
            Echo ".d" >>$iniscript
            ;;
        r) # Remove specific value
            value=`grep -i "^$2=" $NOTESINI |cut -d= -f2`
            if [ "$value" = "$3" ] ; then
                Echo ".d" >>$iniscript # Delete whole line
             else
                value=`grep -i "^$2=" $NOTESINI |grep -i ",$3"`
                if [ "$value" != "" ]; then
                    Echo "s/,$3//g" >>$iniscript
                fi
                value=`grep -i "^$2=" $NOTESINI |grep -i ", $3"`
                if [ "$value" != "" ]; then
                    Echo "s/, $3//g" >>$iniscript
                fi
                value=`grep -i "^$2=" $NOTESINI |grep -i "=$3,*"`
                if [ "$value" != "" ]; then
                    Echo "s/=$3,*/=/g" >>$iniscript
                fi
            fi
            ;;
        esac
        Echo "w" >>$iniscript
        Echo "q" >>$iniscript
        Echo "T_EOF" >>$iniscript
    else
        case $1 in
        a) # Add whole line
            grep -i "^$2=" $NOTESINI |cut -d= -f1 /dev/null 2>&1
            if [ $? -eq 0 ] ; then
                Echo "ed -s $NOTESINI << T_EOF > /dev/null" >>$iniscript
                Echo "?^.*=" >>$iniscript
                Echo ".a" >>$iniscript
                Echo "$2=$3" >>$iniscript
                Echo "." >>$iniscript
                Echo "w" >>$iniscript
                Echo "q" >>$iniscript
                Echo "T_EOF" >>$iniscript
            fi
            ;;
        esac
    fi

    if [ -f $iniscript ] ; then
        lines=`cat $iniscript | wc -l`
        if [ $lines -gt 5 ] ; then
	    if [ "$arch" != "os390" ] ; then
              su - $LOTUSUSER -c "sh $iniscript"
	    else
	      sh $iniscript
	    fi
        fi
    fi

    return 0
}

ascii2ebcdic ()
{
	cp $INIPATH/notes.ini $INIPATH/$TMP_NAME_BAK
	NOTESINI=$INIPATH/notes.ini.sminst
	iconv -f ISO8859-1 -t IBM-1047 $INIPATH/notes.ini > $NOTESINI
	if [ $? -ne 0 ]; then
		if [ -a $INIPATH/notes.ini ]; then
			rm $INIPATH/$TMP_NAME 
			$ECHO "Error converting input file"
			exit 1 
		fi
	fi
}

ebcdic2ascii ()
{
	iconv -f IBM-1047 -t ISO8859-1 $NOTESINI > $INIPATH/$TMP_NAME
	if [ $? -ne 0 ]; then
		cp $INIPATH/$TMP_NAME_BAK $INIPATH/notes.ini
		rm $INIPATH/$TMP_NAME
		$ECHO "Error encountered converting the updated file"
		exit 3 
	fi
	rm $INIPATH/notes.ini
	mv $INIPATH/$TMP_NAME $INIPATH/notes.ini
	chown $LOTUSUSER $INIPATH/notes.ini
#	rm $INIPATH/$TMP_NAME_BAK
	rm $NOTESINI
}

PostProcess()
{
    if [ $setuptype -eq 1 ] ; then
 
	case $arch in
	    aix)
	        if [ ! -L $SOPATH/libem_core.a ] ; then
		  ( cd $SOPATH; \ln -sf libem_core.so libem_core.a > /dev/null )
		  ( cd $SOPATH; \ln -sf libem_helpr.so libem_helpr.a > /dev/null )
		fi
		if [ -x $SOPATH/dataseg ] ; then
			$ECHO "Updating the ScanMail executables for compatibility with Domino 5.09"
			for i in $exe_file
			do
			    $SOPATH/dataseg -s $SOPATH/$i > /dev/null
			done
		fi
	    ;;
	    sol)
	        if [ ! -h $SOPATH/libicuuc.so ] ; then
		  ( cd $SOPATH; \ln -sf libicuuc.so.18 libicuuc.so > /dev/null )
		  ( cd $SOPATH; \ln -sf libicui18n.so.18 libicui18n.so > /dev/null )
		  ( cd $SOPATH; \ln -sf libMessage.so libmessage.so > /dev/null )
		fi
	    ;;
	    linux)
	    	if [ ! -L $SOPATH/libmessage.so ] ; then
		  ( cd $SOPATH; \ln -sf libMessage.so libmessage.so > /dev/null )
		fi
	    ;;
	    os390)
	    	if [ ! -L $SOPATH/Message ] ; then
		  ( cd $SOPATH; \ln -sf libmessage Message > /dev/null )
		fi
		if [ ! -L $SOPATH/smlneman ] ; then
		  ( cd $SOPATH; \ln -sf libsmlneman smlneman > /dev/null )
		fi
		if [ ! -L $SOPATH/libsmlneman.so ] ; then
		  ( cd $SOPATH; \ln -sf libsmlneman libsmlneman.so > /dev/null )
		fi
		if [ ! -L $SOPATH/smlnred ] ; then
		  ( cd $SOPATH; \ln -sf libsmlnred smlnred > /dev/null; \ln -sf libsmlnred libsmlnred.so > /dev/null)
		fi
	    	if [ ! -L $SOPATH/r5open ] ; then
		  ( cd $SOPATH; \ln -sf libr5open r5open > /dev/null ; \ln -sf libr5open libr5open.so > /dev/null  )
		fi
	    	if [ ! -L $SOPATH/vsapicall ] ; then
		  ( cd $SOPATH; \ln -sf libvsapicall vsapicall > /dev/null; \ln -sf libvsapicall libvsapicall.so > /dev/null )
		fi
	        if [ ! -L $LOTUS/bin/convert ] ; then
		  ( cd $LOTUS/bin; \ln -sf tools/startup convert > /dev/null )
		fi
	        if [ ! -L $LOTUS/bin/dbsetup ] ; then
		  ( cd $LOTUS/bin; \ln -sf tools/startup dbsetup > /dev/null )
		fi
	        if [ ! -L $LOTUS/bin/dbmigrate ] ; then
		  ( cd $LOTUS/bin; \ln -sf tools/startup dbmigrate > /dev/null )
		fi

	    ;;
	esac
	if [ "$arch" != "sol" ] ; then
		if [ ! -L $INIPATH/smln/au/libtmactupdate.so ] ; then
          SHORTSOPATH=`dirname $SOPATH`
		  SHORTSOPATH=`dirname $SHORTSOPATH`
		  SHORTSOPATH=$SHORTSOPATH/latest/`basename $SOPATH`
          ( cd $INIPATH/smln/au; \ln -sf $SHORTSOPATH/libtmactupdate.so libtmactupdate.so > /dev/null )
		fi
	else
	   if [ ! -h $INIPATH/smln/au/libtmactupdate.so ] ; then
          SHORTSOPATH=`dirname $SOPATH`
		  SHORTSOPATH=`dirname $SHORTSOPATH`
		  SHORTSOPATH=$SHORTSOPATH/latest/`basename $SOPATH`
          ( cd $INIPATH/smln/au; \ln -sf $SHORTSOPATH/libtmactupdate.so libtmactupdate.so > /dev/null )
	   fi
	fi
	if [ "$arch" = "os390" ] ; then
	  if [ ! -L $INIPATH/smln/au/libsmlnaupdate.so ] ; then
            ( cd $INIPATH/smln/au; \ln -sf libsmlnaupdate libsmlnaupdate.so > /dev/null )
	  fi
	fi
	value=`find $SOPATH -name libvsapi.* 2> /dev/null`
	if [ "$value" != "" ] ; then
	   ( cd $SOPATH; \rm -rf libvsapi.* > /dev/null )
        fi
    else
	case $arch in
	    aix)
		( cd $SOPATH; \rm -rf libem_core.a )
		( cd $SOPATH; \rm -rf libem_helpr.a )
	    ;;
	    sol)
		(cd $SOPATH; \rm -rf libicuuc.so )
		(cd $SOPATH; \rm -rf libicui18n.so )
		(cd $SOPATH; \rm -rf libmessage.so )
	    ;;
	    linux)
		(cd $SOPATH; \rm -rf libmessage.so )
	    ;;
	    os390)
		(cd $SOPATH; \rm -rf smlneman )
		(cd $SOPATH; \rm -rf libsmlneman.so )
		(cd $SOPATH; \rm -rf Message )
		(cd $SOPATH; \rm -rf libmessage )
		(cd $SOPATH; \rm -rf smlnred ; \rm -rf libsmlnred.so )
		(cd $SOPATH; \rm -rf r5open ; \rm -rf libr5open.so )
		(cd $SOPATH; \rm -rf vsapicall ; \rm -rf libvsapicall.so )
		(cd $LOTUS/bin; \rm -f dbsetup ; \rm -f dbmigrate )
		(cd $LOTUS/bin; \rm -f convert )
	    ;;
	esac
    fi

    Determine_mail_number_of_mailboxes

    return 0
}

smchgrp()
{
    if [ "$arch" != "os390" ] ; then
       chgrp $1 $2
    fi
}

#
# add execute rights
# change owner to LOTUSUSER
#
smchmod_own()
{
    chmod +x $1
    chown $LOTUSUSER $1
}

Copyfiles()
{

    AUPATH=$INIPATH/smln/au

    ## Copy so/exe
    for i in $so_file $exe_file
    do
	if [ "$arch" = "aix" ] ; then
	  \rm -rf $SOPATH/$i
	fi
        \cp $TMPDIR/$i $SOPATH
        chown $LOTUSUSER $SOPATH/$i
	
        smchgrp $LOTUSGROUP $SOPATH/$i
        chmod 755 $SOPATH/$i
    done

    ## Copy eManager
    if [ "$arch" != "aix" ] && [ "$arch" != "os390" ] ; then
        for i in $emcore_file $emicu_file $emdmc_file $emclm_file $emlog_file
        do
            \cp $TMPDIR/em/$i $SOPATH
            chown $LOTUSUSER $SOPATH/$i
            smchgrp $LOTUSGROUP $SOPATH/$i
            chmod 755 $SOPATH/$i
        done

        for i in $emini_file
        do
            \cp $TMPDIR/em/$i $INIPATH
            chown $LOTUSUSER $INIPATH/$i
            smchgrp $LOTUSGROUP $INIPATH/$i
            chmod 644 $INIPATH/$i
        done

        if [ ! -d $SOPATH/base2 ] ; then
            mkdir $SOPATH/base2
            chown $LOTUSUSER $SOPATH/base2
            smchgrp $LOTUSGROUP $SOPATH/base2
            chmod 755 $SOPATH/base2
        fi

        cd $SOPATH/base2 ; tar xvf $TMPDIR/em/base2.tar 1> /dev/null
        chown $LOTUSUSER $SOPATH/base2/*
        smchgrp $LOTUSGROUP $SOPATH/base2/*
        chmod 644 $SOPATH/base2/*

    else
        for i in $emcore_file $emicu_file $emdmc_file $emclm_file $emlog_file
        do
            \cp $TMPDIR/em/$i $SOPATH
            chown $LOTUSUSER $SOPATH/$i
            smchgrp $LOTUSGROUP $SOPATH/$i
            chmod 755 $SOPATH/$i
        done

        for i in $emini_file
        do
            \cp $TMPDIR/em/$i $INIPATH
            chown $LOTUSUSER $INIPATH/$i
            smchgrp $LOTUSGROUP $INIPATH/$i
            chmod 644 $INIPATH/$i
        done

    fi

    if [ ! -d $INIPATH/smln ] ; then
        mkdir $INIPATH/smln
        chown $LOTUSUSER $INIPATH/smln
        smchgrp $LOTUSGROUP $INIPATH/smln
        chmod 755 $INIPATH/smln
    fi

    ## Copy engine

    if [ ! -d $INIPATH/smln/scanengine ] ; then
        mkdir $INIPATH/smln/scanengine
        chown $LOTUSUSER $INIPATH/smln/scanengine
        smchgrp $LOTUSGROUP $INIPATH/smln/scanengine
        chmod 755 $INIPATH/smln/scanengine
    fi

    for i in $engine_file
    do
	if [ ! -f $INIPATH/smln/scanengine/$i ] ; then
	   \cp $TMPDIR/$i $INIPATH/smln/scanengine
        fi
        chown $LOTUSUSER $INIPATH/smln/scanengine/$i
        smchgrp $LOTUSGROUP $INIPATH/smln/scanengine/$i
        chmod 755 $INIPATH/smln/scanengine/$i
    done

    for i in $em_db_file
    do
        \cp $TMPDIR/em/$i $INIPATH/smln/scanengine
        chown $LOTUSUSER $INIPATH/smln/scanengine/$i
        smchgrp $LOTUSGROUP $INIPATH/smln/scanengine/$i
        chmod 644 $INIPATH/smln/scanengine/$i
    done

    ## Copy au
    if [ ! -d $AUPATH ] ; then
        mkdir $AUPATH
        chown $LOTUSUSER $AUPATH
        smchgrp $LOTUSGROUP $AUPATH
        chmod 755 $AUPATH
    fi

    for i in $au_file
    do
        \cp $TMPDIR/au/$i $AUPATH
        chown $LOTUSUSER $AUPATH/$i
        smchgrp $LOTUSGROUP $AUPATH/$i
        chmod 755 $AUPATH/$i
    done

    for i in $auini_file
    do
        \cp $TMPDIR/au/$i $AUPATH
        chown $LOTUSUSER $AUPATH/$i
        smchgrp $LOTUSGROUP $AUPATH/$i
        chmod 644 $AUPATH/$i
    done

    ## Copy db
    for i in $install_db
    do
        \cp $TMPDIR/$i $INIPATH
        chown $LOTUSUSER $INIPATH/$i
        smchgrp $LOTUSGROUP $INIPATH/$i
        chmod 644 $INIPATH/$i
    done

    ## Copy db if not exist
    for i in $noupdate_db
    do
        if [ ! -s $INIPATH/$i ] ; then
          \cp $TMPDIR/$i $INIPATH
          chown $LOTUSUSER $INIPATH/$i
          smchgrp $LOTUSGROUP $INIPATH/$i
          chmod 644 $INIPATH/$i
        fi
    done

    if [ "$SOPATH" = "$tmp_sopath" ] ; then # if not equal, it's Domino 6
        latestpath=`dirname $SOPATH`
        domino_ver=`ls -l $latestpath | awk '{print $NF}' | cut -c1`
        if [ $domino_ver -eq 4 ] ; then
            return 0
        fi
    fi

    for i in $r5only_db
    do
        \cp $TMPDIR/$i $INIPATH
        chown $LOTUSUSER $INIPATH/$i
        smchgrp $LOTUSGROUP $INIPATH/$i
        chmod 644 $INIPATH/$i
    done

    return 0
}

CMAgentMsg()
{
        $ECHO "\n\n\tTrend Micro Control Manager is a centralized management utility"
        $ECHO "\tthat you can use to unite individual antivirus solutions"
        $ECHO "\t(such as ScanMail for Lotus Notes) into a comprehensive LAN or WAN"
        $ECHO "\tdefense against viruses."
        $ECHO "\tControl Manager is separately purchased and installed."
        $ECHO "\tIf you are currently running Control Manager, you should install the"
        $ECHO "\tScanMail Control Manager Agent to link up the two programs."
        $ECHO "\tHowever, Control Manager Agent installation program requires"
        $ECHO "\tDomino server to run. If you want to install Control Manager Agent,"
        $ECHO "\tplease first install ScanMail for Lotus Notes on all Domino servers"
        $ECHO "\ton the current system, including partitioned servers,"
        $ECHO "\tstart Domino servers and then use ./smcminst -install"
        $ECHO "\tunder cmagent directory of SMLN installation package."
}

InstallSMLN()
{

    check0=`ls $SOPATH/smconf.nsf 2>/dev/null`
    check1=`ls $INIPATH/smmsg.nsf 2>/dev/null`
    check2=`ls $INIPATH/smftypes.ntf 2>/dev/null`

    upgrade=0
    if [ "$check1" != "" ] ; then
        if [ "$check2" != "" ] ; then
            # CURRENT VERSION FOUND
            upgrade=2
            Echo "\n\tUpgrading ScanMail for Lotus Notes..."
        else
            # PREVIOUS VERSION FOUND
            upgrade=1
            Echo "\n\tThe old release of ScanMail for Lotus Notes was found."
            $ECHO -n "\tDo you want to upgrade ScanMail for Lotus Notes(y/n)? " ; $READ yesno
            while [ ! \( "$yesno" = "y" -o "$yesno" = "Y" \) ]
            do
                if [ "$yesno" = "n" -o "$yesno" = "N" ]; then
                    Echo "\n\tPlease uninstall ScanMail before running Install (enter "
                    Echo "\t./sminst remove at the prompt)."
                    exit 1
                fi
                $ECHO -n "\n\tPlease answer y or n:" ; $READ yesno
            done
            Echo "\n\tUpgrading ScanMail for Lotus Notes..."
        fi
    elif [ "$check0" != "" ]; then
        # OLD VERSION FOUND
        Echo "\n\tScanMail for Lotus Notes was found."
        Echo "\tPlease uninstall ScanMail before running Install (enter "
        Echo "\t./sminst remove at the prompt)."
        exit 1
    fi

    # $upgrade
    # 0: No SMLN found       -- New install
    # 1: SMLN 2.5            -- Upgrade install
    # 2: SMLN 2.6 or later   -- CM agent install
    # 3: nothing to do

    if [ $upgrade -eq 3 ] ; then
	CMAgentMsg
	return 0
    fi

    ## Decompress package
    if [ -d $TMPDIR ] ; then
        \rm -fr $TMPDIR/*
    else
        mkdir $TMPDIR
    fi

    if [ ! -s $LOTUSPKG ] ; then
        Echo "\n\tYou must have $LOTUSPKG in current directory. Exiting..."
        exit 1 
    fi

    case $arch in
        aix)   avail=`df -k $TMPDIR | sed -n '2p' | awk '{print $3}'` ;;
        sol)   avail=`df -k $TMPDIR | sed -n '2p' | awk '{print $4}'` ;;
        linux) avail=`df -k $TMPDIR | sed -n '2p' | awk '{print $4}'` ;;
        os390) avail=`df -k $TMPDIR | sed -n '2p' | awk '{print $4}'` ;;
    esac

    Echo "\n\tChecking disk space..."
    pkgsize=`zcat < $LOTUSPKG | wc -c`
    pkgsize=`expr $pkgsize / 1024 + 1` # size in kBytes
    
    lenavail=`echo $avail | wc -c`
    lenpkg=`echo $pkgsize | wc -c`
    nospace=0
    if [ $lenavail -lt $lenpkg ] ; then # first check string length not enough space
        nospace=1
    fi
    
    if [ $lenavail -eq $lenpkg ] ; then # second check integer values
        if [ $avail -lt $pkgsize ] ; then # $avail: kBytes $pkgsize: kBytes.
            nospace=1
        fi
    fi
	
    if [ $nospace -eq 1 ] ; then # $avail: kBytes $pkgsize: kBytes.
        Echo "\n\tThere is not enough space in $TMPDIR."
        Echo "\t$pkgsize bytes free space is necessary."
        exit 1
    fi

    Echo "\n\tDecompressing the archived files..."

    zcat < $LOTUSPKG | ( cd $TMPDIR ; tar xf - )

    ( cd $TMPDIR ; OSPatch )

    chmod 777 $TMPDIR/*
    chown $LOTUSUSER $TMPDIR/*

    if [ "$arch" = "os390" ] ; then
    	ascii2ebcdic
    fi

    if [ $upgrade -ne 3 ] ; then
        Copyfiles
	PostProcess
    fi
    if [ $upgrade -eq 0 ] ; then
        SetIni a ServerTasks $TMMSCANS
        SetIni a ServerTasks repscan
        SetParamIni Temp $INIPATH/smln $INIPATH/$emini_file
        SetParamIni Shell $INIPATH/seserver.cfg $INIPATH/$emini_file
    fi

    cd "$INIPATH"
    INIPATH=`pwd`
    cd $WORKDIRECTORY
    INIENTRY=`grep -i "^SMSTOPMAIL=" "$NOTESINI" | cut -d= -f2`
    if [ "$INIENTRY" = "" ] ; then
        SetIni a SMSTOPMAIL 0
    fi
    INIENTRY=`grep -i "^SMOUTPUTLEVEL=" "$NOTESINI" | cut -d= -f2`
    if [ "$INIENTRY" = "" ] ; then
        SetIni a SMOUTPUTLEVEL 2
    fi
    INIENTRY=`grep -i "^EXTMGR_ADDINS=" "$NOTESINI" | grep -i smlnred | cut -d= -f2`
    if [ "$INIENTRY" = "" ] ; then
        SetIni a EXTMGR_ADDINS smlnred
    fi
    ## Serial number check for SMLN
    if [ $upgrade -ne 2 ] ; then
     SMLICENSE=`grep -i "^SMLICENSE=" "$NOTESINI" | cut -d= -f2`
     while true ; do
            $ECHO "\n\n\tEnter your ScanMail for Lotus Notes serial number,"
            if [ ! -n "$SMLICENSE" ]; then
               $ECHO "\tor press Enter to install the 30-day trial version."
            else
               $ECHO "\tor press Enter to use the previous serial number."
               $ECHO "\t[$SMLICENSE] "
            fi
            $ECHO -n "\n\tSerial Number : " ; $READ LICENSESTR
            if [ ! -n "$LICENSESTR" ]; then
               LICENSESTR=$SMLICENSE
            fi
            if [ -n "$LICENSESTR" ]; then
                $TMPDIR/licensecheck $LICENSESTR 0
                if [ $? != 0 ]; then
                    $ECHO "\n\tSerial number accepted.\n"
                    break
                else
                    if [ $silentmode -eq 0 ] ; then
                        $ECHO "\n\tSerial number is not valid."
                        $ECHO "\tPlease try it again... "
                    else
                        Echo "\n\tSerial number for ScanMail is not valid."
			if [ "$arch" = "os390" ] ; then
				ebcdic2ascii # exit
			fi
                        exit 1
                    fi
                fi
            else
                Echo "\n\tNo serial number was entered."
                Echo "\tInstalling the trial version of ScanMail for Lotus Notes..."
                break
            fi
        done
    fi

   ## Serial number check for eManager
    if [ $upgrade -ne 2 ] ; then
        while true ; do
            $ECHO "\n\n\tEnter your eManager serial number,"
            $ECHO "\tor press Enter to install the 30-day trial version."
            $ECHO -n "\n\tSerial Number : " ; $READ LICENSEEMGR
            if [ -n "$LICENSEEMGR" ]; then
                $TMPDIR/licensecheck $LICENSEEMGR 1
                if [ $? != 0 ]; then
                    $ECHO "\n\tSerial number accepted.\n"
                    break
                else
                    if [ $silentmode -eq 0 ] ; then
                        $ECHO "\n\tSerial number is not valid."
                        $ECHO "\tPlease try it again... "
                    else
                        Echo "\n\tSerial number for eManager is not valid."
                        exit 1
                    fi
                fi
            else
                Echo "\n\tNo serial number was entered."
                Echo "\tInstalling the trial version of eManager..."
                break
            fi
        done
    fi

    ## Temporary directory
    if [ $upgrade -eq 0 ] ; then
        $ECHO "\n\tScanMail requires a temporary directory for each of the"
        $ECHO "\tfour tasks installed."
        $ECHO "\n\tSpecify the name and full path of the directories you"
        $ECHO "\twant to use or press Enter to accept the default."
        $ECHO
        $ECHO "\tBe sure to use different directories if you will install"
        $ECHO "\tScanMail on partitioned servers, and write down the"
        $ECHO "\ttemporary directories that you have selected."
        $ECHO
        $ECHO "\tEnter the temporary directory for the Mail scanner"
        $ECHO "\t[ $INIPATH/smln/SMTemp/MailTemp/ ] : "
        $ECHO -n "\tPath > " ; $READ MAILTEMP
        $ECHO
        $ECHO "\tEnter the temporary directory for the Real-time scanner"
        $ECHO "\t[ $INIPATH/smln/SMTemp/RepTemp/ ] : "
        $ECHO -n "\tPath > " ; $READ REPTEMP
        $ECHO
        $ECHO "\tEnter the temporary directory for the Manual database scanner"
        $ECHO "\t[ $INIPATH/smln/SMTemp/DbTemp/ ] : "
        $ECHO -n "\tPath > " ; $READ DBTEMP
        $ECHO
        $ECHO "\tEnter the temporary directory for the Scheduled database scanner"
        $ECHO "\t[ $INIPATH/smln/SMTemp/PTemp/ ] : "
        $ECHO -n "\tPath > " ; $READ PTEMP
        $ECHO
    fi

    ## Notes ID to sign databases
    if [ $upgrade -ne 3 ] ; then
        idname=""
        idname=`grep -i "^ServerKeyFilename" $NOTESINI | cut -d= -f2`
	if [ "$idname" = "" ] ; then
	    idname=`grep -i "^KeyFilename" $NOTESINI | cut -d= -f2`	
            if [ "$idname" = "" ] ; then
              Echo "\n\tNo KeyFilename in notes.ini. Please check your setting."
	      if [ "$arch" = "os390" ] ; then
	        ebcdic2ascii #exit
	      fi
              exit 1
            fi
        fi
        idorgname=$idname

        head=`Echo $idname | cut -c1`
        if [ "$head" != "/" ] ; then
            idname=$INIPATH/$idname
        fi


        while [ ! -f $INIPATH/sm_esc_key.lck ]
        do
     	 \rm -f $tmpscript
         $ECHO "\n\tEnter the complete Notes ID path to sign databases"
         $ECHO -n "\t[ $idname ] : " ; $READ IDSIGN
         		if [ "$IDSIGN" != "" ] ; then
				tmpscript_t="$tmpscript"_test
				\rm -f "$tmpscript_t"
				Echo "test -w $IDSIGN" > "$tmpscript_t"
				Echo "if [ \$? -ne 0 ] ; then" >> "$tmpscript_t"
				Echo "echo 1" >> "$tmpscript_t"
				Echo "else" >>"$tmpscript_t"
				Echo "echo 0" >>"$tmpscript_t"
				Echo "fi" >>"$tmpscript_t"
				# test if LOTUSUSER has write access to idfile
				smchmod_own $tmpscript_t
				
				if [ "$arch" != "os390" ] ; then
				  ret=`su $LOTUSUSER -c "sh $tmpscript_t"`
				else
				  ret=`sh $tmpscript_t`
				fi   
				\rm -f "$tmpscript_t"
				if [ "$ret" = "0" ] ; then 
					SetIni d KeyFilename $idorgname
					SetIni a KeyFilename $IDSIGN
					if [ "$arch" != "os390 " ] ; then
					  Echo "PATH=$SOPATH:$INIPATH:$PATH" >>$tmpscript
					  \rm -f $TMPDIR/dbsetup  > /dev/null
					  ln -sf $LOTUS/bin/tools/startup $TMPDIR/dbsetup > /dev/null
					  Echo "cd $INIPATH" >>$tmpscript
					  Echo "$TMPDIR/dbsetup ${MAILTEMP:-"$INIPATH/smln/SMTemp/MailTemp/"} ${DBTEMP:-"$INIPATH/smln/SMTemp/DbTemp/"} ${REPTEMP:-"$INIPATH/smln/SMTemp/RepTemp/"} ${PTEMP:-"$INIPATH/smln/SMTemp/PTemp/"} \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\" "$INIPATH" "$INIPATH" \"${IDSIGN:-"NONE"}\" $upgrade $SKIPSIGN " >> $tmpscript
					else
				          Echo "PATH=$SOPATH:$INIPATH:$PATH" >>$tmpscript
					  Echo "export PATH" >>$tmpscript
					  Echo "cd $INIPATH" >>$tmpscript
					  Echo "$LOTUS/bin/dbsetup ${MAILTEMP:-"$INIPATH/smln/SMTemp/MailTemp/"} ${DBTEMP:-"$INIPATH/smln/SMTemp/DbTemp/"} ${REPTEMP:-"$INIPATH/smln/SMTemp/RepTemp/"} ${PTEMP:-"$INIPATH/smln/SMTemp/PTemp/"} \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\" "$INIPATH" "$INIPATH" \"${IDSIGN:-"NONE"}\" $upgrade $SKIPSIGN " >> $tmpscript
					fi
					
					smchmod_own $tmpscript
					
					if [ "$arch" != "os390" ] ; then
				          su - $LOTUSUSER -c "sh $tmpscript"
					  if [ "$?" != "0" -a -f $INIPATH/sm_esc_key.lck ] ; then
						Echo "\n\tThe automatic configuration failed."
						break
					  fi
					else
					  touch $INIPATH/sm_esc_key.lck 
					fi
				else
					$ECHO "\n\tNo access to id file."
				fi
			else
				SetIni d KeyFilename $IDSIGN
				SetIni a KeyFilename $idorgname
				if [ "$arch" != "os390" ] ; then
				  Echo "PATH=$SOPATH:$INIPATH:$PATH" >>$tmpscript
				  \rm -f $TMPDIR/dbsetup  > /dev/null
				  ln -sf $LOTUS/bin/tools/startup $TMPDIR/dbsetup > /dev/null
				  Echo "cd $INIPATH" >>$tmpscript
				  Echo "$TMPDIR/dbsetup ${MAILTEMP:-"$INIPATH/smln/SMTemp/MailTemp/"} ${DBTEMP:-"$INIPATH/smln/SMTemp/DbTemp/"} ${REPTEMP:-"$INIPATH/smln/SMTemp/RepTemp/"} ${PTEMP:-"$INIPATH/smln/SMTemp/PTemp/"} \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\" "$INIPATH" "$INIPATH" \"${IDSIGN:-"NONE"}\" $upgrade $SKIPSIGN " >> $tmpscript
				else
				  Echo "PATH=$SOPATH:$INIPATH:$PATH" >>$tmpscript
				  Echo "export PATH" >>$tmpscript
				  Echo "cd $INIPATH" >>$tmpscript
				  Echo "$LOTUS/bin/dbsetup ${MAILTEMP:-"$INIPATH/smln/SMTemp/MailTemp/"} ${DBTEMP:-"$INIPATH/smln/SMTemp/DbTemp/"} ${REPTEMP:-"$INIPATH/smln/SMTemp/RepTemp/"} ${PTEMP:-"$INIPATH/smln/SMTemp/PTemp/"} \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\" "$INIPATH" "$INIPATH" \"${IDSIGN:-"NONE"}\" $upgrade $SKIPSIGN " >> $tmpscript
				fi

				smchmod_own $tmpscript

				if [ "$arch" != "os390" ] ; then
				  su - $LOTUSUSER -c "sh $tmpscript"
                                  if [ "$?" != "0" -a -f $INIPATH/sm_esc_key.lck ] ; then
					Echo "\n\tThe automatic configuration failed."
					break
				  fi
				else  
				    touch $INIPATH/sm_esc_key.lck 
				fi
			fi
		done
		if [ -f $INIPATH/sm_esc_key.lck ] ; then
			\rm "$INIPATH/sm_esc_key.lck"
		fi
		if [ "$IDSIGN" != "" ] ; then
			SetIni d KeyFilename $IDSIGN
			SetIni a KeyFilename $idorgname
		fi
    fi
    if [ "$arch" = "os390" ] ; then
    	ebcdic2ascii
    fi

	if [ $upgrade -eq 2 ] ; then
		$ECHO -n "\n\tWould you like to replace database design(y/n)? " ;$READ yesno
		while [ ! \( "$yesno" = "y" -o "$yesno" = "Y" -o "$yesno" = "n" -o "$yesno" = "N" \) ]
		do
			$ECHO -n "\n\tPlease answer y or n : " ; $READ yesno
		done

		if [ "$yesno" = "y" -o "$yesno" = "Y" ]; then
		        if [ "$arch" != "os390" ] ; then
			  ln -sf $LOTUS/bin/tools/startup $TMPDIR/convert > /dev/null
			  Echo "cd $INIPATH" >$tmpscript
			  for i in smconf smvlog smquar smency
			  do
				Echo "$TMPDIR/convert $i.nsf \* $i.ntf" >>$tmpscript
			  done
			
			else
	                  Echo "PATH=$SOPATH:$INIPATH:$PATH" > $tmpscript
	                  Echo "export PATH" >> $tmpscript
	                  Echo "cd $INIPATH" >>$tmpscript
	                  for i in smconf smvlog smquar smency
                          do
                            Echo "$LOTUS/bin/convert $i.nsf \* $i.ntf" >>$tmpscript
                          done
	                fi

			smchmod_own $tmpscript

			if [ "$arch" != "os390" ] ; then
			  su - $LOTUSUSER -c "sh $tmpscript"
			fi
		fi
	fi
   ## Db migration
    if [ $upgrade -eq 1 ] ; then

	if [ "$arch" != "os390" ] ; then
	  Echo "\n\tUpgrading databases..."
          \rm -f $TMPDIR/dbmigrate > /dev/null
          ln -sf $LOTUS/bin/tools/startup $TMPDIR/convert > /dev/null
          ln -sf $LOTUS/bin/tools/startup $TMPDIR/dbmigrate > /dev/null

          Echo "cd $INIPATH" >$tmpscript
          for i in smconf smvlog smquar smency
          do
              Echo "$TMPDIR/convert $i.nsf \* $i.ntf" >>$tmpscript
          done
          Echo "$TMPDIR/dbmigrate "$INIPATH" \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\"" >>$tmpscript
	
	else
	  Echo "PATH=$SOPATH:$INIPATH:$PATH" >> $tmpscript
	  Echo "export PATH" >> $tmpscript
	  Echo "cd $INIPATH" >> $tmpscript
	  for i in smconf smvlog smquar smency
          do
              Echo "$LOTUS/bin/convert $i.nsf \* $i.ntf" >>$tmpscript
          done
          Echo "$LOTUS/bin/dbmigrate "$INIPATH" \"${LICENSESTR:-"NONE"}\" \"${LICENSEEMGR:-"NONE"}\"" >>$tmpscript
	fi

        smchmod_own $tmpscript
	
	if [ "$arch" != "os390" ] ; then
          su - $LOTUSUSER -c "sh $tmpscript"
          if [ "$?" != "0" ] ; then
            Echo "\n\tThe database migration failed."
          fi
	fi
    fi

    ## CM Agent
    if [ "$arch" != "os390" ] ; then
	if [ $upgrade -ne 2 ] ; then
	    CMAgentMsg 
	fi
    fi

    Echo "\n\n\tInstallation complete."
    if [ $arch = "os390" ] ; then
      \cp $tmpscript $INIPATH/smconfscript
      chown $LOTUSUSER $INIPATH/smconfscript
      chmod 0700 $INIPATH/smconfscript

      Echo "\n\n\tBefore restarting the Domino server:"
      Echo "\n\t1. Telnet to OpenEdition with the account that"
      Echo "\t   You use to run the Domino server."
      Echo "\t2. Change into $INIPATH."
      Echo "\t3. Run smconfscript in order to create and configure databases."
      Echo "\n\t You may then restart the Notes server."
    fi
    \rm -rf $TMPDIR

    return 0
}

RemoveSMLN()
{
    if [ -d $TMPDIR ] ; then
        \rm -fr $TMPDIR/*
    else
        mkdir $TMPDIR
    fi
if [ "$BLA" = "" -o $count2 -eq 1 ] ; then
    $ECHO "\n\tDo you still want to use ScanMail on another partitioned server?"
    $ECHO -n "\tIf you do not have a partitioned server, please enter \"n\". (y/n) " ; $READ psyesno
    count1=0
    while [ ! \( "$psyesno" = "y" -o "$psyesno" = "Y" -o "$psyesno" = "n" -o "$psyesno" = "N" \) ]
    do
        $ECHO
        $ECHO -n "\tPlease answer y or n : " ; $READ psyesno
        count1=`expr $count1 + 1`
        if [ $count1 -gt 2 ] ; then
            Echo "\n\tYou entered invalid answer to the question."
            exit 1
        fi
    done
    if [ "$psyesno" = "n" -o "$psyesno" = "N" ] ; then
        ( cd $SOPATH; \rm -f $exe_file )
        ( cd $SOPATH; \rm -f $so_file )
        ( cd $SOPATH; \rm -f $emcore_file )
        ( cd $SOPATH; \rm -f $emicu_file )
        ( cd $SOPATH; \rm -f $emdmc_file )
        ( cd $SOPATH; \rm -f $emclm_file )
        ( cd $SOPATH; \rm -f $emlog_file )
        if [ "$arch" != "aix" ] && [ "$arch" != "os390" ] ; then
            ( cd $SOPATH; \rm -fr base2 )
        fi
    fi
fi
    ( cd $INIPATH; \rm -f $install_db )
    ( cd $INIPATH; \rm -f $remove_db )
    ( cd $INIPATH; \rm -f $noupdate_db )
    ( cd $INIPATH; \rm -f $r5only_db )
    ( cd $INIPATH; \rm -fr smln )
    ( cd $INIPATH; \rm -fr smlog )
    ( cd $INIPATH; \rm -f $emini_file $emini_file*)

    if [ "$arch" = "os390" ] ; then
    	ascii2ebcdic
    fi

    PostProcess

    SetIni r ServerTasks tmmscan
    SetIni r ServerTasks repscan
    SetIni r EXTMGR_ADDINS smlnred
    SetIni d SmLicense
    SetIni d SMInternet_LocalDomain
    SetIni d SMRelay_User
    SetIni d SMPatternCheck_Interval
    SetIni d SMScanMem
    SetIni d SMStopMail
    SetIni d SMOutputlevel
    SetIni d FileDlgDirectory

    if [ "$arch" = "os390" ] ; then
    	ebcdic2ascii
    fi

    \rm -rf $TMPDIR
    Echo "\n\tScanMail for Lotus Notes has been uninstalled."


    return 0
}

ShowLicense()
{
    if [ ! -s $AGREEMENT ] ; then
        Echo "\tYou must have $AGREEMENT in current directory. Exiting..."
        exit 1
    fi
    if [ ! -r $AGREEMENT ] ; then
        chmod 444 $AGREEMENT
        if [ $? -ne 0 ] ; then
            Echo "\tPlease add read access right to $AGREEMENT"
            exit 1
        fi
    fi

    $ECHO
    $MORE $AGREEMENT 
    $ECHO "\n\tTo install ScanMail for Lotus Notes, you must accept this agreement."
    $ECHO -n "\n\tDo you accept all the terms of the preceding License Agreement(y/n)? " ;$READ yesno

    while [ ! \( "$yesno" = "y" -o "$yesno" = "Y" \) ]
    do
        if [ "$yesno" = "n" -o "$yesno" = "N" ]; then
            Echo "\n\tYou have to accept the agreement to start install."
            exit 1
        fi

        $ECHO -n "\n\tPlease answer y or n : " ; $READ yesno
    done

    return 0
}

CommonPart()
{
    $ECHO "\n\tPlease enter the name of the UNIX account you use"
    $ECHO -n "\tto run the Notes server : " ; $READ LOTUSUSER

    if [ "$arch" != "sol" ] ; then
      LOTUSUID=`id -u $LOTUSUSER`
    else
      if [ -x /usr/xpg4/bin/id ] ; then
        LOTUSUID=`/usr/xpg4/bin/id -u $LOTUSUSER`
      else
        LOTUSUID=`id  $LOTUSUSER|cut -f2 -d=|cut -f1 -d\(`
      fi
    fi
    
if [ "$arch" != "os390" ] ; then
    LOTUSUSERPATH=`awk 'BEGIN { FS = ":" } $1 == "'$LOTUSUSER'" { print $6 }' /etc/passwd`

    if [ "$LOTUSUSERPATH" = "" ] ; then
        $ECHO "\n\tPlease enter the name of the UNIX account you use"
        $ECHO -n "\tto run the Notes server : " ; $READ LOTUSUSER
        LOTUSUSERPATH=`awk 'BEGIN { FS = ":" } $1 == "'$LOTUSUSER'" { print $6 }' /etc/passwd`
        if [ "$LOTUSUSERPATH" = "" ] ; then
            Echo "\n\tThe UNIX account is invalid. Exiting..."
            exit 1
        fi
    fi
      geoupid=`awk 'BEGIN { FS = ":" } $1 == "'$LOTUSUSER'" { print $4 }' /etc/passwd`
      groupname=`awk 'BEGIN { FS = ":" } $3 == "'$geoupid'" { print $1 }' /etc/group`
    
    if [ $setuptype -eq 1 ] ; then
        $ECHO "\n\tPlease enter the name of the UNIX group. The UNIX account"
        $ECHO -n "\tmust be included in the group. [ $groupname ] : " ; $READ LOTUSGROUP

        if [ "$LOTUSGROUP" = "" ] ; then
            LOTUSGROUP=$groupname
        fi

        res=`awk 'BEGIN { FS=":" } $1 == "'$LOTUSGROUP'"' /etc/group`
        if [ "$res" = "" ] ; then
            Echo "\n\tThe UNIX group is invalid. Exiting..."
            exit 1
        fi
    fi
fi
    cd $LOTUSUSERPATH 2> /dev/null

    if [ $silentmode -eq 0 ] ; then
        # Support directory name with blank under $LOTUSUSERPATH
        ini_list=`find $datasearchpath -name notes.ini -type f -print 2> /dev/null | awk 'BEGIN {ORS=":"} {print $0}'`
    else
        # $INIPATH is already set as parameter of sminst
        ini_list=""
    fi

    count1=1
    count2=0
	countR6=0
	countR5=0

    if [ "$ini_list" != "" ] ; then
        inipath_list=""
        while :
        do
            Ini=`Echo $ini_list | cut -f$count1 -d:`
            if [ "$Ini" = "" ] ; then
                break
            fi

            Help=`dirname "$Ini"`
            cd "$Help"
            DIR=`pwd`

            if [ $setuptype -eq 2 ] ; then
                grep -i smlnred "$DIR"/notes.ini | grep -i EXTMGR_ADDINS > /dev/null
                if [ "$?" = "0" ] ; then
                    inipath_list=$inipath_list$DIR!
                    count2=`expr $count2 + 1`
						grep "^NSF_QUOTA_METHOD" $Help/notes.ini > /dev/null
						if [ "$?" = "0" ] ; then
							countR6=`expr $countR6 + 1`
						else
							countR5=`expr $countR5 + 1`
						fi
                fi
            else
                count2=`expr $count2 + 1`
                inipath_list=$inipath_list$DIR!
            fi   
            cd $LOTUSUSERPATH 2> /dev/null
           count1=`expr $count1 + 1`
        done

        if [ $count2 -ge 1 ] ; then
            $ECHO "\n\tNumber | \tPath of notes.ini "
            $ECHO "\t-------|---------------------------------"
            count1=1
            while [ $count1 -le $count2 ]
            do
                DIR=`Echo $inipath_list | cut -f$count1 -d!`
                $ECHO "\t ["$count1"]   | $DIR/notes.ini\t "
                count1=`expr $count1 + 1`
            done
            $ECHO "\t ["$count1"]   | others"
            $ECHO  -n "\n\tPlease enter the number of your Notes data directory(1-$count1) : " ; $READ BLA
        while :
        do
                expr "$BLA" + 1 > /dev/null 2>&1
                if [ $? -ge 2 ] ; then
                    BLA=0  # this is not number
                fi

                if [ $BLA -gt 0 -a $BLA -le $count1 ] ; then
                    break
                fi
                $ECHO -n "\n\tPlease select valid number(1-$count1) : " ; $READ BLA
        done
            INIPATH=`Echo $inipath_list | cut -f$BLA -d!`
        fi
    fi

    if [ "$INIPATH" = "" ] ; then
        $ECHO "\n\tPlease enter the complete Notes data directory path :"
        $ECHO -n "\tPath > " ; $READ INIPATH
    fi

    cd "$INIPATH"
    INIPATH=`pwd`
    cd $WORKDIRECTORY

    NOTESINI="$INIPATH/notes.ini"
    NOTESOWNER=`ls -nl "$NOTESINI" |awk '{print $3}'`

    if [ ! -s $NOTESINI ] ; then
        Echo "\n\tUnable to find the notes.ini file. Exiting ..."
        exit 1
    fi

    if [ "$arch" = "os390" ] ; then
	  ascii2ebcdic
    fi

	grep "^NSF_QUOTA_METHOD" $NOTESINI > /dev/null
	if [ "$?" = "0" ] ; then
		count2=$countR6
	else
		count2=$countR5
	fi

    NOTESDIR=`grep -i "^Directory=" "$NOTESINI" | cut -d= -f2`
    if [ "$NOTESDIR" != "$INIPATH" ] ; then
        Echo
        Echo "\tThe Notes data directory is incorrect. Please check"
        Echo "\tthe notes.ini of your Notes server. Exiting ..."

	if [ "$arch" = "os390" ] ; then
	  ebcdic2ascii #exit
	fi

        exit 1
    fi
    
    if [ "$NOTESOWNER" != "$LOTUSUID" ] ; then
        Echo
        Echo "\tYou have entered an incorrect name of the UNIX"
        Echo "\taccount you use to run the Notes server."
        Echo
        Echo "\tPlease check your Notes server configuration. Exiting..."
	if [ "$arch" = "os390" ] ; then
	  ebcdic2ascii #exit
	fi
	exit 1
    fi

    # Domino 6 could have no symbolic link to /opt/lotus.
    # Get program directory path from notes.ini
    lotusdir=`grep -i "^NotesProgram=" "$NOTESINI" | cut -d= -f2`
    if [ "$lotusdir" != "" ] ; then
        SOPATH=$lotusdir
        tmpstr=`dirname $SOPATH`
        tmpstr=`dirname $tmpstr`
        LOTUS=`dirname $tmpstr`
    else
        SOPATH=$tmp_sopath
    fi

    if [ "$arch" = "os390" ] ; then
	  ebcdic2ascii
    fi

    return 0
}

SetParam()
{
    if [ "$SMTEMP" = "" ] ; then
        SMTEMP=/tmp
    fi

    if [ ! -d "$SMTEMP" ] ; then
        $ECHO "\t$SMTEMP does not exist."
        exit 1
    fi

    TMPDIR=$SMTEMP/vlotus
    iniscript=$TMPDIR/iniscript
    tmpscript=$TMPDIR/tmpscript

    BLA=""
    upgrade=0

    ## All files are set here
    exe_file="dbscan pscan repscan tmmscan pupdate dbsetup dbmigrate delpat"
    auini_file="aucfg.ini"
    install_db="smftypes.ntf smconf.ntf smency.ntf smhelp.ntf smquar.ntf smvlog.ntf smmsg.nsf warning.bmp warning_big.bmp macr\$gen.600 lpt\$vpn.*"
    remove_db="smftypes.nsf smconf.nsf smency.nsf smhelp.nsf smquar.nsf smvlog.nsf smbackup.nsf smmsg.ntf"
    noupdate_db="smtime.nsf"
    r5only_db="smadmR5.nsf"

    ## eManager files
    emini_file="TMeMgr.ini"
    em_db_file="TM_AntiSpam.* TM_Trend\$SE.*"

    case $arch in
    os390)
# check whether the LOTUS variable is empty or contains the setting for the other UNICES
		if [ "$LOTUS" = "" ] ; then
			LOTUS="/usr/lpp/lotus"
		fi
		if [ "$LOTUS" = "/opt/lotus" ] ; then
			LOTUS="/usr/lpp/lotus"
		fi
        ;;
    *)
		if [ "$LOTUS" = "" ] ; then
			LOTUS="/opt/lotus"
		fi
    ;;
    esac
## Simple check for path beeing accurate
	if [ ! -d "$LOTUS" ] ; then
		$ECHO "\n\tLotus base directory ($LOTUS) not valid"
		if [ $silentmode -eq 1 ]; then
			$ECHO "\tInstallation aborted"
			exit 1
		fi
		$ECHO -n "\tPlease enter valid path: "; $READ LOTUS
		if [ ! -d "$LOTUS" ] ; then
			$ECHO "\n\tLotus base directory ($LOTUS) not valid"
			$ECHO "\tInstallation aborted"
			exit 1
		fi
	fi

    case $arch in
    aix)
        so_file="libsmlnred.a libr5open.a libvsapicall.a libsmlneman.a libtmactupdate.so"
        engine_file="libvsapi.o"
	au_file="AuPatch libsmlnaupdate.a"
        emcore_file="libem_core.so libem_helpr.so"
        emicu_file="libicuuc.a libicudt18b.a libicui18n.a"
        emdmc_file=""
        emclm_file="SLF2.ldf"
        emlog_file=""
        tmp_sopath="$LOTUS/notes/latest/ibmpow"
        datasearchpath="/local/"
        ;;
    sol)
        so_file="libsmlnred.so libr5open.so libvsapicall.so libsmlneman.so libtmactupdate.so"
        engine_file="libvsapi.so"
        au_file="AuPatch libsmlnaupdate.so"
        emcore_file="libem_core.so libem_helpr.so libem_hprex.so"
        emicu_file="libicuuc.so.18 libicudt18b.so libicui18n.so.18"
        emdmc_file="libdmc_comm.so libdmc_conf.so libdmc_dtct.so libdmc_txdoc.so libdmc_txif.so dmc_txli.dat libdmc_txli.so libdmc_txppt.so libdmc_txxls.so"
        emclm_file="libi18n.so.1 liblogmgt.so.1 liblogrdr.so.1 liblogshr.so.1 liblogwtr.so.1 liblowlib.so.1 SLF2.ldf"
        emlog_file="libTmIntLog.so libtmlogwpr.so"
        tmp_sopath="$LOTUS/notes/latest/sunspa"
	emcore_file="$emcore_file libMessage.so"
	ospatchfile="SunOS_5.5.1.tar"
        datasearchpath="/local/"
        ;;
    linux)
        so_file="libsmlnred.so libr5open.so libvsapicall.so libsmlneman.so libtmactupdate.so"
        engine_file="libvsapi.so"
        au_file="AuPatch libsmlnaupdate.so"
        emcore_file="libem_core.so libem_helpr.so libem_hprex.so"
        emicu_file="libicuuc.so.18 libicudt18l.so libicui18n.so.18"
        emdmc_file="libdmc_comm.so libdmc_conf.so libdmc_dtct.so libdmc_txdoc.so libdmc_txif.so dmc_txli.dat libdmc_txli.so libdmc_txpdf.so libdmc_txppt.so libdmc_txxls.so"
        emclm_file="libi18n.so.1 liblogmgt.so.1 liblogrdr.so.1 liblogshr.so.1 liblogwtr.so.1 liblowlib.so.1 SLF2.ldf"
        emlog_file="libTmIntLog.so libtmlogwpr.so"
        tmp_sopath="$LOTUS/notes/latest/linux"
	emcore_file="$emcore_file libMessage.so libstdc++-libc6.1-2.so.3"
        datasearchpath="/local/"
        ;;
    os390)
        so_file="libsmlnred libr5open libvsapicall libsmlneman libtmactupdate.so libmessage"
        engine_file="libvsapi"
	au_file="AuPatch libsmlnaupdate"
        emcore_file="libem_core.so libem_helpr.so"
        emicu_file="libicuuc.dll libicudt18e_390.dll"
        emdmc_file=""
        emclm_file=""
        emlog_file=""
        tmp_sopath="$LOTUS/notes/latest/os390"
        datasearchpath="/u/notesserver/"
        ;;
    esac

## Simple check if the executable path does exist at all
	if [ ! -d "$tmp_sopath" ] ; then
		$ECHO "\n\tLotus program directory ($tmp_sopath) not valid"
		$ECHO "\tInstallation aborted"
		exit 1
	fi

    return 0
}

# $1 - variable name to set value
# $2 - item name in setting file
# ex. SetData sm_unix_user LOTUSUSER
SetData()
{
    grep "^$2=" $settingfile >> /dev/null
    if [ $? -ne 0 ] ; then
        $ECHO "\tNo $2 entry in $settingfile. Exiting.."
        exit 1
    fi
    
    value=`grep "^$2=" $settingfile | cut -d= -f2`

    eval $1=$value

    return 0
}

GetParam()
{

    # Check the existence of parameters
    if [ $# -eq 0 ] ; then
        $ECHO "\n\t1 ..... Install ScanMail for Lotus Notes"
        $ECHO "\t2 ..... Uninstall ScanMail for Lotus Notes"
        $ECHO -n "\n\tPlease select the number(1, 2) : " ; $READ setuptype
        expr "$setuptype" + 1 > /dev/null 2>&1
        if [ $? -ge 2 ] ; then
            setuptype=0  # this is not number
        fi
        # $setuptype
        # 1: install 2: uninstall/remove
        if [ $setuptype -ne 1 -a $setuptype -ne 2 ] ; then
            $ECHO "\n\tInvalid choice. Exiting..."
            exit 1
        fi

        return 0
    fi

    case $1 in
        install) setuptype=1 ;;
        remove)  setuptype=2 ;;
        *)       $ECHO "\tUsage: sminst [ install | remove ] [ -s SETTINGFILE ]" ; exit 1 ;;
    esac

    if [ $# -eq 1 ] ; then
        return 0
    fi

    # Parameters for non interactive setup
    shift 1

    while getopts s: options
    do
        case $options in
        s) # Setting file name
            settingfile=$OPTARG
            ;;
        \?)
            $ECHO "\tUsage: sminst [ install | remove ] [ -s SETTINGFILE ]"
            exit 1
            ;;
        esac
    done
    shift `expr $OPTIND - 1`

    if [ ! -f $settingfile ] ; then
        $ECHO "\t$settingfile does not exist"
        exit 1
    elif [ ! -r $settingfile ] ; then
        $ECHO "\t$settingfile is read-only"
        exit 1
    fi

    SetData SKIPSIGN sm_skip_sign
    SetData LOTUS sm_lotus_dir
    SetData LOTUSUSER sm_unix_user
    SetData INIPATH sm_ini_path
    SetData LOTUSGROUP sm_unix_group
    SetData LICENSESTR sm_serial_number
    SetData LICENSEEMGR sm_serial_number_em
    SetData MAILTEMP sm_mailtemp
    SetData REPTEMP sm_reptemp
    SetData DBTEMP sm_dbtemp
    SetData PTEMP sm_ptemp
    SetData IDSIGN sm_id_sign
    SetData psyesno sm_partitioned_server

    silentmode=1
    ECHO=:
    READ=:
    MORE=:

    yesno="y"

    return 0
}

CheckState()
{
    WORKDIRECTORY=`pwd`

    # figure out the platform
    if [ ! -x /usr/bin/uname ] ; then
        if [ ! -x /bin/uname ] ; then
            $ECHO "Unknown OS, cannot continue"
            exit 1
        else
            arch=`/bin/uname`
        fi
    else
        arch=`/usr/bin/uname`
    fi

    case $arch in
        SunOS) arch=sol ;;
        AIX)   arch=aix ;;
        Linux) arch=linux ;;
	OS/390) arch=os390 ;;
        *)     $ECHO "Unknown OS: $arch, cannot continue"
               exit 1 ;;
    esac
    if [ "$arch" != "os390" ] ; then
       id | cut -d' ' -f1 | grep root > /dev/null 2>&1

       if [ $? -ne 0 ]; then
          $ECHO "You must be super-user (root) to execute this script."
          exit 1
       fi
    fi
    
    if [ "$arch" = "os390" ] ; then
       id | grep uid=0 > /dev/null 2>&1

       if [ $? -ne 0 ]; then
          $ECHO "You must be super-user (uid=0) to execute this script."
          exit 1
       fi
    fi


    case $arch in
        linux) nspsid=`ps -efw|fgrep lotus|fgrep notes|fgrep server` ;;
        *)     nspsid=`ps -ef|fgrep lotus|fgrep notes|fgrep server` ;;
    esac

    if [ "$nspsid" != "" ] ; then
        $ECHO "\n\tPlease shut down the Notes server before running sminst."
        exit 1
    fi

    return 0
}

Main()
{
    silentmode=0

    CheckState

    GetParam "$@"

    SetParam

    if [ $setuptype -eq 1 ] ; then
        ShowLicense
        CommonPart
        InstallSMLN
    elif [ $setuptype -eq 2 ] ; then
        CommonPart
        RemoveSMLN
    fi

    exit 0
}

Main "$@"
