self-extract.sh
changeset 1 dbce0b16b0c1
parent 0 8e044e762a6c
child 2 7dddb5fb1595
equal deleted inserted replaced
0:8e044e762a6c 1:dbce0b16b0c1
     1 #!/bin/bash
       
     2 
       
     3 if test -z "$BASH"; then
       
     4 	cmd="$0"
       
     5 	if test "`basename $0`" = "$0"; then
       
     6 		cmd="./$0"
       
     7 	fi
       
     8 	echo "This script is designed to be run using GNU bash."
       
     9 	echo "Try: chmod +x $0 && $cmd"
       
    10 	exit 1
       
    11 fi
       
    12 
       
    13 my_which_real()
       
    14 {
       
    15 	test -n "$1" || return 1
       
    16 	local i
       
    17 	local part
       
    18 	for (( i=1; ; i++ )); do
       
    19 		part=`echo "$PATH" | cut -d ':' -f $i`
       
    20 		test -n "$part" || return 1
       
    21 		if test -x "$part/$1"; then echo "$part/$1"; return 0; fi
       
    22 	done
       
    23 }
       
    24 
       
    25 my_which()
       
    26 {
       
    27 	local out
       
    28 	local r
       
    29 	out=`my_which_real "$1"`
       
    30 	r=$?
       
    31 	if test -z "$out" -o $r = 1 ; then echo "ERROR: $1 command not found on system" 1>&2;exit 1;fi
       
    32 	echo $out
       
    33 	return $r
       
    34 }
       
    35 
       
    36 progressbit()
       
    37 {
       
    38 	local i
       
    39 	local pbar
       
    40 	local line
       
    41 	i=0
       
    42   pbar="-\\|/"
       
    43   while read line; do
       
    44     ((i++))
       
    45     j=$(($i % ${#pbar}))
       
    46     echo -ne "\r${pbar:$j:1} Extracting..."
       
    47   done
       
    48 }
       
    49 
       
    50 mktempdir()
       
    51 {
       
    52 	local tempdir
       
    53 	tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6`
       
    54 	echo $tempdir
       
    55 	return 0
       
    56 }
       
    57 
       
    58 getdata()
       
    59 {
       
    60 	offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1`
       
    61 	offset=$((offset + 1))
       
    62 	$cat $0 | $tail -n+$offset
       
    63 }
       
    64 
       
    65 for cmd in bzip2 tar grep tail cat wc dd md5sum; do
       
    66 	my_which $cmd>/dev/null || exit 1
       
    67 	eval $cmd=`my_which $cmd`
       
    68 	if test x$? = x1; then
       
    69 		exit 1
       
    70 	fi
       
    71 done
       
    72 
       
    73 tempdir=""
       
    74 extractonly=0
       
    75 while test -n "$1"; do
       
    76 	case "$1" in
       
    77 	-x)
       
    78 		extractonly=1
       
    79 		if test -n "$2"; then
       
    80       tempdir=$2
       
    81     else
       
    82       tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'`
       
    83     fi
       
    84 		;;
       
    85 	esac
       
    86 	shift
       
    87 done
       
    88 
       
    89 test -n "$tempdir" || tempdir=`mktempdir`
       
    90 
       
    91 if test -d "$tempdir"; then
       
    92 if test $extractonly = 1; then
       
    93 	i=0
       
    94   basetemp="$tempdir"
       
    95   while test -d "$tempdir"; do
       
    96     tempdir="${basetemp}${i}"
       
    97     ((i++))
       
    98   done
       
    99 else
       
   100 	while test -d "$tempdir"; do
       
   101     tempdir=`mktempdir`
       
   102   done
       
   103 fi
       
   104 fi
       
   105 
       
   106 mkdir "$tempdir" || exit 1
       
   107 echo -ne "\e[?25l- Extracting..."
       
   108 getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \
       
   109   || (
       
   110       rm -rf $tempdir
       
   111       echo -ne "\e[?25h"
       
   112       exit 1
       
   113      )
       
   114 
       
   115 echo -ne "\r\e[?25h"
       
   116 
       
   117 # trap exits so we can clean up if the script is interrupted
       
   118 handle_interrupt()
       
   119 {
       
   120   rm -rf $tempdir
       
   121 }
       
   122 trap handle_interrupt 0
       
   123 
       
   124 if test -x $tempdir/autorun.sh && test $extractonly != 1; then
       
   125 	$tempdir/autorun.sh
       
   126 	ret=$?
       
   127 	rm -rf $tempdir
       
   128 	exit $ret
       
   129 else
       
   130 	echo "Contents extracted to $tempdir."
       
   131 fi
       
   132 
       
   133 trap "exit 0;" 0
       
   134 
       
   135 exit 0
       
   136 
       
   137 ##DATA