author | Dan |
Tue, 23 Jun 2009 09:51:01 -0400 | |
changeset 1 | dbce0b16b0c1 |
parent 0 | self-extract.sh@8e044e762a6c |
child 4 | c6c431cf0a89 |
permissions | -rw-r--r-- |
0 | 1 |
#!/bin/bash |
2 |
||
3 |
if test -z "$BASH"; then |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
4 |
cmd="$0" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
5 |
if test "`basename $0`" = "$0"; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
6 |
cmd="./$0" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
7 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
8 |
echo "This script is designed to be run using GNU bash." |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
9 |
echo "Try: chmod +x $0 && $cmd" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
10 |
exit 1 |
0 | 11 |
fi |
12 |
||
13 |
my_which_real() |
|
14 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
15 |
test -n "$1" || return 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
16 |
local i |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
17 |
local part |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
18 |
for (( i=1; ; i++ )); do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
19 |
part=`echo "$PATH" | cut -d ':' -f $i` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
20 |
test -n "$part" || return 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
21 |
if test -x "$part/$1"; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
22 |
echo "$part/$1" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
23 |
return 0 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
24 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
25 |
done |
0 | 26 |
} |
27 |
||
28 |
my_which() |
|
29 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
30 |
local out |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
31 |
local r |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
32 |
out=`my_which_real "$1"` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
33 |
r=$? |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
34 |
if [ -z "$out" -o $r = 1 ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
35 |
echo "ERROR: $1 command not found on system" 1>&2 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
36 |
exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
37 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
38 |
echo $out |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
39 |
return $r |
0 | 40 |
} |
41 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
42 |
# Show the extraction progress spinner. |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
43 |
# Spins the propeller one time for each line on stdin |
0 | 44 |
progressbit() |
45 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
46 |
local i |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
47 |
local pbar |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
48 |
local line |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
49 |
i=0 |
0 | 50 |
pbar="-\\|/" |
51 |
while read line; do |
|
52 |
((i++)) |
|
53 |
j=$(($i % ${#pbar})) |
|
54 |
echo -ne "\r${pbar:$j:1} Extracting..." |
|
55 |
done |
|
56 |
} |
|
57 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
58 |
# Create a temp directory. Very bad in terms of randomness source, but hey, I'm a security major. Can't help it. |
0 | 59 |
mktempdir() |
60 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
61 |
local tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
62 |
tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
63 |
echo $tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
64 |
return 0 |
0 | 65 |
} |
66 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
67 |
# Outputs the raw compressed tarball data on stdout. |
0 | 68 |
getdata() |
69 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
70 |
offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
71 |
offset=$((offset + 1)) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
72 |
$cat $0 | $tail -n+$offset |
0 | 73 |
} |
74 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
75 |
# Go through each required command, and make sure it's on the system. If not, fail. |
0 | 76 |
for cmd in bzip2 tar grep tail cat wc dd md5sum; do |
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
77 |
my_which $cmd>/dev/null || exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
78 |
eval $cmd=`my_which $cmd` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
79 |
if test x$? = x1; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
80 |
exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
81 |
fi |
0 | 82 |
done |
83 |
||
84 |
tempdir="" |
|
85 |
extractonly=0 |
|
86 |
while test -n "$1"; do |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
87 |
case "$1" in |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
88 |
-x) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
89 |
extractonly=1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
90 |
if test -n "$2"; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
91 |
tempdir=$2 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
92 |
else |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
93 |
tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
94 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
95 |
;; |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
96 |
esac |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
97 |
shift |
0 | 98 |
done |
99 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
100 |
# Make sure we have a working temp directory |
0 | 101 |
test -n "$tempdir" || tempdir=`mktempdir` |
102 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
103 |
if [ -d "$tempdir" ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
104 |
if [ $extractonly = 1 ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
105 |
i=0 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
106 |
basetemp="$tempdir" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
107 |
while test -d "$tempdir"; do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
108 |
tempdir="${basetemp}${i}" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
109 |
((i++)) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
110 |
done |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
111 |
else |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
112 |
while test -d "$tempdir"; do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
113 |
tempdir=`mktempdir` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
114 |
done |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
115 |
fi |
0 | 116 |
fi |
117 |
||
118 |
mkdir "$tempdir" || exit 1 |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
119 |
|
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
120 |
# Finally, extract the data. |
0 | 121 |
echo -ne "\e[?25l- Extracting..." |
122 |
getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \ |
|
123 |
|| ( |
|
124 |
rm -rf $tempdir |
|
125 |
echo -ne "\e[?25h" |
|
126 |
exit 1 |
|
127 |
) |
|
128 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
129 |
# clear out the extraction progress |
0 | 130 |
echo -ne "\r\e[?25h" |
131 |
||
132 |
# trap exits so we can clean up if the script is interrupted |
|
133 |
handle_interrupt() |
|
134 |
{ |
|
135 |
rm -rf $tempdir |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
136 |
# added in rev. 2, interrupts should cause an error exit |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
137 |
exit 1 |
0 | 138 |
} |
139 |
trap handle_interrupt 0 |
|
140 |
||
141 |
if test -x $tempdir/autorun.sh && test $extractonly != 1; then |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
142 |
# Run the autorun script |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
143 |
$tempdir/autorun.sh |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
144 |
ret=$? |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
145 |
rm -rf $tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
146 |
exit $ret |
0 | 147 |
else |
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
148 |
# Just let the user know where the files are and die. |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
149 |
echo "Contents extracted to $tempdir." |
0 | 150 |
fi |
151 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
152 |
# free our trap on signals |
0 | 153 |
trap "exit 0;" 0 |
154 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
155 |
# done! |
0 | 156 |
exit 0 |
157 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
158 |
# don't touch this marker! Everything after this is expected to be binary data. |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
159 |
|
0 | 160 |
##DATA |