1*b1cdbd2cSJim Jagielski#!/bin/bash
2*b1cdbd2cSJim Jagielski# *************************************************************
3*b1cdbd2cSJim Jagielski#
4*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
5*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
6*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
7*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
8*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
9*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
10*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
11*b1cdbd2cSJim Jagielski#
12*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
13*b1cdbd2cSJim Jagielski#
14*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
15*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
16*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
18*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
19*b1cdbd2cSJim Jagielski#  under the License.
20*b1cdbd2cSJim Jagielski#
21*b1cdbd2cSJim Jagielski# *************************************************************
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim JagielskiADD="no"
24*b1cdbd2cSJim JagielskiLINK="no"
25*b1cdbd2cSJim JagielskiUPDATE="ask"
26*b1cdbd2cSJim JagielskiUNPACKDIR=""
27*b1cdbd2cSJim JagielskiUSAGE="Usage: $0 [-a,--add] [-l,--link] [-U,--update] [-h,--help] <rpm-source-dir> <office-installation-dir>"
28*b1cdbd2cSJim Jagielski
29*b1cdbd2cSJim Jagielskihelp()
30*b1cdbd2cSJim Jagielski{
31*b1cdbd2cSJim Jagielski  echo
32*b1cdbd2cSJim Jagielski  echo "User Mode Installation script for developer and knowledgeable early access tester"
33*b1cdbd2cSJim Jagielski  echo
34*b1cdbd2cSJim Jagielski  echo "This installation method is not intended for use in a production environment!"
35*b1cdbd2cSJim Jagielski  echo "Using this script is unsupported and completely at your own risk"
36*b1cdbd2cSJim Jagielski  echo
37*b1cdbd2cSJim Jagielski  echo "Usage:" $0 [-lU] "<rpm-source-dir> <office-installation-dir>"
38*b1cdbd2cSJim Jagielski  echo "    <rpm-source-dir>: directory *only* containing the Linux rpm packages to be installed"
39*b1cdbd2cSJim Jagielski  echo "                      or language pack shell script containing the rpm packages"
40*b1cdbd2cSJim Jagielski  echo "    <office-installation-dir>: directory to where the office will get installed into"
41*b1cdbd2cSJim Jagielski  echo
42*b1cdbd2cSJim Jagielski  echo "Optional Parameter:"
43*b1cdbd2cSJim Jagielski  echo "    -a,--add:               add to an existing <office-installation-dir>"
44*b1cdbd2cSJim Jagielski  echo "    -l,--link:              create a link \"soffice\" in $HOME"
45*b1cdbd2cSJim Jagielski  echo "    -U,--update:            update without asking"
46*b1cdbd2cSJim Jagielski  echo "    -h,--help:              output this help"
47*b1cdbd2cSJim Jagielski  echo
48*b1cdbd2cSJim Jagielski}
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielskitry_to_unpack_languagepack_file()
51*b1cdbd2cSJim Jagielski{
52*b1cdbd2cSJim Jagielski  FILENAME=$PACKAGE_PATH
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski  # Checking, if $FILENAME is a language pack.
55*b1cdbd2cSJim Jagielski  # String "language package" has to exist in the shell script file.
56*b1cdbd2cSJim Jagielski  # If this is no language pack, the installation is not supported
57*b1cdbd2cSJim Jagielski
58*b1cdbd2cSJim Jagielski  SEARCHSTRING=`head --lines=10 $FILENAME | grep "language package"`
59*b1cdbd2cSJim Jagielski
60*b1cdbd2cSJim Jagielski  if [ ! -z "$SEARCHSTRING" ]
61*b1cdbd2cSJim Jagielski  then
62*b1cdbd2cSJim Jagielski    echo "First parameter $FILENAME is a language pack";
63*b1cdbd2cSJim Jagielski  else
64*b1cdbd2cSJim Jagielski    printf "\nERROR: First parameter $FILENAME is a file, but no language pack shell script.\n"
65*b1cdbd2cSJim Jagielski    echo $USAGE
66*b1cdbd2cSJim Jagielski    exit 2
67*b1cdbd2cSJim Jagielski  fi
68*b1cdbd2cSJim Jagielski
69*b1cdbd2cSJim Jagielski  echo "Unpacking shell script $FILENAME"
70*b1cdbd2cSJim Jagielski  TAILLINE=`head --lines=20 $FILENAME | sed --quiet 's/linenum=//p'`
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski  UNPACKDIR=/var/tmp/install_$$
73*b1cdbd2cSJim Jagielski  mkdir $UNPACKDIR
74*b1cdbd2cSJim Jagielski  # UNPACKDIR=`mktemp -d`
75*b1cdbd2cSJim Jagielski  tail -n +$TAILLINE $FILENAME | gunzip | (cd $UNPACKDIR; tar xvf -)
76*b1cdbd2cSJim Jagielski
77*b1cdbd2cSJim Jagielski  # Setting the new package path, in which the packages exist
78*b1cdbd2cSJim Jagielski  PACKAGE_PATH=$UNPACKDIR
79*b1cdbd2cSJim Jagielski
80*b1cdbd2cSJim Jagielski  # Setting variable UPDATE, because an Office installation has to exist, if a language pack shall be installed
81*b1cdbd2cSJim Jagielski  UPDATE="yes"
82*b1cdbd2cSJim Jagielski}
83*b1cdbd2cSJim Jagielski
84*b1cdbd2cSJim Jagielski#
85*b1cdbd2cSJim Jagielski# this script is for userland not for root
86*b1cdbd2cSJim Jagielski#
87*b1cdbd2cSJim Jagielski
88*b1cdbd2cSJim Jagielskiif [ $UID -eq 0 ]
89*b1cdbd2cSJim Jagielskithen
90*b1cdbd2cSJim Jagielski  printf "\nThis script is for installation without administrative rights only\nPlease use rpm to install as root\n"
91*b1cdbd2cSJim Jagielski  help
92*b1cdbd2cSJim Jagielski  exit 2
93*b1cdbd2cSJim Jagielskifi
94*b1cdbd2cSJim Jagielski
95*b1cdbd2cSJim Jagielskiset -- `getopt -u -o 'alhU' -l 'add,link,help,update' -- $*`
96*b1cdbd2cSJim Jagielski
97*b1cdbd2cSJim Jagielskiif [ $? != 0 ]
98*b1cdbd2cSJim Jagielskithen
99*b1cdbd2cSJim Jagielski  echo $USAGE
100*b1cdbd2cSJim Jagielski  exit 2
101*b1cdbd2cSJim Jagielskifi
102*b1cdbd2cSJim Jagielski
103*b1cdbd2cSJim Jagielskifor i in $*
104*b1cdbd2cSJim Jagielskido
105*b1cdbd2cSJim Jagielski  case $i in
106*b1cdbd2cSJim Jagielski    -a|--add)       ADD="yes"; shift;;
107*b1cdbd2cSJim Jagielski    -h|--help)      help; exit 0;;
108*b1cdbd2cSJim Jagielski    -l|--link)      LINK="yes"; shift;;
109*b1cdbd2cSJim Jagielski    -U|--update)    UPDATE="yes"; shift;;
110*b1cdbd2cSJim Jagielski    --)             shift; break;;
111*b1cdbd2cSJim Jagielski   esac
112*b1cdbd2cSJim Jagielskidone
113*b1cdbd2cSJim Jagielski
114*b1cdbd2cSJim Jagielskiif [ $# != 2 ]
115*b1cdbd2cSJim Jagielskithen
116*b1cdbd2cSJim Jagielski  echo $USAGE
117*b1cdbd2cSJim Jagielski  exit 2
118*b1cdbd2cSJim Jagielskifi
119*b1cdbd2cSJim Jagielski
120*b1cdbd2cSJim JagielskiPACKAGE_PATH=$1
121*b1cdbd2cSJim Jagielski
122*b1cdbd2cSJim Jagielski#
123*b1cdbd2cSJim Jagielski# If the first parameter is a shell script (download installation set), the packages have to
124*b1cdbd2cSJim Jagielski# be unpacked into temp directory
125*b1cdbd2cSJim Jagielski#
126*b1cdbd2cSJim Jagielski
127*b1cdbd2cSJim Jagielskiif [ -f "$PACKAGE_PATH" ]
128*b1cdbd2cSJim Jagielskithen
129*b1cdbd2cSJim Jagielski  try_to_unpack_languagepack_file
130*b1cdbd2cSJim Jagielskifi
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski#
133*b1cdbd2cSJim Jagielski# Check and get the list of packages to install
134*b1cdbd2cSJim Jagielski#
135*b1cdbd2cSJim Jagielski
136*b1cdbd2cSJim JagielskiRPMLIST=`find $PACKAGE_PATH -maxdepth 2 -type f -name "*.rpm" ! -name "*-menus-*" ! -name "*-desktop-integration-*" ! -name "jre*" ! -name "*-userland-*" -print`
137*b1cdbd2cSJim Jagielski
138*b1cdbd2cSJim Jagielskiif [ -z "$RPMLIST" ]
139*b1cdbd2cSJim Jagielskithen
140*b1cdbd2cSJim Jagielski  printf "\n$0: No packages found in $PACKAGE_PATH\n"
141*b1cdbd2cSJim Jagielski  exit 2
142*b1cdbd2cSJim Jagielskifi
143*b1cdbd2cSJim Jagielski
144*b1cdbd2cSJim Jagielski# #163256# check if we are on a debian system...
145*b1cdbd2cSJim Jagielskiif rpm --help | grep debian >/dev/null;
146*b1cdbd2cSJim Jagielskithen
147*b1cdbd2cSJim Jagielski    DEBIAN_FLAGS="--force-debian --nodeps"
148*b1cdbd2cSJim Jagielskielse
149*b1cdbd2cSJim Jagielski    DEBIAN_FLAGS=
150*b1cdbd2cSJim Jagielskifi
151*b1cdbd2cSJim Jagielski
152*b1cdbd2cSJim Jagielski#
153*b1cdbd2cSJim Jagielski# Determine whether this should be an update or a fresh install
154*b1cdbd2cSJim Jagielski#
155*b1cdbd2cSJim Jagielski
156*b1cdbd2cSJim JagielskiINSTALLDIR=$2
157*b1cdbd2cSJim JagielskiRPM_DB_PATH=${INSTALLDIR}/var/lib/rpm
158*b1cdbd2cSJim Jagielski
159*b1cdbd2cSJim Jagielski# Check for versionrc
160*b1cdbd2cSJim Jagielskiif [ -f ${INSTALLDIR}/program/versionrc ]; then VERSIONRC=versionrc; fi
161*b1cdbd2cSJim Jagielski
162*b1cdbd2cSJim Jagielskiif [ "$UPDATE" = "ask" ]
163*b1cdbd2cSJim Jagielskithen
164*b1cdbd2cSJim Jagielski  PRODUCT=`sed --silent -e "
165*b1cdbd2cSJim Jagielski/^buildid=/ {
166*b1cdbd2cSJim Jagielskis/buildid=\(.*\)/ [\1]/
167*b1cdbd2cSJim Jagielskih
168*b1cdbd2cSJim Jagielski}
169*b1cdbd2cSJim Jagielski/^ProductKey=/ {
170*b1cdbd2cSJim Jagielskis/ProductKey=//
171*b1cdbd2cSJim JagielskiG
172*b1cdbd2cSJim Jagielskip
173*b1cdbd2cSJim Jagielski}" ${INSTALLDIR}/program/${VERSIONRC:-bootstraprc} 2>/dev/null | tr -d "\012"`
174*b1cdbd2cSJim Jagielski
175*b1cdbd2cSJim Jagielski  if [ ! -z "$PRODUCT" ]
176*b1cdbd2cSJim Jagielski  then
177*b1cdbd2cSJim Jagielski    echo
178*b1cdbd2cSJim Jagielski    echo "Found an installation of $PRODUCT in $INSTALLDIR"
179*b1cdbd2cSJim Jagielski    echo
180*b1cdbd2cSJim Jagielski    while [ "$UPDATE" != "yes" ]
181*b1cdbd2cSJim Jagielski    do
182*b1cdbd2cSJim Jagielski      read -a UPDATE -p "Do you want to update this installation (yes/no)? "
183*b1cdbd2cSJim Jagielski      if [ "$UPDATE" = "no" ]
184*b1cdbd2cSJim Jagielski      then
185*b1cdbd2cSJim Jagielski        exit 2
186*b1cdbd2cSJim Jagielski      fi
187*b1cdbd2cSJim Jagielski    done
188*b1cdbd2cSJim Jagielski  elif [ -d $RPM_DB_PATH -a "$ADD" = "no" ]
189*b1cdbd2cSJim Jagielski  then
190*b1cdbd2cSJim Jagielski    echo
191*b1cdbd2cSJim Jagielski    echo "The following packages are already installed in $INSTALLDIR"
192*b1cdbd2cSJim Jagielski    echo
193*b1cdbd2cSJim Jagielski    rpm --dbpath `cd $RPM_DB_PATH; pwd` --query --all
194*b1cdbd2cSJim Jagielski    echo
195*b1cdbd2cSJim Jagielski    while [ "$UPDATE" != "yes" ]
196*b1cdbd2cSJim Jagielski    do
197*b1cdbd2cSJim Jagielski      read -a UPDATE -p "Do you want to continue with this installation (yes/no)? "
198*b1cdbd2cSJim Jagielski      if [ "$UPDATE" = "no" ]
199*b1cdbd2cSJim Jagielski      then
200*b1cdbd2cSJim Jagielski        exit 2
201*b1cdbd2cSJim Jagielski      fi
202*b1cdbd2cSJim Jagielski    done
203*b1cdbd2cSJim Jagielski  else
204*b1cdbd2cSJim Jagielski    UPDATE="no"
205*b1cdbd2cSJim Jagielski  fi
206*b1cdbd2cSJim Jagielskifi
207*b1cdbd2cSJim Jagielski
208*b1cdbd2cSJim Jagielski#
209*b1cdbd2cSJim Jagielski# Check/Create installation directory
210*b1cdbd2cSJim Jagielski#
211*b1cdbd2cSJim Jagielski
212*b1cdbd2cSJim Jagielskiif [ "$UPDATE" = "yes" ]
213*b1cdbd2cSJim Jagielskithen
214*b1cdbd2cSJim Jagielski  # restore original bootstraprc
215*b1cdbd2cSJim Jagielski  mv -f ${INSTALLDIR}/program/bootstraprc.orig ${INSTALLDIR}/program/bootstraprc 2>/dev/null
216*b1cdbd2cSJim Jagielski
217*b1cdbd2cSJim Jagielski  # the RPM_DB_PATH must be absolute
218*b1cdbd2cSJim Jagielski  if [ ! "${RPM_DB_PATH:0:1}" = "/" ]; then
219*b1cdbd2cSJim Jagielski    RPM_DB_PATH=`cd ${RPM_DB_PATH}; pwd`
220*b1cdbd2cSJim Jagielski  fi
221*b1cdbd2cSJim Jagielski
222*b1cdbd2cSJim Jagielski  # we should use --freshen for updates to not add languages with patches, but this will break
223*b1cdbd2cSJim Jagielski  # language packs, so leave it for now ..
224*b1cdbd2cSJim Jagielski#  RPMCMD="--freshen"
225*b1cdbd2cSJim Jagielski  RPMCMD="--upgrade"
226*b1cdbd2cSJim Jagielskielse
227*b1cdbd2cSJim Jagielski  rmdir ${INSTALLDIR} 2>/dev/null
228*b1cdbd2cSJim Jagielski
229*b1cdbd2cSJim Jagielski  if [ -d  ${INSTALLDIR} -a "$ADD" = "no" ]
230*b1cdbd2cSJim Jagielski  then
231*b1cdbd2cSJim Jagielski    printf "\n$0: ${INSTALLDIR} exists and is not empty.\n"
232*b1cdbd2cSJim Jagielski    exit 2
233*b1cdbd2cSJim Jagielski  fi
234*b1cdbd2cSJim Jagielski
235*b1cdbd2cSJim Jagielski  mkdir -p $RPM_DB_PATH || exit 2
236*b1cdbd2cSJim Jagielski  # XXX why ? XXX
237*b1cdbd2cSJim Jagielski  chmod 700 $RPM_DB_PATH
238*b1cdbd2cSJim Jagielski
239*b1cdbd2cSJim Jagielski  # the RPM_DB_PATH must be absolute
240*b1cdbd2cSJim Jagielski  if [ ! "${RPM_DB_PATH:0:1}" = "/" ]; then
241*b1cdbd2cSJim Jagielski    RPM_DB_PATH=`cd ${RPM_DB_PATH}; pwd`
242*b1cdbd2cSJim Jagielski  fi
243*b1cdbd2cSJim Jagielski
244*b1cdbd2cSJim Jagielski  # Creating RPM database and initializing
245*b1cdbd2cSJim Jagielski  if [ "$ADD" = "no" ]; then
246*b1cdbd2cSJim Jagielski    rpm --initdb --dbpath $RPM_DB_PATH
247*b1cdbd2cSJim Jagielski  fi
248*b1cdbd2cSJim Jagielski
249*b1cdbd2cSJim Jagielski  # Default install command
250*b1cdbd2cSJim Jagielski  RPMCMD="--install"
251*b1cdbd2cSJim Jagielskifi
252*b1cdbd2cSJim Jagielski
253*b1cdbd2cSJim Jagielski# populate the private rpm database with the dependencies needed
254*b1cdbd2cSJim JagielskiFAKEDBRPM=/tmp/fake-db-1.0-$$.noarch.rpm
255*b1cdbd2cSJim Jagielskilinenum=???
256*b1cdbd2cSJim Jagielskitail -n +$linenum $0 > $FAKEDBRPM
257*b1cdbd2cSJim Jagielski
258*b1cdbd2cSJim Jagielskirpm ${DEBIAN_FLAGS} --upgrade --ignoresize --dbpath $RPM_DB_PATH $FAKEDBRPM
259*b1cdbd2cSJim Jagielski
260*b1cdbd2cSJim Jagielskirm -f $FAKEDBRPM
261*b1cdbd2cSJim Jagielski
262*b1cdbd2cSJim Jagielskiecho "Packages found:"
263*b1cdbd2cSJim Jagielskifor i in $RPMLIST ; do
264*b1cdbd2cSJim Jagielski  echo `basename $i`
265*b1cdbd2cSJim Jagielskidone
266*b1cdbd2cSJim Jagielski
267*b1cdbd2cSJim Jagielski#
268*b1cdbd2cSJim Jagielski# Perform the installation
269*b1cdbd2cSJim Jagielski#
270*b1cdbd2cSJim Jagielski
271*b1cdbd2cSJim Jagielskiecho
272*b1cdbd2cSJim Jagielskiecho "####################################################################"
273*b1cdbd2cSJim Jagielskiecho "#     Installation of the found packages                           #"
274*b1cdbd2cSJim Jagielskiecho "####################################################################"
275*b1cdbd2cSJim Jagielskiecho
276*b1cdbd2cSJim Jagielskiecho "Path to the database:       " $RPM_DB_PATH
277*b1cdbd2cSJim Jagielskiecho "Path to the packages:       " $PACKAGE_PATH
278*b1cdbd2cSJim Jagielskiecho "Path to the installation:   " $INSTALLDIR
279*b1cdbd2cSJim Jagielskiecho
280*b1cdbd2cSJim Jagielskiecho "Installing the RPMs"
281*b1cdbd2cSJim Jagielski
282*b1cdbd2cSJim JagielskiABSROOT=`cd ${INSTALLDIR}; pwd`
283*b1cdbd2cSJim JagielskiRELOCATIONS=`rpm -qp --qf "--relocate %{PREFIXES}=${ABSROOT}%{PREFIXES} \n" $RPMLIST | sort -u | tr -d "\012"`
284*b1cdbd2cSJim JagielskiUserInstallation=\$OOO_BASE_DIR/../UserInstallation rpm ${DEBIAN_FLAGS} $RPMCMD --ignoresize -vh $RELOCATIONS --dbpath $RPM_DB_PATH $RPMLIST
285*b1cdbd2cSJim Jagielski
286*b1cdbd2cSJim Jagielski#
287*b1cdbd2cSJim Jagielski# Create a link into the users home directory
288*b1cdbd2cSJim Jagielski#
289*b1cdbd2cSJim Jagielski
290*b1cdbd2cSJim Jagielskiif [ "$LINK" = "yes" ]
291*b1cdbd2cSJim Jagielskithen
292*b1cdbd2cSJim Jagielski  find `cd "$INSTALLDIR" && pwd` -name soffice -type f -perm /u+x -exec /bin/bash -ce 'ln -sf "$0" "$HOME/soffice" && echo "Creating link from $0 to $HOME/soffice"' {} \;
293*b1cdbd2cSJim Jagielskifi
294*b1cdbd2cSJim Jagielski
295*b1cdbd2cSJim Jagielskiif [ "$UPDATE" = "yes" -a ! -f $INSTALLDIR/program/bootstraprc ]
296*b1cdbd2cSJim Jagielskithen
297*b1cdbd2cSJim Jagielski  echo
298*b1cdbd2cSJim Jagielski  echo "Update failed due to a bug in RPM, uninstalling .."
299*b1cdbd2cSJim Jagielski  rpm ${DEBIAN_FLAGS} --erase -v --nodeps --dbpath $RPM_DB_PATH `rpm --query --queryformat "%{NAME} " --package $RPMLIST --dbpath $RPM_DB_PATH`
300*b1cdbd2cSJim Jagielski  echo
301*b1cdbd2cSJim Jagielski  echo "Now re-installing new packages .."
302*b1cdbd2cSJim Jagielski  echo
303*b1cdbd2cSJim Jagielski  rpm ${DEBIAN_FLAGS} --install --nodeps --ignoresize -vh $RELOCATIONS --dbpath $RPM_DB_PATH $RPMLIST
304*b1cdbd2cSJim Jagielski  echo
305*b1cdbd2cSJim Jagielskifi
306*b1cdbd2cSJim Jagielski
307*b1cdbd2cSJim Jagielski# patch the "bootstraprc" to create a self-containing installation
308*b1cdbd2cSJim Jagielskifind "$INSTALLDIR" -type f -name bootstraprc -exec /bin/bash -ce 'test ! -e "$0".orig && mv "$0" "$0".orig && sed '\''s,^UserInstallation=$SYSUSERCONFIG.*,UserInstallation=$OOO_BASE_DIR/../UserInstallation,'\'' "$0".orig > "$0"' {} \;
309*b1cdbd2cSJim Jagielski
310*b1cdbd2cSJim Jagielski# if an unpack directory exists, it can be removed now
311*b1cdbd2cSJim Jagielskiif [ ! -z "$UNPACKDIR" ]
312*b1cdbd2cSJim Jagielskithen
313*b1cdbd2cSJim Jagielski  rm $UNPACKDIR/*.rpm
314*b1cdbd2cSJim Jagielski  rmdir $UNPACKDIR
315*b1cdbd2cSJim Jagielski  echo "Removed temporary directory $UNPACKDIR"
316*b1cdbd2cSJim Jagielskifi
317*b1cdbd2cSJim Jagielski
318*b1cdbd2cSJim Jagielskiecho
319*b1cdbd2cSJim Jagielskiecho "Installation done ..."
320*b1cdbd2cSJim Jagielski
321*b1cdbd2cSJim Jagielskiexit 0
322