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