#!/bin/sh
# $Id: postinstall.sh,v 1.11 2005/10/05 13:57:03 billwe Exp $
#  Copyright 2005 Sun Microsystems, Inc. All rights reserved.
#  This software code contains proprietary information 
#  and should be treated as confidential.
#
#  Component: RPM post installation script
#  Author: Clare Priestley

N=""
C=""
if echo -n | grep n > /dev/null
then
  C="\c"
else
  N="-n"
fi
case "$MACHINEARCH" in
i3li*|ppli*) N="$N -e" ;;
esac
_ss_headlines="--------------------------------------------------------------------------"


OK=0
FAIL=1
CANT_DOWNGRADE=2
CANT_UPGRADE=3

setPkgLang()
{
     posslangs=`(cd "${REQDIR}" ; ls ??-??_setup 2>/dev/null)`
     case $posslangs in
       "" )                                          # no languages!
         PKGLANG=""
         echo "Can't find any language files in directory $REQDIR" >>$LOGFILE
         exit 1
       ;;
       ??-??_setup )                                         # Good; just one
         PKGLANG=`expr "$posslangs" : "\(..-..\)"`
       ;;
       * )                                           # Choices, choices
         case $LANG in
           ""|C|POSIX )
           ;;                                        # Ignored
           * )                                       # Some OS specific string
             LA=`expr $LANG : "\(..\)"`              # 1st two chars
             # must ensure tr uses C locale to lower case!
             la=`echo $LA | LANG=C LC_ALL=C tr '[A-Z]' '[a-z]'`
             goodlangs=`echo "$posslangs" | grep "^$la"`
             if [ -n "$goodlangs" ]
             then
               posslangs=$goodlangs                  # ones that match $LANG
             fi
             ;;
         esac
         # Pick the first language in posslangs, or en-us if that is empty
         if [ -z "$posslangs" ]; then
            PKGLANG="en-us"
         else
            PKGLANG=`expr "$posslangs" : "\(..-..\)"`
         fi
     ;;
     esac

     if [ -n "$PKGLANG" ]; then
       # read the strings in
       . ${REQDIR}/${PKGLANG}_setup
     fi
}

getLicenseInfo()
{
    #
    #-------------------------------------------------------------------------
    # Check user has read and agreed to the license file.
    #-------------------------------------------------------------------------
    #

    skip_license=

    awkscript='{v="true"; if (NF!=1 || $1!~/^['${a_case_yes}${a_case_no}${a_case_readlicense}'].*$/) v="false"; print v}'

    a_chooselicop=$a_readlicense
    echo " "
echo $_ss_headlines
echo "$h_licenseagreement"
echo $_ss_headlines

echo "$p_chooselicop"
echo # blank line between prompt and question
_valid=false
while [ $_valid = false ]
do
  echo $N "$q_chooselicop [$a_chooselicop] $C"
  read _answer
  if [ -z "$_answer" ]
  then
    _answer="$a_chooselicop"   # default
  fi
   _valid=`echo "$_answer" | sed 's/[, ][, ]*/ /g' | awk "$awkscript"` 
done
a_chooselicop="$_answer"


    case $a_chooselicop in
	[${a_case_no}]* ) return 3 ;;
	[${a_case_yes}]* ) skip_license=true ;;
    esac

    if [ -z "$skip_license" ]
    then
	licfile=${REQDIR}/${PKGLANG}_license
	if [ -f "$licfile" ]; then
	    # Select a pager to view the license agreement.  Use pg if available
	    # (usually everywhere except Linux) - otherwise use less or more.
	    # /usr/bin/pg is on some Linuxen, but its not a pager! fitz/514246
	    #
	    # Do all this in a subshell so that we don't have to bother with
	    # resetting environment variables at the end.
	    #
	(
	    PAGER=""
	    case $MACHINEARCH in
	    i3li*|ppli*) pagers="less:more" ;;
	    * ) pagers="pg:less:more" ;;
	    esac
	    savedifs="$IFS"
	    IFS=:
	    for cmd in $pagers; do
		[ -n "$PAGER" ] && break
		for d in $PATH; do
		    [ -x $d/$cmd ] && { PAGER=$d/$cmd; break; }
		done
	    done
	    IFS="$savedifs"

	    # We should make sure we're in raw mode if we're using less
	    oldstty="" oldless="$LESS"
	    case "$PAGER" in 
	    */less)
		oldstty=`stty -g`
                stty -icanon
		LESS="-f -Es -P \"$less_prompt\"" export LESS
		;;
	    */pg)
		PAGER="$PAGER -p \"$pg_prompt\""
		;;
	    esac

pg=${PAGER:-more}
##ifdef ja-jp
# == eval LANG=ja_JP LC_ALL=ja_JP $pg $licfile
##endif
##ifdef ko-kr
# == eval LANG=ko_KR LC_ALL=ko_KR $pg $licfile
##endif
##ifdef zh-cn
# == eval LANG=zh_CN LC_ALL=zh_CN $pg $licfile
##endif
##ifdef en-us
eval $pg $licfile
##endif


	    # reset stty if we changed it
	    [ -n "$oldstty" ] && stty $oldstty
	)
	else
	    echo "Error - cannot find license file."
	    return 1
	fi

	a_license=$a_no 
	echo " "; echo " "
echo $_ss_headlines
echo "$h_licenseagreement"
echo $_ss_headlines

echo ""
echo # blank line between prompt and question
_valid=false
while [ $_valid = false ]
do
  echo $N "$q_license [$a_license] $C"
  read _answer
  if [ -z "$_answer" ]
  then
    _answer="$a_license"   # default
  fi
  case "$_answer" in
    [${a_case_yes}]* )
	    _answer=$s_yes
	    _valid=true
	    ;;
    [${a_case_no}]* )
	    _answer=$s_no
	    _valid=true
	    ;;
  esac
done
a_license="$_answer"


	case $a_license in
	    [${a_case_no}]* ) return 3 ;;
	esac
    fi

    return $OK
}

# This should only be executed if the server has not been configured
configure=${INSTDIR}/var/configure
if [ ! -f $configure ]; then
  exit
fi

# Set the package and architecture env variables
PKG_NAME=`awk '{print $1}' $configure`
SHORT_ARCH=`expr ${SCOARCH} : '\(....\).*'`

# Set up the environment
TTA_RPM="t" export TTA_RPM
TTA_TMPDIR=/tmp/ttarpm export TTA_TMPDIR
DATADIR="${INSTDIR}/var" export DATADIR
INST_DATADIR="${INSTDIR}/etc/pkg" export INST_DATADIR
PKG=${PKG_NAME} export PKG
PKGINST=${PKG_NAME} export PKGINST
REQDIR=${INST_DATADIR}/${PKG_NAME}/install export REQDIR
xflag=""; [ -f /tmp/pkgadd.debug ] && { trace=x export trace; xflag="-x"; }
mkdir ${TTA_TMPDIR} 2>/dev/null

# Check the license agreement
setPkgLang
getLicenseInfo
rVal=$?
[ $rVal -ne $OK ] && exit $rVal

# Get the config from the user
cd ${INSTDIR}/bin/scripts/install_scripts
TTA_TMPDIR="${INSTDIR}/bin/bin" export TTA_TMPDIR
. ./setup_dialog
peer="${TTA_TMPDIR}/ttahostname"
mkdir -p ${INSTDIR}/var/tmp 2>/dev/null
chmod 770 ${INSTDIR}/var/tmp 2>/dev/null
setup_dialog --response ${INSTDIR}/var/tmp/setup_response --ask_httpport --ask_peerdns ${peer} --ask_crontab
. ${INSTDIR}/var/tmp/setup_response
# Make it available to the post-installation scripts
export a_PeerDNS
export a_httpport
export a_CronDay
export a_CronTime
export a_Archive
[ -n "${jive_Build}" ] && UPGRADE="${jive_Build}" export UPGRADE

# reset the tmpdir
TTA_TMPDIR=/tmp/ttarpm export TTA_TMPDIR
# call the post install scripts
for dir in \
    install install.arch \
    install.pip install.${SHORT_ARCH}.pip \
    install.wcp install.${SHORT_ARCH}.wcp; do
    postinstall=${INST_DATADIR}/${PKG_NAME}/${dir}/postinstall
    [ -f ${postinstall} ] && sh ${xflag} ${postinstall}
done

# Delete the file so the server is now configured
[ -f ${configure} ] && rm -f ${configure}

[ -n "${UPGRADE}" ] && ${INSTDIR}/bin/scripts/postupgrade ${INSTDIR}

# If there are add-on packs to configure, run their postinstall scripts
TTA_FROM_START="t" export TTA_FROM_START
configure_addons=`ls ${INSTDIR}/var/configure.* 2> /dev/null`
for file in ${configure_addons}; do
  installdirs=`cat ${file}`
  add_on=`expr "${file}" : "${INSTDIR}/var/configure.\(.*\)"`
  PKG="${add_on}" export PKG
  PKG_NAME="$PKG" export PKG_NAME
  PKGINST="${add_on}" export PKGINST
  REQDIR=${INST_DATADIR}/${PKG_NAME}/install export REQDIR
  for dir in ${installdirs}; do
    postinstall=${INST_DATADIR}/${add_on}/${dir}/postinstall
    [ -f ${postinstall} ] && sh ${xflag} ${postinstall}
  done
  # Delete the configure file
  [ -f ${file} ] && rm -f ${file}
done

exit 0



