#!/bin/ksh
#ident "@(#)x400dbrecover.sh	1.10 07/02/97 SMI"
#
#
# Copyright 07/02/97 Sun Microsystems, Inc. All Rights Reserved
#
#
# This script will stop the MHS running processes and try to backup
# the MTA database.
#

OSIPATH=""
EXEPKG=SUNWx4srv	# pkg that install the executables
BASEDIR=`/bin/pkgparam $EXEPKG BASEDIR 2>/dev/null`
if [ -z "$BASEDIR" ]	# pkg not installed
then
	BASEDIR=/opt	# set defaul. Maybe the dir is nfs mounted
fi
OSIPATH=$BASEDIR/SUNWconn/mhs/sbin:$BASEDIR/SUNWconn/sbin:$BASEDIR/SUNWconn/lib

PATH="/usr/bin:/bin:/usr/sbin:$OSIPATH:" ; export PATH 

OSIROOT_FILE=/etc/opt/SUNWconn/OSIROOT

#
#	get OSIROOT, if not already defined in the environment
#

if [ -z "$OSIROOT" ]
then
	if [ -r $OSIROOT_FILE ]
	then
		OSIROOT=`cat $OSIROOT_FILE` 2>&1 > /dev/null 
	else
		echo "Can't find file: $OSIROOT_FILE"; exit 1
	fi
fi

#	Check: OSIROOT must be a directroy

if [ ! -d "$OSIROOT" ]
then
	echo "No such directory: <$OSIROOT> "; exit 1
fi

ans=`ckyorn -e "please answer y or n: " \
            -h "please answer y or n:" \
            -p "This script will stop the X.400 running processes,\n \
                regenerate the configuration using master.mhscf.text, \n \
                change the content of the MTA temporary spool, \n \
                and restart the processes. \n \
                Do you really want to continue ? "`


if [ $ans = "y" -o $ans = "yes" -o $ans = "Y"  \
  -o $ans = "YES" -o $ans = "Yes" ]
then
  echo "Checking for running processes ..."
  X4TL=`ps -ef | grep "x400tool" | grep -v grep | awk '{ print $2 }' `
  if [ "$X4TL" != "" ]
  then
    echo "Found that x400tool is running ..."
    PX4TL=$X4TL
  fi
  echo "Checking for running processes ..."
  OSIMTA=`ps -ef | grep "osimta osimta osimta" | grep -v grep `
  if [ "$OSIMTA" != "" ]
  then
    echo "Found that MTA is running ..."
    POSIMTA=osimta
  fi
  OSIMS=`ps -ef | grep "osix400ms osix400ms osix400ms" | grep -v grep `
  if [ "$OSIMS" != "" ]
  then
    echo "Found that MS is running ..."    
    POSIMS=osix400ms
  fi
  OSISMTP=`ps -ef | grep "osismtpx400 osismtpx400 osismtpx400" | grep -v grep`
  if [ "$OSISMTP" != "" ]
  then
    echo "Found that Internet Adaptator is running ..."    
    POSISMTP=osismtpx400
  fi
  OSIMQA=`ps -ef | grep "osix400mqa osix400mqa osix400mqa" | grep -v grep`
  if [ "$OSIMQA" != "" ]
  then
    echo "Found that Message Queue Interface is running ..."
    POSIMQA=osix400mqa
  fi
  for prc in $POSIMTA $POSIMS $POSISMTP $POSIMQA
  do 
    echo " Stopping $prc ..."
    x400stop $prc
  done
  kill $PX4TL
  OSIMTA=""
  OSIMTA=`ps -ef | grep "osimta osimta osimta" | grep -v grep `
  if [ "$OSIMTA" != "" ]
  then
    echo "The MTA process is still running \n \
          Stop it manually then restart this script"
    exit 1
  fi
  echo "This script will create a directory in /var/tmp/x4r \n \
        to store saved messages temporarily. "
  ans=""
  ans=`ckyorn -e "please answer y or n: " \
              -h "please answer y or n:" \
              -p "Do you want to select a different directory ? "`

  if [ $ans = "y" -o $ans = "yes" -o $ans = "Y"  \
       -o $ans = "YES" -o $ans = "Yes" ]
  then
     echo "Please, give the pathname of the directory to use \n \
           (must be on /var): \c"
     read tmpdir
     if [ ! -d "$tmpdir" ]
     then
       ans=""
       ans=`ckyorn -e "please answer y or n: " \
              -h "please answer y or n:" \
              -p "This directory does not exist; create directory ? "`

       if [ $ans = "y" -o $ans = "yes" -o $ans = "Y"  \
           -o $ans = "YES" -o $ans = "Yes" ]
       then
          mkdir -p $tmpdir
          if [ $? != 0 ]
          then
             echo "Problem while creating $tmpdir"
             exit 1
          fi 
       fi
     fi
  else
     tmpdir=/var/tmp/x4r
     if [ -d $tmpdir ]
     then
        ans=""
        ans=`ckyorn -e "please answer y or n: " \
              -h "please answer y or n:" \
              -p "This script will erase everything under $tmpdir, \n \
                  Do you want to continue ? "`

        if [ $ans = "y" -o $ans = "yes" -o $ans = "Y"  \
             -o $ans = "YES" -o $ans = "Yes" ]
        then
           (
              cd $tmpdir
              dir=`pwd`
              if [ $tmpdir != $dir ]
              then
                 echo "Problem while accessing $tmpdir"
                 exit 1
              else
                 rm -rf $tmpdir/*
              fi
            )
        else
           echo "Exiting script ..."
           exit 1
        fi
     else
        mkdir -p $tmpdir
        if [ $? != 0 ]
        then
           echo "Problem while creating $tmpdir"
           exit 1
        fi        
     fi
  fi
  x400dbsave -i $OSIROOT/spool -o $tmpdir
  RESULT=$? 
  if [ $RESULT -ge 0 ]
  then
     if [ $RESULT -gt 0 ]
     then
       echo "Problem while saving database"
       ans=""
       ans=`ckyorn -e "please answer y or n: " \
              -h "please answer y or n:" \
              -p "Regarding the previous errors, do you want to try to \n \
                  regenerate the database (This can destroy all your messages) ? "`
       if [ "$ans" = "n" -o "$ans" = "no" -o "$ans" = "N"  \
             -o "$ans" = "NO" -o "$ans" = "No" -o -z "$ans" ]

       then
             exit 1
       fi
     fi 
     GENCONF_MODE=QUIET  x400_genconf $OSIROOT/mhs/conf/master.mhscf.text MTA
     if [ $? != 0 ]
     then
       echo "Problem while regenerating database"
       echo "Please try to find a working configuration file and \n \
               run the following scripts (without removing saved messages):\n"
       echo  "x400_genconf <your_configuration_file> MTA \n \
                x400dbload -i $tmpdir -o $OSIROOT/spool \n \
                x400start $POSIMTA $POSIMS $POSISMTP $POSIMQA "
       exit 1
     else
       x400dbload -i $tmpdir -o $OSIROOT/spool
       if [ $? != 0 ]
       then
          echo "Problem while restoring messages"
          echo "Check that the generated configuration is the current one and\n\  
                run the following scripts (without removing saved messages) :\n"
          echo "x400_genconf <current_configuration_file> MTA \n \
                x400dbload -i $tmpdir -o $OSIROOT/spool \n \
                x400start $POSIMTA $POSIMS $POSISMTP $POSIMQA "
          exit 1
       else
          if [ "$POSIMTA $POSIMS $POSISMTP $POSIMQ" != "   " ]
          then
             x400start $POSIMTA $POSIMS $POSISMTP $POSIMQA
          fi
       fi
     fi
  else
    echo "Problem while trying to save database"
    exit 1
  fi
fi
