#!/bin/sh
#set -x
# use an existing create initial runtime configuration
#
MSG_SVR_BASE=<msg.RootPath>
INSTALLER_TYPE=<INSTALLER_TYPE>
LOGFILE=$MSG_SVR_BASE/install/useconfiglog_`date +%Y%m%d%H%M%S`
. $MSG_SVR_BASE/lib/util.sh

if [ $INSTALLER_TYPE = "PKGADD" ]; then
    PKGLIST=pkgcfg:config
elif [ $INSTALLER_TYPE = "ZIP" ]; then
    PKGLIST=zipcfg:config
elif [ $INSTALLER_TYPE = "RPM" ]; then
    PKGLIST=pkgcfg:config
elif [ $INSTALLER_TYPE = "DEVINSTALL" ]; then
    PKGLIST=devcfg:config
fi

#
# usage
#
usage()
{
    echo
    echo  "Usage: $0 <configdir>\n"
    echo
    echo  "  Devsetup.properties: path to existing configuration directory\n"
    echo  "    e.g. /var${MSG_SVR_BASE}/setup/configure_YYYYMMDDHHMMSS\n"
    echo
    exit 1
}

while getopts h c
do
    case $c in
    h)
        usage;;
    \?)
        usage;;
    esac
done
shift `expr $OPTIND - 1 `

if [ $# -ne 1 ]; then
  usage
fi
CONFIG_DIR=$*

if [ ! -s ${CONFIG_DIR}/Devsetup.properties ]; then
  echo "Could not find file ${CONFIG_DIR}/Devsetup.properties\n"
  echo "Invalid configuration directory specified. \n"
  exit 2
fi

#
# must be root
#
set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
  echo
  echo "Initial Runtime configurator for Sun ONE Messaging Server \n"
  echo "To use this installer you will need to be the system's root user. \n"
  echo 
  exit 1
fi

log_init

# copy Devsetup.properties
log_msg "cp ${CONFIG_DIR}/Devsetup.properties ${MSG_SVR_BASE}/lib/config-templates/Devsetup.properties"
cp ${CONFIG_DIR}/Devsetup.properties ${MSG_SVR_BASE}/lib/config-templates/Devsetup.properties | tee -a $LOGFILE

# extract iMS.UserId and iMS.GroupId

user=`grep iMS.UserId ${CONFIG_DIR}/saveState | awk '{print $3}'`
group=`grep iMS.GroupId ${CONFIG_DIR}/saveState | awk '{print $3}'`

# add the group and user
log_msg "/usr/sbin/groupadd $group"
/usr/sbin/groupadd $group >>$LOGFILE 2>&1
stat=$?
if [ $stat -ne 0 -a $stat -ne 9 ]; then
  log_msg "groupadd failed status $stat"
fi

log_msg "/usr/sbin/useradd -g $group -d / $user"
/usr/sbin/useradd -g $group -d / $user >>$LOGFILE 2>&1
stat=$?
if [ $stat -ne 0 -a $stat -ne 9 ]; then
  log_msg "useradd failed status $stat"
fi

log_msg "/usr/sbin/usermod -G $group $user"
/usr/sbin/usermod -G $group $user >>$LOGFILE 2>&1
stat=$?
if [ $stat -ne 0 -a $stat -ne 3 ]; then
  log_msg "usermod failed status $stat"
fi

# sed config.ins and devtypes.txt

log_msg "sed -e \"s/<local.serveruid>/${user}/\" -e \"s/<local.servergid>/${group}/\" -e \"s:<msg\.RootPath>:${MSG_SVR_BASE}:\" ${MSG_SVR_BASE}/lib/config-templates/devtypes.txt.template > ${MSG_SVR_BASE}/lib/config-templates/devtypes.txt"
sed -e "s/<local.serveruid>/${user}/" \
    -e "s/<local.servergid>/${group}/" \
    -e "s:<msg\.RootPath>:${MSG_SVR_BASE}:" \
  ${MSG_SVR_BASE}/lib/config-templates/devtypes.txt.template \
  > ${MSG_SVR_BASE}/lib/config-templates/devtypes.txt | tee -a $LOGFILE

log_msg "sed -e \"s/<local.serveruid>/${user}/\" -e \"s/<local.servergid>/${group}/\" -e \"s:<msg\.RootPath>:${MSG_SVR_BASE}:\" ${MSG_SVR_BASE}/lib/config-templates/config.ins.template > ${MSG_SVR_BASE}/lib/config-templates/config.ins"
sed -e "s/<local.serveruid>/${user}/" \
    -e "s/<local.servergid>/${group}/" \
    -e "s:<msg\.RootPath>:${MSG_SVR_BASE}:" \
  ${MSG_SVR_BASE}/lib/config-templates/config.ins.template \
  > ${MSG_SVR_BASE}/lib/config-templates/config.ins | tee -a $LOGFILE

# run devinstall utility

log_msg "${MSG_SVR_BASE}/lib/devinstall -l ${PKGLIST} -v -m -i ${MSG_SVR_BASE}/lib/config-templates/config.ins ${MSG_SVR_BASE}/lib/config-templates ${MSG_SVR_BASE}/lib/jars ${MSG_SVR_BASE}/lib"
${MSG_SVR_BASE}/lib/devinstall -l ${PKGLIST} -v -m -i ${MSG_SVR_BASE}/lib/config-templates/config.ins ${MSG_SVR_BASE}/lib/config-templates ${MSG_SVR_BASE}/lib/jars ${MSG_SVR_BASE}/lib >>$LOGFILE 2>&1
stat=$?
log_msg "devinstall returned $stat"

# run crle on solaris systems

if [ `uname` = "SunOS" ]; then
  if [ -f /var/ld/ld.config ]; then
    cmd="`crle | grep crle` -s ${MSG_SVR_BASE}/lib"
  else
    cmd="crle -s /usr/lib/secure -s ${MSG_SVR_BASE}/lib"
  fi
  log_msg "$cmd"
  $cmd | tee -a $LOGFILE
fi

log_msg "See $LOGFILE for more details"
