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