|
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 |
|
22 echo "$part/$1" |
|
23 return 0 |
|
24 fi |
|
25 done |
|
26 } |
|
27 |
|
28 my_which() |
|
29 { |
|
30 local out |
|
31 local r |
|
32 out=`my_which_real "$1"` |
|
33 r=$? |
|
34 if [ -z "$out" -o $r = 1 ]; then |
|
35 echo "ERROR: $1 command not found on system" 1>&2 |
|
36 exit 1 |
|
37 fi |
|
38 echo $out |
|
39 return $r |
|
40 } |
|
41 |
|
42 # Show the extraction progress spinner. |
|
43 # Spins the propeller one time for each line on stdin |
|
44 progressbit() |
|
45 { |
|
46 local i |
|
47 local pbar |
|
48 local line |
|
49 i=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 |
|
58 # Create a temp directory. Very bad in terms of randomness source, but hey, I'm a security major. Can't help it. |
|
59 mktempdir() |
|
60 { |
|
61 local tempdir |
|
62 tempdir=/tmp/ext`$dd if=/dev/urandom bs=128 count=1 2>/dev/null | $md5sum - | cut -c 1-6` |
|
63 echo $tempdir |
|
64 return 0 |
|
65 } |
|
66 |
|
67 # Outputs the raw compressed tarball data on stdout. |
|
68 getdata() |
|
69 { |
|
70 offset=`$cat $0 | $grep -an '^##DATA' | cut -d ':' -f 1 || exit 1` |
|
71 offset=$((offset + 1)) |
|
72 $cat $0 | $tail -n+$offset |
|
73 } |
|
74 |
|
75 # Go through each required command, and make sure it's on the system. If not, fail. |
|
76 for cmd in bzip2 tar grep tail cat wc dd md5sum; do |
|
77 my_which $cmd>/dev/null || exit 1 |
|
78 eval $cmd=`my_which $cmd` |
|
79 if test x$? = x1; then |
|
80 exit 1 |
|
81 fi |
|
82 done |
|
83 |
|
84 tempdir="" |
|
85 extractonly=0 |
|
86 while test -n "$1"; do |
|
87 case "$1" in |
|
88 -x) |
|
89 extractonly=1 |
|
90 if test -n "$2"; then |
|
91 tempdir=$2 |
|
92 else |
|
93 tempdir=`echo $0 | sed -re 's/\.[a-z0-9_-]{1,5}$//'` |
|
94 fi |
|
95 ;; |
|
96 esac |
|
97 shift |
|
98 done |
|
99 |
|
100 # Make sure we have a working temp directory |
|
101 test -n "$tempdir" || tempdir=`mktempdir` |
|
102 |
|
103 if [ -d "$tempdir" ]; then |
|
104 if [ $extractonly = 1 ]; then |
|
105 i=0 |
|
106 basetemp="$tempdir" |
|
107 while test -d "$tempdir"; do |
|
108 tempdir="${basetemp}${i}" |
|
109 ((i++)) |
|
110 done |
|
111 else |
|
112 while test -d "$tempdir"; do |
|
113 tempdir=`mktempdir` |
|
114 done |
|
115 fi |
|
116 fi |
|
117 |
|
118 mkdir "$tempdir" || exit 1 |
|
119 |
|
120 # Finally, extract the data. |
|
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 |
|
129 # clear out the extraction progress |
|
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 |
|
136 # added in rev. 2, interrupts should cause an error exit |
|
137 exit 1 |
|
138 } |
|
139 trap handle_interrupt 0 |
|
140 |
|
141 if test -x $tempdir/autorun.sh && test $extractonly != 1; then |
|
142 # Run the autorun script |
|
143 $tempdir/autorun.sh |
|
144 ret=$? |
|
145 rm -rf $tempdir |
|
146 exit $ret |
|
147 else |
|
148 # Just let the user know where the files are and die. |
|
149 echo "Contents extracted to $tempdir." |
|
150 fi |
|
151 |
|
152 # free our trap on signals |
|
153 trap "exit 0;" 0 |
|
154 |
|
155 # done! |
|
156 exit 0 |
|
157 |
|
158 # don't touch this marker! Everything after this is expected to be binary data. |
|
159 |
|
160 ##DATA |