1#!/usr/bin/env bash
2# *************************************************************
3#
4#  Licensed to the Apache Software Foundation (ASF) under one
5#  or more contributor license agreements.  See the NOTICE file
6#  distributed with this work for additional information
7#  regarding copyright ownership.  The ASF licenses this file
8#  to you under the Apache License, Version 2.0 (the
9#  "License"); you may not use this file except in compliance
10#  with the License.  You may obtain a copy of the License at
11#
12#    http://www.apache.org/licenses/LICENSE-2.0
13#
14#  Unless required by applicable law or agreed to in writing,
15#  software distributed under the License is distributed on an
16#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17#  KIND, either express or implied.  See the License for the
18#  specific language governing permissions and limitations
19#  under the License.
20#
21# *************************************************************
22
23ADD="no"
24LINK="no"
25UPDATE="no"
26USAGE="Usage: $0 [-a] [-l] [-h] <pkg-source-dir> <office-installation-dir>"
27
28help()
29{
30  echo
31  echo "User Mode Installation script for developer and knowledgeable early access tester"
32  echo
33  echo "This installation method is not intended for use in a production environment!"
34  echo "Using this script is unsupported and completely at your own risk"
35  echo
36  echo "Usage:" $0 "<pkg-source-dir> <office-installation-dir> [-l]"
37  echo "    <pkg-source-dir>:       directory *only* containing the Solaris pkg packages to be installed"
38  echo "                            or language pack shell script containing the Solaris pkg packages"
39  echo "    <office-installation-dir>: directory to where the office and the pkg database will get installed into"
40  echo
41  echo "Optional Parameter:"
42  echo "    -a :              add to an existing <office-installation-dir>"
43  echo "    -l :              create a link \"soffice\" in $HOME"
44  echo "    -h :              output this help"
45}
46
47try_to_unpack_languagepack_file()
48{
49  FILENAME=$PACKAGE_PATH
50
51  # Checking, if $FILENAME is a language pack.
52  # String "language package" has to exist in the shell script file.
53  # If this is no language pack, the installation is not supported
54
55  SEARCHSTRING=`head -n 10 $FILENAME | grep "language package"`
56
57  if [ ! -z "$SEARCHSTRING" ]
58  then
59    echo "First parameter $FILENAME is a language pack";
60  else
61    printf "\nERROR: First parameter $FILENAME is a file, but no language pack shell script.\n"
62    echo $USAGE
63    exit 2
64  fi
65
66  echo "Unpacking shell script $FILENAME"
67  # TAILLINE=`head -n 20 $FILENAME | sed --quiet 's/linenum=//p'`
68  TAILLINE=`head -n 20 $FILENAME | sed -n 's/linenum=//p'`
69
70  if [ -x "/usr/bin/mktemp" ]  # available in Solaris 10
71  then
72    UNPACKDIR=`mktemp -d`
73  else
74    UNPACKDIR=/var/tmp/install_$$
75    mkdir $UNPACKDIR
76  fi
77
78  echo $UNPACKDIR
79  tail +$TAILLINE $FILENAME | gunzip | (cd $UNPACKDIR; tar xvf -)
80
81  # Setting the new package path, in which the packages exist
82  PACKAGE_PATH=$UNPACKDIR
83
84  # Setting variable UPDATE, because an Office installation has to exist, if a language pack shall be installed
85  UPDATE="yes"
86}
87
88pkg_error()
89{
90  # pkg command failed, check for admin log and report help
91  if [ -f /tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e ]
92  then
93    echo "####################################################################"
94    echo "#     Installation failed due to stale administrative lock         #"
95    echo "####################################################################"
96    printf "\nERROR: please remove the following file first:\n"
97	ls -l /tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e
98  fi
99  rm -f $GETUID_SO
100  exit 1
101}
102
103get_pkg_list()
104{
105  cd $1; ls -1
106}
107
108#
109# this script is for userland not for root
110#
111
112if [ $UID -eq 0 ]
113then
114  printf "\nThis script is for installation without administrative rights only\nPlease use pkgadd/patchadd to install as root\n"
115  help
116  exit 2
117fi
118
119while getopts "alh" VALUE
120do
121  echo $VALUE
122  case $VALUE in
123    a)      ADD="yes"; break;;
124    h)      help; exit 0;;
125    l)      LINK="yes"; break;;
126    ?)      echo $USAGE; exit 2;;
127  esac
128done
129shift `expr $OPTIND - 1`
130
131if [ $# != 2 ]
132then
133  echo $USAGE
134  exit 2
135fi
136
137# Determine whether this is a patch or a regular install set ..
138/bin/bash -c "ls $1/*/patchinfo >/dev/null 2>&1"
139if [ "$?" = 0 ]
140then
141  UPDATE="yes"
142  PATCH_PATH="$1"
143  PATCH_INFO_LIST=`/bin/bash -c "cd $1; ls */patchinfo"`
144  PATCH_LIST=`for i in ${PATCH_INFO_LIST}; do dirname $i; done`
145elif [ -f "$1/patchinfo" ]
146then
147  UPDATE="yes"
148  PATCH_PATH=`dirname "$1"`
149  PATCH_LIST=`basename "$1"`
150else
151  if [ -d "$1/packages" ]
152  then
153    PACKAGE_PATH="$1/packages"
154  else
155    PACKAGE_PATH=$1
156  fi
157
158  #
159  # If the first parameter is a shell script (download installation set), the packages have to
160  # be unpacked into temp directory
161  #
162  if [ -f "$PACKAGE_PATH" ]
163  then
164    try_to_unpack_languagepack_file
165  fi
166
167  #
168  # Create sed filter script for unwanted packages
169  #
170
171  cat > /tmp/userinstall_filer.$$ << EOF
172/SUNWadabas/d
173/^SUNWj[0-9]/d
174/-desktop-int/d
175/-shared-mime-info/d
176/-cde/d
177EOF
178
179  # Do not install gnome-integration package on systems without GNOME
180  pkginfo -q SUNWgnome-vfs
181  if [ $? -ne 0 ]
182  then
183
184    echo '/-gnome/d' >> /tmp/userinstall_filer.$$
185  fi
186
187  # pkgdep sorts the packages based on their dependencies
188  PKGDEP="`dirname $0`/pkgdep"
189  if [ ! -x $PKGDEP ]; then
190    PKGDEP="get_pkg_list"
191  fi
192
193  #
194  # Get the list of packages to install
195  #
196
197  PKG_LIST=`$PKGDEP $PACKAGE_PATH | sed -f  /tmp/userinstall_filer.$$`
198  rm -f /tmp/userinstall_filer.$$
199
200  if [ -z "$PKG_LIST" ]
201  then
202    printf "\n$0: No packages found in $PACKAGE_PATH\n"
203    exit 2
204  fi
205
206  echo "Packages found:"
207  for i in $PKG_LIST ; do
208    echo $i
209  done
210fi
211
212INSTALL_ROOT=$2
213if [ "$UPDATE" = "yes" ]
214then
215  if [ ! -d ${INSTALL_ROOT}/var/sadm/install/admin ]
216  then
217    printf "\n$0: No package database in ${INSTALL_ROOT}.\n"
218    exit 2
219  fi
220else
221  if [ "$ADD" = "no" ]
222  then
223    rmdir ${INSTALL_ROOT} 2>/dev/null
224    if [ -d ${INSTALL_ROOT} ]
225    then
226      printf "\n$0: ${INSTALL_ROOT} exists and is not empty.\n"
227      exit 2
228    fi
229  fi
230  mkdir -p ${INSTALL_ROOT}/var/sadm/install/admin
231fi
232
233# Previous versions of this script did not write this file
234if [ ! -f ${INSTALL_ROOT}/var/sadm/install/admin/default ]
235then
236  cat > ${INSTALL_ROOT}/var/sadm/install/admin/default << EOF
237action=nocheck
238conflict=nocheck
239setuid=nocheck
240idepend=nocheck
241mail=
242EOF
243fi
244
245if [ ! "${INSTALL_ROOT:0:1}" = "/" ]; then
246  INSTALL_ROOT=`cd ${INSTALL_ROOT}; pwd`
247fi
248
249# This script must exist to make extension registration work
250# always overwrite to get the latest version.
251mkdir -p ${INSTALL_ROOT}/usr/lib
252cat > ${INSTALL_ROOT}/usr/lib/postrun << \EOF
253#!/bin/sh
254set -e
255
256# Override UserInstallation in bootstraprc for unopkg ..
257UserInstallation='$OOO_BASE_DIR/../UserInstallation'
258export UserInstallation
259
260if [ -x /usr/bin/mktemp ]
261then
262  CMD=`/usr/bin/mktemp /tmp/userinstall.XXXXXX`
263else
264  CMD=/tmp/userinstall.$$; echo "" > $CMD
265fi
266
267sed -e 's|/opt/|${PKG_INSTALL_ROOT}/opt/|g' > $CMD
268/bin/sh -e $CMD
269rm -f $CMD
270EOF
271chmod +x ${INSTALL_ROOT}/usr/lib/postrun 2>/dev/null
272
273# create local tmp directory to install on S10
274LOCAL_TMP=
275if [ -x /usr/bin/mktemp ]
276then
277  LOCAL_TMP=`mktemp -d`
278  rmdir ${INSTALL_ROOT}/tmp 2>/dev/null
279  ln -s ${LOCAL_TMP} ${INSTALL_ROOT}/tmp
280fi
281
282#
283# the tail of the script contains a shared object for overloading the getuid()
284# and a few other calls
285#
286
287GETUID_SO=/tmp/getuid.so.$$
288linenum=???
289tail +$linenum $0 > $GETUID_SO
290
291#
292# Perform the installation
293#
294if [ "$UPDATE" = "yes" ]
295then
296  # restore original "bootstraprc" prior to patching
297  for i in ${PKG_LIST}; do
298    my_dir=${INSTALL_ROOT}`pkgparam -d ${PACKAGE_PATH} "$i" BASEDIR`
299    find "$my_dir" -type f -name bootstraprc.orig -exec sh -ce 'mv "$0" `dirname "$0"`/bootstraprc' {} \;
300  done
301
302  # copy INST_RELEASE file
303  if [ ! -f ${INSTALL_ROOT}/var/sadm/system/admin/INST_RELEASE ]
304  then
305    mkdir -p ${INSTALL_ROOT}/var/sadm/system/admin 2>/dev/null
306    cp -f /var/sadm/system/admin/INST_RELEASE ${INSTALL_ROOT}/var/sadm/system/admin/INST_RELEASE
307  fi
308
309  # The case UPDATE="yes" is valid for patch installation and for language packs.
310  # For patches the variable PKG_LIST is empty, for language packs it is not empty.
311  # Patches have to be installed with patchadd, language packs with pkgadd
312
313  if [ -z "${PKG_LIST}" ]
314  then
315    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
316  else
317    LD_PRELOAD_32=$GETUID_SO /usr/sbin/pkgadd -d ${PACKAGE_PATH} -R ${INSTALL_ROOT} ${PKG_LIST} >/dev/null || pkg_error
318  fi
319
320else
321
322  # Create BASEDIR directories to avoid manual user interaction
323  for i in ${PKG_LIST}; do
324    mkdir -m 0755 -p ${INSTALL_ROOT}`pkgparam -d ${PACKAGE_PATH} $i BASEDIR` 2>/dev/null
325  done
326
327  if [ ! "${INSTALL_ROOT:0:1}" = "/" ]; then
328    INSTALL_ROOT=`cd ${INSTALL_ROOT}; pwd`
329  fi
330
331  echo "####################################################################"
332  echo "#     Installation of the found packages                           #"
333  echo "####################################################################"
334  echo
335  echo "Path to the packages       : " $PACKAGE_PATH
336  echo "Path to the installation   : " $INSTALL_ROOT
337
338  LD_PRELOAD_32=$GETUID_SO /usr/sbin/pkgadd -d ${PACKAGE_PATH} -R ${INSTALL_ROOT} ${PKG_LIST} >/dev/null || pkg_error
339fi
340
341rm -f $GETUID_SO
342rm -f /tmp/.ai.pkg.zone.lock*
343
344# remove local tmp directory
345if [ ! -z ${LOCAL_TMP} ]
346then
347  rm -f ${LOCAL_TMP}/.ai.pkg.zone.lock*
348  rmdir ${LOCAL_TMP}
349  rm -f ${INSTALL_ROOT}/tmp
350  mkdir ${INSTALL_ROOT}/tmp
351fi
352
353#
354# Create a link into the users home directory
355#
356
357if [ "$LINK" = "yes" ]
358then
359  rm -f $HOME/soffice 2>/dev/null
360  find `cd "$INSTALL_ROOT" && pwd` -name soffice -type f -perm -u+x -exec /bin/sh -ce 'ln -sf "$0" "$HOME/soffice" && echo "Creating link from $0 to $HOME/soffice"' {} \;
361fi
362
363# patch the "bootstraprc" to create a self-containing installation
364for i in ${PKG_LIST}; do
365  my_dir=${INSTALL_ROOT}`pkgparam -d ${PACKAGE_PATH} "$i" BASEDIR`
366  find "$my_dir" -type f -name bootstraprc -exec sh -ce 'test ! -f "$0".orig && mv "$0" "$0".orig && sed '\''s,^UserInstallation=$SYSUSERCONFIG.*,UserInstallation=$OOO_BASE_DIR/../UserInstallation,'\'' "$0".orig > "$0"' {} \;
367done
368
369# if an unpack directory exists, it can be removed now
370if [ ! -z "$UNPACKDIR" ]
371then
372  # for i in ${PKG_LIST}; do
373  #   cd $UNPACKDIR; rm -rf $i
374  # done
375  # rmdir $UNPACKDIR
376
377  rm -rf $UNPACKDIR
378  echo "Removed temporary directory $UNPACKDIR"
379fi
380
381echo
382echo "Installation done ..."
383exit 0
384
385