makeself.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #!/bin/sh
  2. #
  3. # Makeself version 2.1.x
  4. # by Stephane Peter <megastep@megastep.org>
  5. #
  6. # $Id: makeself.sh,v 1.64 2008/01/04 23:52:14 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. # Added --noexec to prevent execution of embedded scripts.
  51. # Added --nomd5 and --nocrc to avoid creating checksums in archives.
  52. # Added command used to create the archive in --info output.
  53. # Run the embedded script through eval.
  54. # - 2.1.4 : Fixed --info output.
  55. # Generate random directory name when extracting files to . to avoid problems. (Jason Trent)
  56. # Better handling of errors with wrong permissions for the directory containing the files. (Jason Trent)
  57. # Avoid some race conditions (Ludwig Nussel)
  58. # Unset the $CDPATH variable to avoid problems if it is set. (Debian)
  59. # Better handling of dot files in the archive directory.
  60. # - 2.1.5 : Made the md5sum detection consistent with the header code.
  61. # Check for the presence of the archive directory
  62. # Added --encrypt for symmetric encryption through gpg (Eric Windisch)
  63. # Added support for the digest command on Solaris 10 for MD5 checksums
  64. # Check for available disk space before extracting to the target directory (Andreas Schweitzer)
  65. # Allow extraction to run asynchronously (patch by Peter Hatch)
  66. # Use file descriptors internally to avoid error messages (patch by Kay Tiong Khoo)
  67. #
  68. # (C) 1998-2008 by Stéphane Peter <megastep@megastep.org>
  69. #
  70. # This software is released under the terms of the GNU GPL version 2 and above
  71. # Please read the license at http://www.gnu.org/copyleft/gpl.html
  72. #
  73. MS_VERSION=2.1.5
  74. MS_COMMAND="$0"
  75. unset CDPATH
  76. for f in "${1+"$@"}"; do
  77. MS_COMMAND="$MS_COMMAND \\\\
  78. \\\"$f\\\""
  79. done
  80. # Procedures
  81. MS_Usage()
  82. {
  83. echo "Usage: $0 [params] archive_dir file_name label [startup_script] [args]"
  84. echo "params can be one or more of the following :"
  85. echo " --version | -v : Print out Makeself version number and exit"
  86. echo " --help | -h : Print out this help message"
  87. echo " --gzip : Compress using gzip (default if detected)"
  88. echo " --bzip2 : Compress using bzip2 instead of gzip"
  89. echo " --compress : Compress using the UNIX 'compress' command"
  90. echo " --nocomp : Do not compress the data"
  91. echo " --notemp : The archive will create archive_dir in the"
  92. echo " current directory and uncompress in ./archive_dir"
  93. echo " --copy : Upon extraction, the archive will first copy itself to"
  94. echo " a temporary directory"
  95. echo " --append : Append more files to an existing Makeself archive"
  96. echo " The label and startup scripts will then be ignored"
  97. echo " --current : Files will be extracted to the current directory."
  98. echo " Implies --notemp."
  99. echo " --nomd5 : Don't calculate an MD5 for archive"
  100. echo " --nocrc : Don't calculate a CRC for archive"
  101. echo " --header file : Specify location of the header script"
  102. echo " --follow : Follow the symlinks in the archive"
  103. echo " --nox11 : Disable automatic spawn of a xterm"
  104. echo " --nowait : Do not wait for user input after executing embedded"
  105. echo " program from an xterm"
  106. echo " --lsm file : LSM file describing the package"
  107. echo
  108. echo "Do not forget to give a fully qualified startup script name"
  109. echo "(i.e. with a ./ prefix if inside the archive)."
  110. exit 1
  111. }
  112. # Default settings
  113. if type gzip 2>&1 > /dev/null; then
  114. COMPRESS=gzip
  115. else
  116. COMPRESS=Unix
  117. fi
  118. KEEP=n
  119. CURRENT=n
  120. NOX11=n
  121. APPEND=n
  122. COPY=none
  123. TAR_ARGS=cvf
  124. HEADER=`dirname $0`/makeself-header.sh
  125. # LSM file stuff
  126. LSM_CMD="echo No LSM. >> \"\$archname\""
  127. while true
  128. do
  129. case "$1" in
  130. --version | -v)
  131. echo Makeself version $MS_VERSION
  132. exit 0
  133. ;;
  134. --bzip2)
  135. COMPRESS=bzip2
  136. shift
  137. ;;
  138. --gzip)
  139. COMPRESS=gzip
  140. shift
  141. ;;
  142. --compress)
  143. COMPRESS=Unix
  144. shift
  145. ;;
  146. --encrypt)
  147. COMPRESS=gpg
  148. shift
  149. ;;
  150. --nocomp)
  151. COMPRESS=none
  152. shift
  153. ;;
  154. --notemp)
  155. KEEP=y
  156. shift
  157. ;;
  158. --copy)
  159. COPY=copy
  160. shift
  161. ;;
  162. --current)
  163. CURRENT=y
  164. KEEP=y
  165. shift
  166. ;;
  167. --header)
  168. HEADER="$2"
  169. shift 2
  170. ;;
  171. --follow)
  172. TAR_ARGS=cvfh
  173. shift
  174. ;;
  175. --nox11)
  176. NOX11=y
  177. shift
  178. ;;
  179. --nowait)
  180. shift
  181. ;;
  182. --nomd5)
  183. NOMD5=y
  184. shift
  185. ;;
  186. --nocrc)
  187. NOCRC=y
  188. shift
  189. ;;
  190. --append)
  191. APPEND=y
  192. shift
  193. ;;
  194. --lsm)
  195. LSM_CMD="cat \"$2\" >> \"\$archname\""
  196. shift 2
  197. ;;
  198. -h | --help)
  199. MS_Usage
  200. ;;
  201. -*)
  202. echo Unrecognized flag : "$1"
  203. MS_Usage
  204. ;;
  205. *)
  206. break
  207. ;;
  208. esac
  209. done
  210. if test $# -lt 1; then
  211. MS_Usage
  212. else
  213. if test -d "$1"; then
  214. archdir="$1"
  215. else
  216. echo "Directory $1 does not exist."
  217. exit 1
  218. fi
  219. fi
  220. archname="$2"
  221. if test "$APPEND" = y; then
  222. if test $# -lt 2; then
  223. MS_Usage
  224. fi
  225. # Gather the info from the original archive
  226. OLDENV=`sh "$archname" --dumpconf`
  227. if test $? -ne 0; then
  228. echo "Unable to update archive: $archname" >&2
  229. exit 1
  230. else
  231. eval "$OLDENV"
  232. fi
  233. else
  234. if test "$KEEP" = n -a $# = 3; then
  235. echo "ERROR: Making a temporary archive with no embedded command does not make sense!" >&2
  236. echo
  237. MS_Usage
  238. fi
  239. # We don't really want to create an absolute directory...
  240. if test "$CURRENT" = y; then
  241. archdirname="."
  242. else
  243. archdirname=`basename "$1"`
  244. fi
  245. if test $# -lt 3; then
  246. MS_Usage
  247. fi
  248. LABEL="$3"
  249. SCRIPT="$4"
  250. test x$SCRIPT = x || shift 1
  251. shift 3
  252. SCRIPTARGS="$*"
  253. fi
  254. if test "$KEEP" = n -a "$CURRENT" = y; then
  255. echo "ERROR: It is A VERY DANGEROUS IDEA to try to combine --notemp and --current." >&2
  256. exit 1
  257. fi
  258. case $COMPRESS in
  259. gzip)
  260. GZIP_CMD="gzip -c9"
  261. GUNZIP_CMD="gzip -cd"
  262. ;;
  263. bzip2)
  264. GZIP_CMD="bzip2 -9"
  265. GUNZIP_CMD="bzip2 -d"
  266. ;;
  267. gpg)
  268. GZIP_CMD="gpg -ac -z9"
  269. GUNZIP_CMD="gpg -d"
  270. ;;
  271. Unix)
  272. GZIP_CMD="compress -cf"
  273. GUNZIP_CMD="exec 2>&-; uncompress -c || test \\\$? -eq 2 || gzip -cd"
  274. ;;
  275. none)
  276. GZIP_CMD="cat"
  277. GUNZIP_CMD="cat"
  278. ;;
  279. esac
  280. tmpfile="${TMPDIR:=/tmp}/mkself$$"
  281. if test -f $HEADER; then
  282. oldarchname="$archname"
  283. archname="$tmpfile"
  284. # Generate a fake header to count its lines
  285. SKIP=0
  286. . $HEADER
  287. SKIP=`cat "$tmpfile" |wc -l`
  288. # Get rid of any spaces
  289. SKIP=`expr $SKIP`
  290. rm -f "$tmpfile"
  291. echo Header is $SKIP lines long >&2
  292. archname="$oldarchname"
  293. else
  294. echo "Unable to open header file: $HEADER" >&2
  295. exit 1
  296. fi
  297. echo
  298. if test "$APPEND" = n; then
  299. if test -f "$archname"; then
  300. echo "WARNING: Overwriting existing file: $archname" >&2
  301. fi
  302. fi
  303. USIZE=`du -ks $archdir | cut -f1`
  304. DATE=`LC_ALL=C date`
  305. if test "." = "$archdirname"; then
  306. if test "$KEEP" = n; then
  307. archdirname="makeself-$$-`date +%Y%m%d%H%M%S`"
  308. fi
  309. fi
  310. test -d "$archdir" || { echo "Error: $archdir does not exist."; rm -f "$tmpfile"; exit 1; }
  311. echo About to compress $USIZE KB of data...
  312. echo Adding files to archive named \"$archname\"...
  313. exec 3<> "$tmpfile"
  314. (cd "$archdir" && ( tar $TAR_ARGS - . | eval "$GZIP_CMD" >&3 ) ) || { echo Aborting: Archive directory not found or temporary file: "$tmpfile" could not be created.; exec 3>&-; rm -f "$tmpfile"; exit 1; }
  315. exec 3>&- # try to close the archive
  316. fsize=`cat "$tmpfile" | wc -c | tr -d " "`
  317. # Compute the checksums
  318. md5sum=00000000000000000000000000000000
  319. crcsum=0000000000
  320. if test "$NOCRC" = y; then
  321. echo "skipping crc at user request"
  322. else
  323. crcsum=`cat "$tmpfile" | CMD_ENV=xpg4 cksum | sed -e 's/ /Z/' -e 's/ /Z/' | cut -dZ -f1`
  324. echo "CRC: $crcsum"
  325. fi
  326. if test "$NOMD5" = y; then
  327. echo "skipping md5sum at user request"
  328. else
  329. # Try to locate a MD5 binary
  330. OLD_PATH=$PATH
  331. PATH=${GUESS_MD5_PATH:-"$OLD_PATH:/bin:/usr/bin:/sbin:/usr/local/ssl/bin:/usr/local/bin:/opt/openssl/bin"}
  332. MD5_ARG=""
  333. MD5_PATH=`exec <&- 2>&-; which md5sum || type md5sum`
  334. test -x $MD5_PATH || MD5_PATH=`exec <&- 2>&-; which md5 || type md5`
  335. test -x $MD5_PATH || MD5_PATH=`exec <&- 2>&-; which digest || type digest`
  336. PATH=$OLD_PATH
  337. if test `basename $MD5_PATH` = digest; then
  338. MD5_ARG="-a md5"
  339. fi
  340. if test -x "$MD5_PATH"; then
  341. md5sum=`cat "$tmpfile" | eval "$MD5_PATH $MD5_ARG" | cut -b-32`;
  342. echo "MD5: $md5sum"
  343. else
  344. echo "MD5: none, MD5 command not found"
  345. fi
  346. fi
  347. if test "$APPEND" = y; then
  348. mv "$archname" "$archname".bak || exit
  349. # Prepare entry for new archive
  350. filesizes="$filesizes $fsize"
  351. CRCsum="$CRCsum $crcsum"
  352. MD5sum="$MD5sum $md5sum"
  353. USIZE=`expr $USIZE + $OLDUSIZE`
  354. # Generate the header
  355. . $HEADER
  356. # Append the original data
  357. tail -n +$OLDSKIP "$archname".bak >> "$archname"
  358. # Append the new data
  359. cat "$tmpfile" >> "$archname"
  360. chmod +x "$archname"
  361. rm -f "$archname".bak
  362. echo Self-extractible archive \"$archname\" successfully updated.
  363. else
  364. filesizes="$fsize"
  365. CRCsum="$crcsum"
  366. MD5sum="$md5sum"
  367. # Generate the header
  368. . $HEADER
  369. # Append the compressed tar data after the stub
  370. echo
  371. cat "$tmpfile" >> "$archname"
  372. chmod +x "$archname"
  373. echo Self-extractible archive \"$archname\" successfully created.
  374. fi
  375. rm -f "$tmpfile"