/
undeb.sh
executable file
·196 lines (168 loc) · 4.61 KB
/
undeb.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/sh
# Tell zsh we expect to be treated like an sh script
# zsh really should take the hint from the shebang line
if which emulate 1>&2; then
emulate sh
fi
# Ensure we have neccesary utils (ar, tar)
AR=`which ar`
if [ -z "$AR" ]; then
echo "You must have a POSIXly-compliant version of ar to use $0" 1>&2
exit 1
fi
TAR=`which tar`
if [ -z "$TAR" ]; then
echo "You must have a POSIXly-compliant version of tar to use $0" 1>&2
exit 1
fi
# Verify that the user has invoked the script correctly
if [ -z "$1" ]; then
echo "You must specify a package to install." 1>&2
exit 1
fi
# Get the name of a safe temporary directory
if [ ! -z "$TMPDIR" ]; then
temp=$TMPDIR
elif [ ! -z "$TEMP" ]; then
temp="$TEMP"
elif [ ! -z "$TMP" ]; then
temp="$TMP"
elif [ -d "/tmp" ]; then
temp="/tmp"
else
#fallback
temp="."
fi
# Get a random directory name and try to create it
if which mktemp 1>&2; then # Try to use mktemp
temp="`mktemp -d "$temp/undeb-$$-XXXXXX"`"
else
temp="$temp/undeb-$$-$RANDOM-$RANDOM" #$RANDOM is non-standard and likely blank on your shell
mkdir -p "$temp"
if [ $? != 0 ]; then
echo "ERROR: $temp is not writable (it may already exist)." 1>&2
exit 1
fi
fi
# Get the name of where we want the package file to live
deb="$temp/`basename $1`"
# Copy the package to its new home
cp "$1" "$deb"
if [ $? != 0 ]; then
echo "ERROR: $deb could not be created." 1>&2
exit 1
fi
# Unpack the package with ar
cd "$temp"
"$AR" xv "$deb"
cd - # Pop back to where we were, just in case we forget where we are later
# Check that the package unpacked and is a package we can understand
DEB_VERSION=`cat "$temp/debian-binary"`
if [ -z "$DEB_VERSION" ]; then
echo "ERROR: The package does not appear to be valid." 1>&2
exit 1
elif [ "$DEB_VERSION" != "2.0" ]; then
echo "WARN: debian-binary says $DEB_VERSION, expected 2.0 " 1>&2
fi
# XXX: Should check dependencies? Should check md5sums. Should check _gpgorigin.
if [ -f "$temp/control.tar" ]; then
echo "Not compressed, no decompression necessary."
elif [ -f "$temp/control.tar.gz" ]; then
GZIP="`which gzip`"
if [ -z "$GZIP" ]; then
echo "You must have a version of gzip to unpack $1" 1>&2
exit 1
fi
"$GZIP" -d "$temp/control.tar.gz"
elif [ -f "$temp/control.tar.bz2" ]; then
BZIP2="`which bzip2`"
if [ -z "$BZIP2" ]; then
echo "You must have a version of bzip2 to unpack $1" 1>&2
exit 1
fi
"$BZIP2" -d "$temp/control.tar.bz2"
elif [ -f "$temp/control.tar.bzip2" ]; then
BZIP2="`which bzip2`"
if [ -z "$BZIP2" ]; then
echo "You must have a version of bzip2 to unpack $1" 1>&2
exit 1
fi
"$BZIP2" -d "$temp/control.tar.bzip2"
else
echo "ERROR: No control.tar.* found." 1>&2
exit 1
fi
cd "$temp"
tar xvf "$temp/control.tar"
cd -
if [ -f "$temp/preinst" ]; then
sh "$temp/preinst"
fi
# Determine what kind of data ball is being used and decompress it
if [ -f "$temp/data.tar" ]; then
echo "Not compressed, no decompression necessary."
elif [ -f "$temp/data.tar.gz" ]; then
GZIP="`which gzip`"
if [ -z "$GZIP" ]; then
echo "You must have a version of gzip to unpack $1" 1>&2
exit 1
fi
"$GZIP" -d "$temp/data.tar.gz"
elif [ -f "$temp/data.tar.bz2" ]; then
BZIP2="`which bzip2`"
if [ -z "$BZIP2" ]; then
echo "You must have a version of bzip2 to unpack $1" 1>&2
exit 1
fi
"$BZIP2" -d "$temp/data.tar.bz2"
elif [ -f "$temp/data.tar.bzip2" ]; then
BZIP2="`which bzip2`"
if [ -z "$BZIP2" ]; then
echo "You must have a version of bzip2 to unpack $1" 1>&2
exit 1
fi
"$BZIP2" -d "$temp/data.tar.bzip2"
else
echo "ERROR: No data.tar.* found." 1>&2
exit 1
fi
# Create a dir out
mkdir -p "$temp/out"
# Copy the data tar into it
cp "$temp/data.tar" "$temp/out"
# Unpack using tar
cd "$temp/out"
tar xvf data.tar
rm -f data.tar
cd - # Pop back to where we were, just in case we forget where we are later
# Copy the data to the dir where it will be installed
if [ -z "$PREFIX" ]; then
PREFIX="/"
fi
if ! mkdir -p "$PREFIX"; then
echo "ERROR: files not installed (you may not have sufficient permissions)." 1>&2
exit 1
fi
if [ ! -w "$PREFIX" ]; then
echo "ERROR: files not installed (you may not have sufficient permissions)." 1>&2
exit 1
fi
if [ ! -z "$LOG" ]; then
cp -Rfpv "$temp/out/"* "$PREFIX" | cut -s -d'`' -f3 | cut -s -d\' -f1 >> "$LOG"
if [ $? != 0 ]; then
echo "ERROR: files not installed (you may not have sufficient permissions)." 1>&2
exit 1
fi
else
cp -Rfp "$temp/out/"* "$PREFIX"
if [ $? != 0 ]; then
echo "ERROR: files not installed (you may not have sufficient permissions)." 1>&2
exit 1
fi
fi
if [ -f "$temp/postinst" ]; then
sh "$temp/postinst"
fi
# Clean up our temporary directory
rm -rf "$temp"
echo "Package $1 installed to $PREFIX successfully."