author | Dan Fuhry <dan@enanocms.org> |
Mon, 05 Jul 2010 23:29:07 -0400 | |
changeset 4 | c6c431cf0a89 |
parent 1 | dbce0b16b0c1 |
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 |
||
4 | 13 |
# OS X sed (at least leopard and up) seem to support -E |
14 |
sedflags=Ee |
|
15 |
if ( sed --version 2>&1 | grep 'GNU sed' 2>&1 ) > /dev/null ; then |
|
16 |
sedflags=re |
|
17 |
fi |
|
18 |
||
0 | 19 |
my_which_real() |
20 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
21 |
test -n "$1" || return 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
22 |
local i |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
23 |
local part |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
24 |
for (( i=1; ; i++ )); do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
25 |
part=`echo "$PATH" | cut -d ':' -f $i` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
26 |
test -n "$part" || return 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
27 |
if test -x "$part/$1"; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
28 |
echo "$part/$1" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
29 |
return 0 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
30 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
31 |
done |
0 | 32 |
} |
33 |
||
34 |
my_which() |
|
35 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
36 |
local out |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
37 |
local r |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
38 |
out=`my_which_real "$1"` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
39 |
r=$? |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
40 |
if [ -z "$out" -o $r = 1 ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
41 |
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
|
42 |
exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
43 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
44 |
echo $out |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
45 |
return $r |
0 | 46 |
} |
47 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
48 |
# Show the extraction progress spinner. |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
49 |
# Spins the propeller one time for each line on stdin |
0 | 50 |
progressbit() |
51 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
52 |
local i |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
53 |
local pbar |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
54 |
local line |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
55 |
i=0 |
0 | 56 |
pbar="-\\|/" |
57 |
while read line; do |
|
58 |
((i++)) |
|
59 |
j=$(($i % ${#pbar})) |
|
60 |
echo -ne "\r${pbar:$j:1} Extracting..." |
|
61 |
done |
|
62 |
} |
|
63 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
64 |
# Create a temp directory. Very bad in terms of randomness source, but hey, I'm a security major. Can't help it. |
0 | 65 |
mktempdir() |
66 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
67 |
local tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
68 |
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
|
69 |
echo $tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
70 |
return 0 |
0 | 71 |
} |
72 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
73 |
# Outputs the raw compressed tarball data on stdout. |
0 | 74 |
getdata() |
75 |
{ |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
76 |
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
|
77 |
offset=$((offset + 1)) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
78 |
$cat $0 | $tail -n+$offset |
0 | 79 |
} |
80 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
81 |
# Go through each required command, and make sure it's on the system. If not, fail. |
4 | 82 |
for cmd in bzip2 tar grep tail cat wc dd; do |
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
83 |
my_which $cmd>/dev/null || exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
84 |
eval $cmd=`my_which $cmd` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
85 |
if test x$? = x1; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
86 |
exit 1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
87 |
fi |
0 | 88 |
done |
89 |
||
4 | 90 |
# special case for md5sum |
91 |
if my_which_real md5sum 2>&1 > /dev/null; then |
|
92 |
md5sum=`my_which_real md5sum` |
|
93 |
elif my_which_real openssl 2>&1 > /dev/null; then |
|
94 |
md5sum=md5sum |
|
95 |
md5sum() |
|
96 |
{ |
|
97 |
local f=${1:--} |
|
98 |
cat $f | openssl dgst -md5 | sed -$sedflags 's/^\(stdin\)= //' |
|
99 |
} |
|
100 |
else |
|
101 |
echo "Could not find md5sum or openssl" |
|
102 |
exit 1 |
|
103 |
fi |
|
104 |
||
0 | 105 |
tempdir="" |
106 |
extractonly=0 |
|
107 |
while test -n "$1"; do |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
108 |
case "$1" in |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
109 |
-x) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
110 |
extractonly=1 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
111 |
if test -n "$2"; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
112 |
tempdir=$2 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
113 |
else |
4 | 114 |
tempdir=`echo $0 | sed -$sedflags 's/\.[a-z0-9_-]{1,5}$//'` |
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
115 |
fi |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
116 |
;; |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
117 |
esac |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
118 |
shift |
0 | 119 |
done |
120 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
121 |
# Make sure we have a working temp directory |
0 | 122 |
test -n "$tempdir" || tempdir=`mktempdir` |
123 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
124 |
if [ -d "$tempdir" ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
125 |
if [ $extractonly = 1 ]; then |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
126 |
i=0 |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
127 |
basetemp="$tempdir" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
128 |
while test -d "$tempdir"; do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
129 |
tempdir="${basetemp}${i}" |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
130 |
((i++)) |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
131 |
done |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
132 |
else |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
133 |
while test -d "$tempdir"; do |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
134 |
tempdir=`mktempdir` |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
135 |
done |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
136 |
fi |
0 | 137 |
fi |
138 |
||
139 |
mkdir "$tempdir" || exit 1 |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
140 |
|
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
141 |
# Finally, extract the data. |
0 | 142 |
echo -ne "\e[?25l- Extracting..." |
143 |
getdata | $bzip2 -dc | $tar xvCf $tempdir - | progressbit \ |
|
144 |
|| ( |
|
145 |
rm -rf $tempdir |
|
146 |
echo -ne "\e[?25h" |
|
147 |
exit 1 |
|
148 |
) |
|
149 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
150 |
# clear out the extraction progress |
0 | 151 |
echo -ne "\r\e[?25h" |
152 |
||
153 |
# trap exits so we can clean up if the script is interrupted |
|
154 |
handle_interrupt() |
|
155 |
{ |
|
156 |
rm -rf $tempdir |
|
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
157 |
# 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
|
158 |
exit 1 |
0 | 159 |
} |
160 |
trap handle_interrupt 0 |
|
161 |
||
162 |
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
|
163 |
# Run the autorun script |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
164 |
$tempdir/autorun.sh |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
165 |
ret=$? |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
166 |
rm -rf $tempdir |
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
167 |
exit $ret |
0 | 168 |
else |
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
169 |
# 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
|
170 |
echo "Contents extracted to $tempdir." |
0 | 171 |
fi |
172 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
173 |
# free our trap on signals |
0 | 174 |
trap "exit 0;" 0 |
175 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
176 |
# done! |
0 | 177 |
exit 0 |
178 |
||
1
dbce0b16b0c1
Added shellscript compactor to reduce size of self-extract script
Dan
parents:
0
diff
changeset
|
179 |
# 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
|
180 |
|
0 | 181 |
##DATA |