#!/usr/bin/ksh
# ------------------------------------------------------------------------------
#  ident "@(#)nhsmctsetup 1.11     02/11/04 SMI"
# ------------------------------------------------------------------------------
# SMCT environment setup script
# ------------------------------------------------------------------------------

# Command definitions
RM=/usr/bin/rm
MKDIR=/usr/bin/mkdir
CP=/usr/bin/cp
CAT=/usr/bin/cat
CHMOD=/usr/bin/chmod
FIND=/usr/bin/find
DIRNAME=/usr/bin/dirname
BASENAME=/usr/bin/basename
LS=/usr/bin/ls
SED=/usr/bin/sed
AWK=/usr/bin/awk
PKGINFO=/usr/bin/pkginfo
GREP=/usr/bin/grep
CUT=/usr/bin/cut

# To define PERL environment  
USER_PERL=""
PERL_VER=5.

# Global variables
SMCTDIR=""
SMCTINSDIR=""
SMCTPRODIR=""
SMCTPKGDIR=""
SMCTPATDIR=""
SMCTSOLDIR=""
STATUS=0
SLENV=0
FLENV=0
PATCH=0
DUPLI=0
YES=""

# Set umask for directory creation
umask 022

# ------------------------------------------------------------------------------

info () {
   print "INFO: - nhsmctsetup - "$*
}

# -----------------------------------------------------------------------------

query () {
   print
   print -n "QUERY: - nhsmctsetup - "$*
}

# -----------------------------------------------------------------------------

error () {
   print
   print "ERROR: - nhsmctsetup - "$*
   STATUS=1
   return $STATUS
}

# -----------------------------------------------------------------------------

warning () {
   print "WARNING: - nhsmctsetup - "$*
}

# -----------------------------------------------------------------------------

help () {
   print
   print "HELP: - nhsmctsetup - "$*
}

# -----------------------------------------------------------------------------

getAbsolutePath () {
   BACK=$PWD
   cd $1
   pwd
   cd $BACK
}

# -----------------------------------------------------------------------------

yes_or_no () {
   YES=""
   while [ -z "$YES" ]
   do
      read YES
      case $YES in
         y | yes ) YES="yes"
	 ;; 
         n | no ) YES="no"
	 ;;
	 q | quit ) exit $STATUS
	 ;;
	 * ) query "Please, enter [ y[es] | n[o] | q[uit] ] : " 
	     YES=""
	 ;;
      esac
   done
}

# -----------------------------------------------------------------------------

checkEnv () {
   STATUS=0
   # Check the product installation
   if [ ! -f "${SMCTPRODIR}/nhsmct/bin/smct.pl" ] 
   then
      error "Directory < ${SMCTPRODIR} > does not point to a valid NHAS product"
      return $STATUS
   fi
   SMCTVER=`${PKGINFO} -l SUNWnhsmc | grep VERSION | awk '{print $2}'`
   # Check the perl environment
   TEST_PERL=`type perl`
   if [ $? -eq 0  ]
   then
      USER_PERL=${TEST_PERL##(perl is )}
      VERSION=`${USER_PERL} -v | ${GREP} "This is perl" | ${GREP} ${PERL_VER}`
      if [ -z "$VERSION" ]
      then
         error "< $USER_PERL > must have a version greater or equal to < ${PERL_VER} >"
         return $STATUS
      fi
   else
      error "< perl > is not defined in the current environment"
      return $STATUS
   fi
   return $STATUS
}

# -----------------------------------------------------------------------------

createEnv () {
   STATUS=0
   $MKDIR -p $SMCTDIR/tmp
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/tmp"
   $MKDIR -p $SMCTDIR/scripts
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/scripts"
   $MKDIR -p $SMCTDIR/config/services 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/config/services"
   $MKDIR -p $SMCTDIR/config/jumpstart 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/config/jumpstart"
   $MKDIR -p $SMCTDIR/config/hardware 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/config/hardware"
   $MKDIR -p $SMCTDIR/config/models 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/config/models"
   $MKDIR -p $SMCTDIR/export 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/export"
   $MKDIR -p $SMCTDIR/flash 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/flash"
   $MKDIR -p $SMCTDIR/jumpstart 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/jumpstart"
   $MKDIR -p $SMCTDIR/softrepo 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/softrepo"
   $MKDIR -p $SMCTDIR/swlrepo 
   [ $? -eq 0 ] || error "Cannot $MKDIR -p $SMCTDIR/swlrepo" 
   return $STATUS
}

# -----------------------------------------------------------------------------

adaptScripts () {
   SMCTENV=$SMCTDIR/scripts/smct.env
   if [ -f $SMCTENV ]
   then
      $CP $SMCTENV /tmp
      $CAT /tmp/smct.env | \
      eval $SED -e 's,SMCT_ENV_DIR=.*\;,SMCT_ENV_DIR=${SMCTDIR}\;,' | \
      eval $SED -e 's,SMCT_SOL_DIR=.*\;,SMCT_SOL_DIR=${SMCTSOLDIR}\;,' > $SMCTENV
   else
      error "The file < ${SMCTENV} > does not exist"
   fi
}

# -----------------------------------------------------------------------------

genScripts () {
   STATUS=0
   $CAT > $SMCTDIR/scripts/smct.env <<EOF
#!/bin/ksh
# This is a generated file - do not modify -

# Unix commands paths
#
SMCT_BASENAME=/usr/bin/basename; export SMCT_BASENAME
SMCT_CAT=/usr/bin/cat; export SMCT_CAT
SMCT_CKSUM=/usr/bin/cksum; export SMCT_CKSUM
SMCT_CP=/usr/bin/cp; export SMCT_CP
SMCT_DD=/usr/bin/dd; export SMCT_DD
SMCT_DATE=/usr/bin/date; export SMCT_DATE
SMCT_DIFF=/usr/bin/diff; export SMCT_DIFF
SMCT_DIRNAME=/usr/bin/dirname; export SMCT_DIRNAME
SMCT_EGREP=/usr/bin/egrep; export SMCT_EGREP
SMCT_FIND=/usr/bin/find; export SMCT_FIND
SMCT_FLAR=/usr/sbin/flar; export SMCT_FLAR
SMCT_FLARCREATE=/usr/sbin/flarcreate; export SMCT_FLARCREATE
SMCT_GREP=/usr/bin/grep; export SMCT_GREP
SMCT_GZIP=/usr/bin/gzip; export SMCT_GZIP
SMCT_KSH=/usr/bin/ksh; export SMCT_KSH
SMCT_LS=/usr/bin/ls; export SMCT_LS
SMCT_MV=/bin/mv; export SMCT_MV
SMCT_MKDIR=/usr/bin/mkdir; export SMCT_MKDIR
SMCT_MOUNT=/usr/sbin/mount; export SMCT_MOUNT
SMCT_PATCHADD=/usr/sbin/patchadd; export SMCT_PATCHADD
SMCT_PERL=$USER_PERL; export SMCT_PERL
SMCT_PING=/usr/sbin/ping; export SMCT_PING
SMCT_PKGTRANS=/usr/bin/pkgtrans; export SMCT_PKGTRANS
SMCT_PKGADD=/usr/sbin/pkgadd; export SMCT_PKGADD
SMCT_PWD=/usr/bin/pwd; export SMCT_PWD
SMCT_RCP=/usr/bin/rcp; export SMCT_RCP
SMCT_RM=/bin/rm; export SMCT_RM
SMCT_RSH=/usr/bin/rsh; export SMCT_RSH
SMCT_SED=/usr/bin/sed; export SMCT_SED
SMCT_SH=/usr/bin/sh; export SMCT_SH
SMCT_SHARE=/usr/sbin/share; export SMCT_SHARE
SMCT_TAR=/usr/bin/tar; export SMCT_TAR
SMCT_TEE=/usr/bin/tee; export SMCT_TEE
SMCT_UMOUNT=/usr/sbin/umount; export SMCT_UMOUNT
SMCT_UNSHARE=/usr/sbin/unshare; export SMCT_UNSHARE
SMCT_UUENCODE=/usr/bin/uuencode; export SMCT_UUENCODE

# Messages catalogues path for i18N support
#
if [ $LANG ] 
then
   NLSPATH=/opt/SUNWcgha/nhsmct/lib/locale/${LANG}/LC_MESSAGES/nhsmct.mo; export NLSPATH
else
   NLSPATH=/opt/SUNWcgha/nhsmct/lib/locale/${LC_MESSAGES}/LC_MESSAGES/nhsmct.mo; export NLSPATH
fi

# SMCT framework variables
#
EOF

   $CAT >> $SMCTDIR/scripts/smct.env <<EOF
SMCT_SOL_DIR=$SMCTSOLDIR; export SMCT_SOL_DIR
SMCT_ENV_DIR=$SMCTDIR; export SMCT_ENV_DIR
NHAS_PROD_DIR=$SMCTPRODIR; export NHAS_PROD_DIR
SMCT_ETC_DIR=$SMCTPRODIR/nhsmct/etc; export SMCT_ETC_DIR
SMCT_MODELS_DIR=\$SMCT_ETC_DIR/models; export SMCT_MODELS_DIR
SMCT_HARDWARE_DIR=\$SMCT_ETC_DIR/hardware; export SMCT_HARDWARE_DIR
SMCT_SERVICES_DIR=\$SMCT_ETC_DIR/services; export SMCT_SERVICES_DIR
SMCT_CONFIG_DIR=\$SMCT_ENV_DIR/config; export SMCT_CONFIG_DIR
SMCT_DEFAULT_CONFIG_DIR=\$SMCT_ENV_DIR/config; export SMCT_DEFAULT_CONFIG_DIR
SMCT_SCRIPT_DIR=\$SMCT_ENV_DIR/scripts; export SMCT_SCRIPT_DIR
SMCT_EXPORT_DIR=\$SMCT_ENV_DIR/export; export SMCT_EXPORT_DIR
SMCT_IMPORT_DIR=\$SMCT_ENV_DIR/import; export SMCT_IMPORT_DIR
SMCT_JUMPSTART_DIR=\$SMCT_ENV_DIR/jumpstart; export SMCT_JUMPSTART_DIR
SMCT_FLASH_DIR=\$SMCT_ENV_DIR/flash; export SMCT_FLASH_DIR
SMCT_TMP_DIR=\$SMCT_ENV_DIR/tmp; export SMCT_TMP_DIR
SMCT_VER=$SMCTVER; export SMCT_VER
EOF

   if [ $SLENV -eq 1 ]
   then
      $CAT >> $SMCTDIR/scripts/smct.env <<EOF
NHAS_PAT_DIR=$SMCTPATDIR; export NHAS_PAT_DIR
NHAS_PKG_DIR=$SMCTPKGDIR; export NHAS_PKG_DIR
SMCT_SOFTREP_DIR=\$SMCT_ENV_DIR/softrepo; export SMCT_SOFTREP_DIR
SMCT_SWLREP_DIR=\$SMCT_ENV_DIR/swlrepo; export SMCT_SWLREP_DIR
EOF
   fi

   $CAT >> $SMCTDIR/scripts/smct.env <<EOF

# Function used to clean the SMCT tmp dir
clean() {
   case \$SMCT_TMP_DIR in
      *(/)) print "ERROR: cannot remove < \$SMCT_TMP_DIR > content !!!"
            exit 1
	 ;;
      *) $RM -rf \$SMCT_TMP_DIR/*
         ;;
   esac
}

# SMCT commands aliases (for setup USER environment)
#
alias flcreate="\$SMCT_ENV_DIR/scripts/flcmd create"
alias flinstall="\$SMCT_ENV_DIR/scripts/flcmd install"
alias flconfig="\$SMCT_ENV_DIR/scripts/flcmd config"
alias fldeploy="\$SMCT_ENV_DIR/scripts/flcmd deploy"
EOF

   if [ $SLENV -eq 1 ]
   then
      $CAT >> $SMCTDIR/scripts/smct.env <<EOF
alias slcreate="\$SMCT_ENV_DIR/scripts/slcmd create"
alias slconfig="\$SMCT_ENV_DIR/scripts/slcmd config"
alias slexport="\$SMCT_ENV_DIR/scripts/slcmd export"
alias sldelete="\$SMCT_ENV_DIR/scripts/slcmd delete"
alias sllist="\$SMCT_ENV_DIR/scripts/slcmd list"
alias sldeploy="\$SMCT_ENV_DIR/scripts/slcmd deploy"
EOF
   fi

   # - flcmd wrapper generation -
   $CAT > $SMCTDIR/scripts/flcmd <<EOF
#!/bin/ksh
# This is a generated file - do not modify  -
. \$SMCT_ENV_DIR/scripts/smct.env

opt=\$1

usage() {
   \$PERL \$NHAS_PROD_DIR/nhsmct/bin/flash.pl -h
}

clean

USERID=\`/usr/xpg4/bin/id -u\`
ROOTID=0

for i in \$*
do
   if [ \$i = "\$opt" ]
   then
      if [ \$USERID != "\${ROOTID}" ]
      then
	 echo "\nERROR: You must be root to run < fl\$opt >\n"
	 exit 1
      else
	 \$PERL \$NHAS_PROD_DIR/nhsmct/bin/flash.pl \$*
	 exit
      fi
   fi
done

usage
exit
EOF

   # - slcmd wrapper generation -
   if [ $SLENV -eq 1 ]
   then
      $CAT > $SMCTDIR/scripts/slcmd <<EOF
#!/bin/ksh
# This is a generated file - do not modify  -
. \$SMCT_ENV_DIR/scripts/smct.env

cmd=\$1

usage() {
   \$PERL \$NHAS_PROD_DIR/nhsmct/bin/smct.pl \$cmd -h
}

clean

if [ \$# -eq 0 ]
then
   usage
else
   \$PERL \$NHAS_PROD_DIR/nhsmct/bin/smct.pl \$cmd \$*
fi
exit
EOF
   fi

   # - Finish command installation -
   $CHMOD a+rx $SMCTDIR/scripts/smct.env
   [ $? -eq 0 ] || error "Cannot $CHMOD a+rx $SMCTDIR/scripts/smct.env"
   if [ $SLENV -eq 1 ]
   then
      $CHMOD a+x $SMCTDIR/scripts/sl*
      [ $? -eq 0 ] || error "Cannot $CHMOD a+x $SMCTDIR/scripts/sl*"
   fi
   $CHMOD a+x $SMCTDIR/scripts/fl*
   [ $? -eq 0 ] || error "Cannot $CHMOD a+x $SMCTDIR/scripts/fl*"
   return $STATUS
}

# -----------------------------------------------------------------------------

usage () {
   BINPATH="\$ ksh /opt/SUNWcgha/nhsmct/bin"
   print "\nUsage:\n\n"
   print " ${BINPATH}/nhsmctsetup\n"
   print "     Install complete SMCT environment (interactive mode)\n\n" 
   print " ${BINPATH}/nhsmctsetup -h\n"
   print "     display usage information\n\n"
   print " ${BINPATH}/nhsmctsetup -w <working-dir> -s <solaris-dist>\n"
   print "     Install SMCT environment for flxxx commands only\n\n"
   print " ${BINPATH}/nhsmctsetup -d -w <working-dir> -s <solaris-dist>\n"
   print "     Install a duplicated SMCT environment\n\n"
   print " ${BINPATH}/nhsmctsetup -w <working-dir> -s <solaris-dist> [-p <nhas-patch>] -r <nhas-pkgs>\n"
   print "     Install complete SMCT environment (non-interactive mode)\n" 
   print "Options:\n" 
   echo  " -h Display help information"
   echo  " -d Duplicate SMCT environment flag"
   echo  " -w Specifiy the root directory of the SMCT environment that will be created"
   echo  " -s Specifiy the root directory of a Jumpstart Solaris distribution"
   echo  " -r Specifiy the NHAS run-time packages repository directory"
   echo  " -p Specifiy the NHAS run-time patches repository directory\n"
   exit $1
}

# -----------------------------------------------------------------------------

checkWorkingDir () {
   ROOTDIR=`${DIRNAME} ${SMCTDIR}`
   if [ ! -r $ROOTDIR ] || [ ! -w $ROOTDIR ] || [ ! -x $ROOTDIR ]
   then
      error "Directory < ${SMCTDIR} > cannot be created (insufficient privileges)"
      SMCTDIR=""
   else
      if [ ! -d $SMCTDIR ]
      then
         if [ $DUPLI -eq 1 ]
	 then
	    # Directory must exist: it must point to the duplicated environment
	    error "Directory < ${SMCTDIR} > does not exist"
	 else
            if [ $SLENV -eq 1 ] || [ $FLENV -eq 1 ]
	    then
	       # Don't ask in non-interactive mode
	       YES="yes"
	    else
	       query "Directory < ${SMCTDIR} > does not exist. Do you want to create it ? [ y | n | q ] : "
	       yes_or_no
	    fi
	    if [  $YES = "yes" ]
	    then
	       $MKDIR -p $SMCTDIR
	       SMCTDIR=`getAbsolutePath ${SMCTDIR}`
	    else	 
	       SMCTDIR=""
	    fi
	 fi
      else
         if [ $DUPLI -eq 0 ]
	 then
	    print
	    warning "Directory < ${SMCTDIR} > already exists"
  	    query "Do you want to remove its content ? [ y | n | q ] : "	 
	    yes_or_no
	    if [  $YES = "yes" ]
	    then
	       if [ -w $SMCTDIR ] && [ -x $SMCTDIR ] && [ -r $SMCTDIR ]
	       then
		  if [ $SMCTDIR != "/" ]
		  then
 		     $RM -rf $SMCTDIR
		     $MKDIR -p $SMCTDIR
		     SMCTDIR=`getAbsolutePath ${SMCTDIR}`
		  else
		     error "Cannot remove root (/) directory"
		     SMCTDIR=""
		  fi
	       else
		  error "Directory < ${SMCTDIR} > cannot be removed (insufficient privileges)"
		  SMCTDIR=""
	       fi
	    else	 
	       SMCTDIR=""
	    fi
	 else
	    SMCTDIR=`getAbsolutePath ${SMCTDIR}`
	 fi
      fi
   fi
}

# -----------------------------------------------------------------------------

checkPackagesDir () {
   if [ ! -d $SMCTPKGDIR ]
   then
      error "Directory < ${SMCTPKGDIR} > does not exist"
      SMCTPKGDIR=""
   else
      if [ -r $SMCTPKGDIR ] && [ -x $SMCTPKGDIR ]
      then
	 SMCTPKGDIR=`getAbsolutePath ${SMCTPKGDIR}`
      else
	 error "Directory < ${SMCTPKGDIR} > cannot be read (insufficient privileges)"
         SMCTPKGDIR=""
      fi
   fi
}

# -----------------------------------------------------------------------------

checkPatchesDir () {
   if [ ! -d $SMCTPATDIR ]
   then
      error "Directory < ${SMCTPATDIR} > does not exist"
      SMCTPATDIR=""
   else
      if [ -r $SMCTPATDIR ] && [ -x $SMCTPATDIR ]
      then
	 SMCTPATDIR=`getAbsolutePath ${SMCTPATDIR}`
      else
	 error "Directory < ${SMCTPATDIR} > cannot be read (insufficient privileges)"
         SMCTPATDIR=""
      fi
   fi
}

# -----------------------------------------------------------------------------

checkSolarisDist () {
   if [ ! -d $SMCTSOLDIR ]
   then
      error "Directory < ${SMCTSOLDIR} > does not exist"
      SMCTSOLDIR=""
   else
      if [ -r $SMCTSOLDIR ] && [ -x $SMCTSOLDIR ]
      then
	 if [ -f "$SMCTSOLDIR/.cdtoc" ]
	 then
	    SMCTSOLDIR=`getAbsolutePath ${SMCTSOLDIR}`
	 else
	    error "Directory < ${SMCTSOLDIR} > does not point to a valid Solaris distribution"
	    SMCTSOLDIR=""
	 fi
      else
	 error "Directory < ${SMCTSOLDIR} > cannot be read (insufficient privileges)"
	 SMCTSOLDIR=""
      fi
   fi
}

# -----------------------------------------------------------------------------
# Main starts here

if [ $# -eq 1 ]
then
   if [ $1 = "-h" ]
   then
      usage 0
   fi
fi

# - Parse command options -
while getopts w:s:r:p:h:d opt
do
   case $opt in
      w) SMCTDIR=$OPTARG;;
      s) SMCTSOLDIR=$OPTARG;;
      r) SMCTPKGDIR=$OPTARG;;
      p) SMCTPATDIR=$OPTARG;;
      d) DUPLI=1;;
      h) usage 0;;
      ?) usage 1;;
   esac
done

if [ $DUPLI -eq 1 ]
then
   # adapt an existing smct environment that has been duplicated
   checkWorkingDir   
   checkSolarisDist  
elif [ -z "${SMCTPKGDIR}" ] && [ -z "${SMCTPATDIR}" ] && 
     [ ! -z "${SMCTDIR}" ] && [ ! -z "${SMCTSOLDIR}" ]
then
   # setup flxxx environment (non-interactive mode)
   FLENV=1
   checkWorkingDir
   checkSolarisDist
elif [ -z "${SMCTPKGDIR}" ] && [ -z "${SMCTPATDIR}" ] && 
     [ -z "${SMCTDIR}" ] && [ -z "${SMCTSOLDIR}" ]
then
   # setup slxxx and flxxx environment (interactive mode)
   SLENV=1
elif [ ! -z "${SMCTPKGDIR}" ] && [ ! -z "${SMCTPATDIR}" ] && 
     [ ! -z "${SMCTDIR}" ] && [ ! -z "${SMCTSOLDIR}" ]
then
   # setup slxxx and flxxx environment (non-interactive mode)
   # patch defined
   SLENV=1
   checkWorkingDir   
   checkPackagesDir
   checkPatchesDir
   checkSolarisDist
elif [ ! -z "${SMCTPKGDIR}" ] && [ -z "${SMCTPATDIR}" ] && 
     [ ! -z "${SMCTDIR}" ] && [ ! -z "${SMCTSOLDIR}" ]
then
   # setup slxxx and flxxx environment (non-interactive mode)
   # No patch defined
   SLENV=1
   PATCH=1
   checkWorkingDir   
   checkPackagesDir
   checkSolarisDist
else
   usage 1
fi

# - Get SMCT root environment Directory -
while [ -z "$SMCTDIR" ] && [ $DUPLI -eq 0 ]
do
   query "Enter SMCT installation directory [ <dir-name> | q | ? ] : " 
   read SMCTDIR
   if [ ! -z "$SMCTDIR" ]
   then
      if [ $SMCTDIR = "q" ]; then exit $STATUS; fi
      if [ $SMCTDIR = "?" ] 
      then
	 help "This directory will be used to install the SMCT environment"
	 SMCTDIR=""
      else
         checkWorkingDir
      fi
   fi
done

# - Get NHAS Packages Repository Directory -
if [ $SLENV -eq 1 ]
then
   while [ -z "$SMCTPKGDIR" ] 
   do
      query "Enter NHAS packages repository directory [ <dir-name> | q | ? ] : " 
      read SMCTPKGDIR
      if [ ! -z "$SMCTPKGDIR" ]
      then
	 if [ $SMCTPKGDIR = "q" ]; then exit $STATUS; fi
	 if [ $SMCTPKGDIR = "?" ] 
	 then
	    help "This directory must contain the NHAS run-time packages\nrequired to build a software load (see NHAS documentation)"
	    SMCTPKGDIR=""
	 else
            checkPackagesDir
	 fi
      fi
   done
fi

# - Get NHAS Patches Repository Directory -
if [ $SLENV -eq 1 ] && [ $PATCH -eq 0 ]
then
   while [ -z "$SMCTPATDIR" ]
   do
      query "Enter NHAS patches repository directory [ <dir-name> | q | ? ] : " 
      read SMCTPATDIR
      if [ ! -z "$SMCTPATDIR" ]
      then
	 if [ $SMCTPATDIR = "q" ]; then exit $STATUS; fi
	 if [ $SMCTPATDIR = "?" ] 
	 then
	    help "This directory must contain the NHAS run-time patches\nrequired to build a software load (see NHAS documentation)"
	    SMCTPATDIR=""
	 else
            checkPatchesDir
	 fi
      fi
   done
fi

# - Get Solaris distribution Directory -
while [ -z "$SMCTSOLDIR" ]
do
   query "Enter Jumpstart Solaris distribution directory [ <dir-name> | q | ? ] : " 
   read SMCTSOLDIR
   if [ ! -z "$SMCTSOLDIR" ]
   then
      if [ $SMCTSOLDIR = "q" ]; then exit $STATUS; fi
      if [ $SMCTSOLDIR = "?" ] 
      then         
	 help "This directory must contain the Solaris distribution\nrequired to build a software load (see NHAS documentation)"
	 SMCTSOLDIR=""
      else
         checkSolarisDist
      fi
   fi
done

# - Set NHAS Product Installation Directory -
INSDIR=$0
SMCTINSDIR=`${DIRNAME} ${INSDIR}`
SMCTINSDIR=`getAbsolutePath ${SMCTINSDIR}`
SMCTPRODIR=`echo ${SMCTINSDIR} | $SED -e 's/\/nhsmct\/bin//'`

# Create SMCT environment -
if [ $DUPLI -eq 0 ]
then
   createEnv
fi

if [ $STATUS -eq 0 ]
then
   # - Check SMCT environment dependencies -
   checkEnv
fi

if [ $STATUS -eq 0 ]
then
   if [ $DUPLI -eq 0 ]
   then
      # - Generate SMCT commands wrappers -
      genScripts
   else
      # - Adapt duplicated SMCT environment
      adaptScripts
   fi
fi

# - Epilog -

if [ $STATUS -eq 0 ]
then
   print
   info "SMCT setup has succeeded"
   info "You can add SMCT commands aliases into your ksh environment by"
   info "sourcing the file < ${SMCTDIR}/scripts/smct.env >"
   print
else
   error "SMCT setup has failed"
   print
fi
exit $STATUS
