]> icculus.org git repositories - divverent/netradiant.git/blob - setup/linux/makeself/makeself.sh
initial
[divverent/netradiant.git] / setup / linux / makeself / makeself.sh
1 #!/bin/sh
2 #
3 # Makeself version 2.1.x
4 #  by Stephane Peter <megastep@megastep.org>
5 #
6 # $Id: makeself.sh,v 1.40 2003/09/12 02:19:29 megastep Exp $
7 #
8 # Utility to create self-extracting tar.gz archives.
9 # The resulting archive is a file holding the tar.gz archive with
10 # a small Shell script stub that uncompresses the archive to a temporary
11 # directory and then executes a given script from withing that directory.
12 #
13 # Makeself home page: http://www.megastep.org/makeself/
14 #
15 # Version 2.0 is a rewrite of version 1.0 to make the code easier to read and maintain.
16 #
17 # Version history :
18 # - 1.0 : Initial public release
19 # - 1.1 : The archive can be passed parameters that will be passed on to
20 #         the embedded script, thanks to John C. Quillan
21 # - 1.2 : Package distribution, bzip2 compression, more command line options,
22 #         support for non-temporary archives. Ideas thanks to Francois Petitjean
23 # - 1.3 : More patches from Bjarni R. Einarsson and Francois Petitjean:
24 #         Support for no compression (--nocomp), script is no longer mandatory,
25 #         automatic launch in an xterm, optional verbose output, and -target 
26 #         archive option to indicate where to extract the files.
27 # - 1.4 : Improved UNIX compatibility (Francois Petitjean)
28 #         Automatic integrity checking, support of LSM files (Francois Petitjean)
29 # - 1.5 : Many bugfixes. Optionally disable xterm spawning.
30 # - 1.5.1 : More bugfixes, added archive options -list and -check.
31 # - 1.5.2 : Cosmetic changes to inform the user of what's going on with big 
32 #           archives (Quake III demo)
33 # - 1.5.3 : Check for validity of the DISPLAY variable before launching an xterm.
34 #           More verbosity in xterms and check for embedded command's return value.
35 #           Bugfix for Debian 2.0 systems that have a different "print" command.
36 # - 1.5.4 : Many bugfixes. Print out a message if the extraction failed.
37 # - 1.5.5 : More bugfixes. Added support for SETUP_NOCHECK environment variable to
38 #           bypass checksum verification of archives.
39 # - 1.6.0 : Compute MD5 checksums with the md5sum command (patch from Ryan Gordon)
40 # - 2.0   : Brand new rewrite, cleaner architecture, separated header and UNIX ports.
41 # - 2.0.1 : Added --copy
42 # - 2.1.0 : Allow multiple tarballs to be stored in one archive, and incremental updates.
43 #           Added --nochown for archives
44 #           Stopped doing redundant checksums when not necesary
45 # - 2.1.1 : Work around insane behavior from certain Linux distros with no 'uncompress' command
46 #           Cleaned up the code to handle error codes from compress. Simplified the extraction code.
47 # - 2.1.2 : Some bug fixes. Use head -n to avoid problems.
48 # - 2.1.3 : Bug fixes with command line when spawning terminals.
49 #           Added --tar for archives, allowing to give arbitrary arguments to tar on the contents of the archive.
50 #
51 # (C) 1998-2003 by Stéphane Peter <megastep@megastep.org>
52 #
53 # This software is released under the terms of the GNU GPL
54 # Please read the license at http://www.gnu.org/copyleft/gpl.html
55 #
56
57 MS_VERSION=2.1.3
58
59 # Procedures
60
61 MS_Usage()
62 {
63     echo "Usage: $0 [params] archive_dir file_name label [startup_script] [args]"
64     echo "params can be one or more of the following :"
65     echo "    --version | -v  : Print out Makeself version number and exit"
66     echo "    --help | -h     : Print out this help message"
67     echo "    --gzip          : Compress using gzip (default if detected)"
68     echo "    --bzip2         : Compress using bzip2 instead of gzip"
69     echo "    --compress      : Compress using the UNIX 'compress' command"
70     echo "    --nocomp        : Do not compress the data"
71     echo "    --notemp        : The archive will create archive_dir in the"
72     echo "                      current directory and uncompress in ./archive_dir"
73     echo "    --copy          : Upon extraction, the archive will first copy itself to"
74     echo "                      a temporary directory"
75     echo "    --append        : Append more files to an existing Makeself archive"
76     echo "                      The label and startup scripts will then be ignored"
77     echo "    --current       : Files will be extracted to the current directory."
78     echo "                      Implies --notemp."
79     echo "    --header file   : Specify location of the header script"
80     echo "    --follow        : Follow the symlinks in the archive"
81     echo "    --nox11         : Disable automatic spawn of a xterm"
82     echo "    --nowait        : Do not wait for user input after executing embedded"
83     echo "                      program from an xterm"
84     echo "    --lsm file      : LSM file describing the package"
85     echo
86     echo "Do not forget to give a fully qualified startup script name"
87     echo "(i.e. with a ./ prefix if inside the archive)."
88     exit 1
89 }
90
91 # Default settings
92 if type gzip 2>&1 > /dev/null; then
93     COMPRESS=gzip
94 else
95     COMPRESS=Unix
96 fi
97 KEEP=n
98 CURRENT=n
99 NOX11=n
100 APPEND=n
101 COPY=none
102 TAR_ARGS=cvf
103 HEADER=`dirname $0`/makeself-header.sh
104
105 # LSM file stuff
106 LSM_CMD="echo No LSM. >> \"\$archname\""
107
108 while true
109 do
110     case "$1" in
111     --version | -v)
112         echo Makeself version $MS_VERSION
113         exit 0
114         ;;
115     --bzip2)
116         COMPRESS=bzip2
117         shift
118         ;;
119     --gzip)
120         COMPRESS=gzip
121         shift
122         ;;
123     --compress)
124         COMPRESS=Unix
125         shift
126         ;;
127     --nocomp)
128         COMPRESS=none
129         shift
130         ;;
131     --notemp)
132         KEEP=y
133         shift
134         ;;
135     --copy)
136         COPY=copy
137         shift
138         ;;
139     --current)
140         CURRENT=y
141         KEEP=y
142         shift
143         ;;
144     --header)
145         HEADER="$2"
146         shift 2
147         ;;
148     --follow)
149         TAR_ARGS=cvfh
150         shift
151         ;;
152     --nox11)
153         NOX11=y
154         shift
155         ;;
156     --nowait)
157         shift
158         ;;
159     --append)
160         APPEND=y
161         shift
162         ;;
163     --lsm)
164         LSM_CMD="cat \"$2\" >> \"\$archname\""
165         shift 2
166         ;;
167     -h | --help)
168         MS_Usage
169         ;;
170     -*)
171         echo Unrecognized flag : "$1"
172         MS_Usage
173         ;;
174     *)
175         break
176         ;;
177     esac
178 done
179
180 archdir="$1"
181 archname="$2"
182
183 if test "$APPEND" = y; then
184     if test $# -lt 2; then
185         MS_Usage
186     fi
187
188     # Gather the info from the original archive
189     OLDENV=`sh "$archname" --dumpconf`
190     if test $? -ne 0; then
191         echo "Unable to update archive: $archname" >&2
192         exit 1
193     else
194         eval "$OLDENV"
195     fi
196 else
197     if test "$KEEP" = n -a $# = 3; then
198         echo "ERROR: Making a temporary archive with no embedded command does not make sense!" >&2
199         echo
200         MS_Usage
201     fi
202     # We don't really want to create an absolute directory...
203     if test "$CURRENT" = y; then
204         archdirname="."
205     else
206         archdirname=`basename "$1"`
207     fi
208
209     if test $# -lt 3; then
210         MS_Usage
211     fi
212
213     LABEL="$3"
214     SCRIPT="$4"
215     test x$SCRIPT = x || shift 1
216     shift 3
217     SCRIPTARGS="$*"
218 fi
219
220 if test "$KEEP" = n -a "$CURRENT" = y; then
221     echo "ERROR: It is A VERY DANGEROUS IDEA to try to combine --notemp and --current." >&2
222     exit 1
223 fi
224
225 case $COMPRESS in
226 gzip)
227     GZIP_CMD="gzip -c9"
228     GUNZIP_CMD="gzip -cd"
229     ;;
230 bzip2)
231     GZIP_CMD="bzip2 -9"
232     GUNZIP_CMD="bzip2 -d"
233     ;;
234 Unix)
235     GZIP_CMD="compress -cf"
236     GUNZIP_CMD="exec 2>&-; uncompress -c || test \\\$? -eq 2 || gzip -cd"
237     ;;
238 none)
239     GZIP_CMD="cat"
240     GUNZIP_CMD="cat"
241     ;;
242 esac
243
244 tmpfile="${TMPDIR:=/tmp}/mkself$$"
245
246 if test -f $HEADER; then
247         oldarchname="$archname"
248         archname="$tmpfile"
249         # Generate a fake header to count its lines
250         SKIP=0
251     . $HEADER
252     SKIP=`cat "$tmpfile" |wc -l`
253         # Get rid of any spaces
254         SKIP=`expr $SKIP`
255         rm -f "$tmpfile"
256     echo Header is $SKIP lines long >&2
257
258         archname="$oldarchname"
259 else
260     echo "Unable to open header file: $HEADER" >&2
261     exit 1
262 fi
263
264 echo
265
266 if test "$APPEND" = n; then
267     if test -f "$archname"; then
268                 echo "WARNING: Overwriting existing file: $archname" >&2
269     fi
270 fi
271
272 USIZE=`du -ks $archdir | cut -f1`
273 DATE=`LC_ALL=C date`
274
275 echo About to compress $USIZE KB of data...
276 echo Adding files to archive named \"$archname\"...
277 (cd "$archdir"; tar $TAR_ARGS - * | eval "$GZIP_CMD" ) >> "$tmpfile" || { echo Aborting; rm -f "$tmpfile"; exit 1; }
278 echo >> "$tmpfile" >&- # try to close the archive
279
280 fsize=`cat "$tmpfile" | wc -c | tr -d " "`
281
282 # Compute the checksums
283
284 md5sum=00000000000000000000000000000000
285 crcsum=`cat "$tmpfile" | cksum | sed -e 's/ /Z/' -e 's/ /Z/' | cut -dZ -f1`
286 echo "CRC: $crcsum"
287
288 # Try to locate a MD5 binary
289 OLD_PATH=$PATH
290 PATH=${GUESS_MD5_PATH:-"$OLD_PATH:/bin:/usr/bin:/sbin:/usr/local/ssl/bin:/usr/local/bin:/opt/openssl/bin"}
291 MD5_PATH=`type -p md5sum`
292 MD5_PATH=${MD5_PATH:-`type -p md5`}
293 PATH=$OLD_PATH
294
295 if test -x "$MD5_PATH"; then
296         md5sum=`cat "$tmpfile" | "$MD5_PATH" | cut -b-32`;
297         echo "MD5: $md5sum"
298 else
299         echo "MD5: none, md5sum binary not found"
300 fi
301
302 if test "$APPEND" = y; then
303     mv "$archname" "$archname".bak || exit
304
305     # Prepare entry for new archive
306     filesizes="$filesizes $fsize"
307     CRCsum="$CRCsum $crcsum"
308     MD5sum="$MD5sum $md5sum"
309     USIZE=`expr $USIZE + $OLDUSIZE`
310     # Generate the header
311     . $HEADER
312     # Append the original data
313     tail -n +$OLDSKIP "$archname".bak >> "$archname"
314     # Append the new data
315     cat "$tmpfile" >> "$archname"
316
317     chmod +x "$archname"
318     rm -f "$archname".bak
319     echo Self-extractible archive \"$archname\" successfully updated.
320 else
321     filesizes="$fsize"
322     CRCsum="$crcsum"
323     MD5sum="$md5sum"
324
325     # Generate the header
326     . $HEADER
327
328     # Append the compressed tar data after the stub
329     echo
330     cat "$tmpfile" >> "$archname"
331     chmod +x "$archname"
332     echo Self-extractible archive \"$archname\" successfully created.
333 fi
334 rm -f "$tmpfile"