#!/bin/bash

LINK="no"
UPDATE="no"
USAGE="Usage: $0 [-l] [-h] <pkg-source-dir> <office-installation-dir>"

help()
{
  echo 
  echo "User Mode Installation script for developer and knowledgeable early access tester"
  echo 
  echo "This installation method is not intended for use in a production environment!"
  echo "Using this script is unsupported and completely at your own risk"
  echo 
  echo "Usage:" $0 "<pkg-source-dir> <office-installation-dir> [-l]"
  echo "    <pkg-source-dir>:       directory *only* containing the Solaris pkg packages to be installed"
  echo "                            or language pack shell script containing the Solaris pkg packages"
  echo "    <office-installation-dir>: directory to where the office and the pkg database will get installed into"
  echo
  echo "Optional Parameter:"
  echo "    -l :              create a link \"soffice\" in $HOME"
  echo "    -h :              output this help"
}

try_to_unpack_languagepack_file()
{
  FILENAME=$PACKAGE_PATH

  # Checking, if $FILENAME is a language pack.
  # String "language package" has to exist in the shell script file.
  # If this is no language pack, the installation is not supported

  SEARCHSTRING=`head -n 10 $FILENAME | grep "language package"`

  if [ ! -z "$SEARCHSTRING" ]
  then
    echo "First parameter $FILENAME is a language pack";
  else
    printf "\nERROR: First parameter $FILENAME is a file, but no language pack shell script.\n"
    echo $USAGE
    exit 2  
  fi
  
  echo "Unpacking shell script $FILENAME"
  # TAILLINE=`head -n 20 $FILENAME | sed --quiet 's/linenum=//p'`
  TAILLINE=`head -n 20 $FILENAME | sed -n 's/linenum=//p'`
  
  if [ -x "/usr/bin/mktemp" ]  # available in Solaris 10
  then
    UNPACKDIR=`mktemp -d`
  else
    UNPACKDIR=/var/tmp/install_$$
    mkdir $UNPACKDIR
  fi
  
  echo $UNPACKDIR
  tail +$TAILLINE $FILENAME | gunzip | (cd $UNPACKDIR; tar xvf -)

  # Setting the new package path, in which the packages exist
  PACKAGE_PATH=$UNPACKDIR
  
  # Setting variable UPDATE, because an Office installation has to exist, if a language pack shall be installed
  UPDATE="yes"
}

pkg_error()
{
  # pkg command failed, check for admin log and report help
  if [ -f /tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e ]
  then
    echo "####################################################################"
    echo "#     Installation failed due to stale administrative lock         #"
    echo "####################################################################"
    printf "\nERROR: please remove the following file first:\n"
	ls -l /tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e
  fi
  rm -f $GETUID_SO
  exit 1
}

#
# this script is for userland not for root
#

if [ $UID -eq 0 ]
then
  printf "\nThis script is for installation without administrative rights only\nPlease use pkgadd/patchadd to install as root\n"
  help
  exit 2
fi

while getopts "lh" VALUE
do
  echo $VALUE
  case $VALUE in
    h)      help; exit 0;;
    l)      LINK="yes"; break;;
    ?)      echo $USAGE; exit 2;;
  esac
done
shift `expr $OPTIND - 1`

if [ $# != 2 ]
then
  echo $USAGE
  exit 2
fi

# Determine whether this is a patch or a regular install set ..
/bin/bash -c "ls $1/*/patchinfo >/dev/null 2>&1" 
if [ "$?" = 0 ]
then
  UPDATE="yes"
  PATCH_PATH="$1"
  PATCH_INFO_LIST=`/bin/bash -c "cd $1; ls */patchinfo"`
  PATCH_LIST=`for i in ${PATCH_INFO_LIST}; do dirname $i; done`
elif [ -f "$1/patchinfo" ]
then
  UPDATE="yes"
  PATCH_PATH=`dirname "$1"`
  PATCH_LIST=`basename "$1"`
else
  if [ -d "$1/packages" ]
  then
    PACKAGE_PATH="$1/packages"
  else
    PACKAGE_PATH=$1
  fi

  #
  # If the first parameter is a shell script (download installation set), the packages have to 
  # be unpacked into temp directory
  #
  if [ -f "$PACKAGE_PATH" ]
  then
    try_to_unpack_languagepack_file
  fi

  #
  # Check and get the list of packages to install
  #
  PKG_LIST=`cd $PACKAGE_PATH ; find * -type d -prune ! -name "*adabas*" \
    ! -name "*j5*" ! -name "*-desktop-int*" ! -name "*-shared-mime-info" \
    ! -name "*-cde*" ! -name "*-gnome*" -print`
  
  if [ -z "$PKG_LIST" ]
  then
    printf "\n$0: No packages found in $PACKAGE_PATH\n"
    exit 2
  fi

  # Do not install gnome-integration package on systems without GNOME
  pkginfo -q SUNWgnome-vfs
  if [ $? -eq 0 ]
  then
    GNOMEPKG=`cd $PACKAGE_PATH ; find * -type d -prune -name "*-gnome*" -print`
  fi
  
  echo "Packages found:"
  for i in $PKG_LIST $GNOMEPKG; do
    echo $i
  done
fi

INSTALL_ROOT=$2
if [ "$UPDATE" = "yes" ]
then
  if [ ! -d ${INSTALL_ROOT}/var/sadm/install/admin ]
  then
    printf "\n$0: No package database in ${INSTALL_ROOT}.\n"
    exit 2
  fi
else
  rmdir ${INSTALL_ROOT} 2>/dev/null
  if [ -d ${INSTALL_ROOT} ]
  then
    printf "\n$0: ${INSTALL_ROOT} exists and is not empty.\n"
    exit 2
  fi
  mkdir -p ${INSTALL_ROOT}/var/sadm/install/admin
fi

# Previous versions of this script did not write this file
if [ ! -f ${INSTALL_ROOT}/var/sadm/install/admin/default ]
then
  cat > ${INSTALL_ROOT}/var/sadm/install/admin/default << EOF
action=nocheck
conflict=nocheck
setuid=nocheck
idepend=nocheck
mail=
EOF
fi

# create local tmp directory to install on S10
LOCAL_TMP=
if [ -x /usr/bin/mktemp ]
then
  LOCAL_TMP=`mktemp -d`
  rmdir ${INSTALL_ROOT}/tmp 2>/dev/null
  ln -s ${LOCAL_TMP} ${INSTALL_ROOT}/tmp
fi

#
# the tail of the script contains a shared object for overloading the getuid()
# and a few other calls
#

GETUID_SO=/tmp/getuid.so.$$
linenum=336
tail +$linenum $0 > $GETUID_SO

#
# Perform the installation 
#
if [ "$UPDATE" = "yes" ]
then
  if [ ! "${INSTALL_ROOT:0:1}" = "/" ]; then
    INSTALL_ROOT=`cd ${INSTALL_ROOT}; pwd`
  fi

  OFFICE_DIR=${INSTALL_ROOT}`pkgparam -f ${INSTALL_ROOT}/var/sadm/pkg/*core01/pkginfo BASEDIR 2>/dev/null`

  # restore original "bootstraprc" and "soffice" prior to patching
  if [ "${OFFICE_DIR}" != "" ]
  then
    mv -f ${OFFICE_DIR}/program/bootstraprc.orig ${OFFICE_DIR}/program/bootstraprc
    mv -f ${OFFICE_DIR}/program/soffice.orig ${OFFICE_DIR}/program/soffice
  fi
  
  # copy INST_RELEASE file
  if [ ! -f ${INSTALL_ROOT}/var/sadm/system/admin/INST_RELEASE ]
  then
    mkdir -p ${INSTALL_ROOT}/var/sadm/system/admin 2>/dev/null
    cp -f /var/sadm/system/admin/INST_RELEASE ${INSTALL_ROOT}/var/sadm/system/admin/INST_RELEASE
  fi
  
  # The case UPDATE="yes" is valid for patch installation and for language packs.
  # For patches the variable PKG_LIST is empty, for language packs it is not empty.
  # Patches have to be installed with patchadd, language packs with pkgadd
  
  if [ -z "${PKG_LIST}" ]
  then
    LD_PRELOAD_32=$GETUID_SO /usr/sbin/patchadd -R ${INSTALL_ROOT} -M ${PATCH_PATH} ${PATCH_LIST} 2>&1 | grep -v '/var/sadm/patch' || pkg_error
  else
    LD_PRELOAD_32=$GETUID_SO /usr/sbin/pkgadd -d ${PACKAGE_PATH} -R ${INSTALL_ROOT} ${PKG_LIST} >/dev/null || pkg_error
  fi

else

  # Create BASEDIR directories to avoid manual user interaction
  for i in ${PKG_LIST}; do
    mkdir -m 0755 -p ${INSTALL_ROOT}`pkgparam -d ${PACKAGE_PATH} $i BASEDIR` 2>/dev/null
  done
  
  #
  # Determine office installation directory
  #

  for i in ${PKG_LIST}; do
    echo $i | grep core01 > /dev/null
    if [ $? = 0 ]; then
      OFFICE_DIR=${INSTALL_ROOT}`pkgparam -d ${PACKAGE_PATH} $i BASEDIR`
    fi
  done
  
  if [ ! "${INSTALL_ROOT:0:1}" = "/" ]; then
    INSTALL_ROOT=`cd ${INSTALL_ROOT}; pwd`
  fi
  
  echo "####################################################################"
  echo "#     Installation of the found packages                           #"
  echo "####################################################################"
  echo
  echo "Path to the packages       : " $PACKAGE_PATH
  echo "Path to the installation   : " $INSTALL_ROOT

  LD_PRELOAD_32=$GETUID_SO /usr/sbin/pkgadd -d ${PACKAGE_PATH} -R ${INSTALL_ROOT} ${PKG_LIST} ${GNOMEPKG} >/dev/null || pkg_error
fi

rm -f $GETUID_SO
rm -f /tmp/.ai.pkg.zone.lock*

# remove local tmp directory
if [ ! -z ${LOCAL_TMP} ]
then
  rm -f ${LOCAL_TMP}/.ai.pkg.zone.lock*
  rmdir ${LOCAL_TMP}
  rm -f ${INSTALL_ROOT}/tmp
  mkdir ${INSTALL_ROOT}/tmp
fi

#
# Create a link into the users home directory
#

if [ "$LINK" = "yes" ]
then
  echo
  echo "Creating link from $OFFICE_DIR/program/soffice to $HOME/soffice"
  rm -f $HOME/soffice 2>/dev/null
  ln -s $OFFICE_DIR/program/soffice $HOME/soffice
fi

# patch the "bootstraprc" to create a self-containing installation
if [ "${OFFICE_DIR}" != "" ]; then
  mv ${OFFICE_DIR}/program/bootstraprc ${OFFICE_DIR}/program/bootstraprc.orig
  sed 's/UserInstallation=$SYSUSERCONFIG.*/UserInstallation=$ORIGIN\/..\/UserInstallation/g' \
    ${OFFICE_DIR}/program/bootstraprc.orig > ${OFFICE_DIR}/program/bootstraprc
fi

# patch the LD_LIBRARY_PATH in the "soffice" script so that it finds a suitable libfreetype
if [ "${OFFICE_DIR}" != "" ]; then
  mv ${OFFICE_DIR}/program/soffice ${OFFICE_DIR}/program/soffice.orig
  sed 's| LD_LIBRARY_PATH=\"\$sd_prog\"| LD_LIBRARY_PATH=/usr/sfw/lib:"$sd_prog":"$sd_prog/../../../usr/sfw/lib"|' \
    ${OFFICE_DIR}/program/soffice.orig > ${OFFICE_DIR}/program/soffice
  chmod a+x ${OFFICE_DIR}/program/soffice
fi

# if an unpack directory exists, it can be removed now
if [ ! -z "$UNPACKDIR" ]
then
  # for i in ${PKG_LIST}; do
  #   cd $UNPACKDIR; rm -rf $i
  # done
  # rmdir $UNPACKDIR

  rm -rf $UNPACKDIR
  echo "Removed temporary directory $UNPACKDIR"
fi

echo
echo "Installation done ..."
exit 0

ELF                    4        4    (                   
  
           
 
                                                                                                                             	   
   
                                                                                                                                                                               
                   $          +  
      
   2       	   8          9              >          H  <        N 
         d        "              "        X          p                "              "              "        `                    	     
     P          x                "             "     *             0  h      _PROCEDURE_LINKAGE_TABLE_ _DYNAMIC _edata _etext _init fstat fstatat64 chmod _GLOBAL_OFFSET_TABLE_ __1cG__CrunVdo_exit_code_in_range6Fpv1_v_ __1cH__CimplKcplus_fini6F_v_ lchown setuid atexit _ex_register _get_exit_frame_monitor fchown _end _fini setgid __1cH__CimplKcplus_init6F_v_ _ex_deregister dlsym getuid libdl.so.1 SISCD_2.3 libCrun.so.1 SUNW_1.1 libc.so.1 SYSVABI_1.3 SUNWprivate_1.1 libpthread.so.1 libm.so.1 $ORIGIN       7       zt      B        L       
='      Y        b       7̳      l   Nq      x     4     	 8      l        p     L       H       D       h       @       <       0       
                          㿘@   5  A-   'D<'H#\ #`      @\@@ A	?\   ᴂ@ ` \   D
   HH#`  H $H$  `  @   5  @-   'D'H#\       @\?@@ @ې?\   Ḃ@ ` H`@ 
D@ @Ӓt     H`@'H\%   D   H 㿘@   -  @'D+   'Ha'L 'P #\#`    @\@@ @?\   @'` \   DaHL   PL#`  L $` L#$`  㿠@     㿠@      l?       㿠@      @?        x`??????                @     @  @     @         @     @        㿠@      ?           @      L???쀤                @     @          chown lchown fchown getuid setuid setgid fstat chmod fstatat64  fstat   chmod   fstatat64                                                      00     <0      o        7     o        Lo        o        b        
  	                        
             o  Go  Do   o                    8              	   o   o   lo   o         o      
                                                                                              	getuid.c  cg: Sun Compiler Common 11 2005/10/13 cg: Sun Compiler Common 11 2005/10/13 @(#)fcntl.h	1.15	01/07/29 SMI @(#)feature_tests.h	1.18	99/07/26 SMI @(#)isa_defs.h	1.20	99/05/04 SMI @(#)types.h	1.75	04/01/06 SMI @(#)machtypes.h	1.13	99/05/04 SMI @(#)int_types.h	1.6	97/08/20 SMI @(#)select.h	1.17	01/08/15 SMI @(#)time.h	2.72	05/01/24 SMI @(#)time.h	1.39	99/08/10 SMI @(#)time_iso.h	1.1	99/08/09 SMI @(#)time_impl.h	1.7	01/09/06 SMI @(#)fcntl.h	1.48	01/12/17 SMI @(#)stat.h	1.4	03/06/01 SMI @(#)stat_impl.h	1.3	03/06/01 SMI @(#)unistd.h	1.60	01/07/29 SMI @(#)unistd.h	1.39	01/07/29 SMI @(#)dlfcn.h	1.40	05/08/08 SMI @(#)auxv.h	1.27	00/02/07 SMI @(#)systeminfo.h	1.18	99/07/18 SMI @(#)strings.h	1.3	96/03/12 SMI @(#)string.h	1.24	99/08/10 SMI @(#)string_iso.h	1.2	99/11/09 SMI acomp: Sun C 5.8 2005/10/13 iropt: Sun Compiler Common 11 2005/10/13  .SUNW_syminfo .hash .dynsym .dynstr .SUNW_version .SUNW_reloc .rela.plt .text .init .fini .exception_ranges .rodata .rodata1 .got .dynamic .ex_shared .cpp_finidata .data .bss .symtab .strtab .compcom .debug_info .debug_line .debug_abbrev .debug_pubnames .debug_typenames .debug_varnames .comment .shstrtab                                               o   B         l                                                                               "                        %o     D  D   p                3                             ?      B  8  8                  I        P  P                    O                               U        	  	                     [        
`  
`                    m        
d  
d   ?                  u        
  
                     ~       
  
                    D       
  
   L                                                    0  0   8                         h  h                            t  t                                                                    !                                 I                 )               3               