File: common.sh

package info (click to toggle)
apt-zip 0.13.2
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 80 kB
  • ctags: 48
  • sloc: sh: 474; makefile: 67; awk: 10
file content (323 lines) | stat: -rw-r--r-- 6,356 bytes parent folder | download
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# library file sourced by apt-zip-*

APTZIPVERSION=0.13.2
export APTZIPVERSION

CONFFILE=/etc/apt/apt-zip.conf
METHODSDIR=/usr/share/apt-zip
SKIPMOUNT=no
APTGETEXTRAOPTS=""

KNOWNAPTGETACTIONS=" dselect-upgrade upgrade dist-upgrade "
KNOWNOPTIONS="TAR GNUTAR SOLARISTAR RESTART"

DEFAULT_TAR=tar
DEFAULT_GNUTAR=tar
DEFAULT_SOLARISTAR=tar

APTZIPTARFILE=debs-$(uname -n).tar
APTZIPINSFILE=apt-zip.options
export APTZIPINSFILE
export APTZIPTARFILE

error()
{
    echo >&2 "$(basename $0): $*"
    exit 1
}

version_exit()
{
    echo "$(basename $0) version ${APTZIPVERSION}"
    exit 0
}

usage()
{
cat <<EOF
Usage:
	$(basename $0) [options]

where options can be:
 -h, --help   	     Print this help message
 -v, --version       Print version and exit
 -M, --method=METH   Fetch method METH used on download host
 -m, --medium=DIR    Removable medium at mountpoint DIR for the transfers
 -o, --options=LIST  List of options
 -a, --aptgetaction=ACTION
 		     Action ACTION done by apt-get
 -p, --packages=LIST
  	  	     List of extra packages to install
 -A, --accept=LIST   List of accepted protocols for download (http,ftp,..)
 -R, --reject=LIST   List of rejected protocols for download (file,cdrom,..)
 -f, --fix-broken    Call apt-get with this flag
 -s, --skip-mount    Do not mount a device on the dir specified by --medium 
EOF
}

usage_exit()
{
    usage
    exit 0
}

syntax_error()
{
    echo 2>&1 "$(basename $0): $*"
    usage
    exit 1
}

check_method()
{
    FILTER=${SHAREDIR}/methods/$METHOD
    if [ ! -x $FILTER ]
    then
	error "No $METHOD method installed."
    fi
}

check_medium()
{
    [ -d $MEDIUM ] || error "$MEDIUM does not exist (no mountpoint)"
}

az_filter()
{
    [ -n "$GREP" ] && GREP=${GREP//^/^\'}
    if [ -n "$ACCEPT" ] ; then 
	ACCEPT=${ACCEPT#, }
	ACCEPT=^$(echo "$ACCEPT" | sed -e "s/[, ]\+/\\\|^/g")
	GREP="'$ACCEPT"
    elif [ -n "$REJECT" ] ; then
	REJECT=${REJECT#, }
	REJECT=^$(echo "$REJECT" | sed -e "s/[, ]\+/\\\|^/g")
	GREP="-v '$REJECT"
    fi
}
check_packages()
{
    PACKAGES=$(echo "$PACKAGES" | sed -e "s/[, ]\+/ /g")
}
check_aptgetaction()
{
    [ -n "$APTGETACTION" ] && 
	echo "$KNOWNAPTGETACTIONS" | grep " $APTGETACTION " &> /dev/null || 
	    error "Unknown apt-get action $APTGETACTION"
}
az_loadconf()
{
    [ -r $CONFFILE ] && . $CONFFILE
    METHOD=${METHOD-wget}
    MEDIUM=${MEDIUM-/ZIP}
    GREP=${GREP-^http\\\|^ftp}
}

az_process_commandline ()
{
    while [ $# -gt 0 ]
    do
	case $1 in
	--method=*)
	    METHOD=${1#--method=}
	    shift
	    ;;
	--method|-M)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    METHOD=$2
	    shift
	    shift
	    ;;
	--medium=*)
	    MEDIUM=${1#--medium=}
	    shift
	    ;;
	--medium|-m)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    MEDIUM=$2
	    shift
	    shift
	    ;;
	--options=*)
	    OPTIONS="$OPTIONS, ${1#--options=}"
	    shift
	    ;;
	--options|-o)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    OPTIONS="$OPTIONS, $2"
	    shift
	    shift
	    ;;
	--aptgetaction=*)
	    APTGETACTION=${1#--aptgetaction=}
	    shift
	    ;;
	--aptgetaction|-a)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    APTGETACTION=$2
	    shift
	    shift
	    ;;
	--packages=*)
	    PACKAGES="$PACKAGES, ${1#--packages=}"
	    shift
	    ;;
	--packages|-p)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    PACKAGES="$PACKAGES, $2"
	    shift
	    shift
	    ;;
	--accept=*)
	    ACCEPT="$ACCEPT, ${1#--accept=}"
	    shift
	    ;;
	--accept|-A)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    ACCEPT="$ACCEPT, $2"
	    shift
	    shift
	    ;;
	--reject=*)
	    REJECT="$REJECT, ${1#--reject=}"
	    shift
	    ;;
	--reject|-R)
	    if [ $# -lt 2 ]; then syntax_error "$1 needs an argument"; fi
	    REJECT="$REJECT, $2"
	    shift
	    shift
	    ;;
	--fix-broken|-f)
	    APTGETEXTRAOPTS="${APTGETEXTRAOPTS} $1"
	    shift
	    ;;
	--skip-mount|-s)
	    SKIPMOUNT=yes
	    shift
	    ;;
	--version|-V)
	    version_exit
	    ;;
	--help|-h)
	    usage_exit
	    ;;
	-*)
	    syntax_error "Unknown option \`$1'"
	    ;;
	*)
	    syntax_error "No non-option arguments allowed"
	    ;;
	esac
    done
}

az_process_options()
{
    OPTIONS=$(echo "$OPTIONS" | awk 'BEGIN { RS = ", *" ; ORS = " " ; FS = "=" } { print toupper($1) ( $2 != "" ? "=" $2 : "" ) }'
)
    export OPTIONS

    # ensure known opts are set to 0 if not requested
    for kopt in $KNOWNOPTIONS
    do
	eval export OPTION_${kopt}=0
    done
    
    for opt in $OPTIONS
    do
	# Warn if unknown option
	local opt_known=0
	for kopt in $KNOWNOPTIONS
	do
	    case $opt in $kopt|NO$kopt|$kopt=*) opt_known=1 ;; esac
	done
	[ $opt_known = 0 ] && echo >&2 "WARNING: Unknown option \`$opt'"

	# Export option
	case ${opt} in
	*=*)
	    eval export OPTION_${opt}
#	    eval export OPTION_${opt%=*}=${opt#*=}
	    ;;
	NO*)
	    eval export OPTION_${opt#NO}=0
	    ;;
	*)
	    eval export OPTION_${opt}=\${DEFAULT_${opt}-1}
	    ;;
	esac
    done

    # Special case: set TAR to the 1st of GNUTAR and SOLARISTAR if one is set
    if [ "$OPTION_TAR" = 0 ] ; then
       [ "$OPTION_SOLARISTAR" != 0 ] && OPTION_TAR=$OPTION_SOLARISTAR
       [ "$OPTION_GNUTAR" != 0 ] && OPTION_TAR=$OPTION_GNUTAR
    fi
}

az_mount()
{
    if [ "$SKIPMOUNT" = no ]
    then
	# Maybe mount removable medium
	MNTENTRY=$(mount | egrep "^[^ ]+ on $MEDIUM type ")
	if [ "$MNTENTRY" ]
	then
	    WAS_MOUNTED=1
	    if mount | grep "$MEDIUM.*[(,]ro[,)]" >/dev/null
	    then
		error "$MEDIUM is mounted READ ONLY"
	    fi
	else
	    WAS_MOUNTED=0
	    echo "Mounting $MEDIUM"
	    local out=$(env LC_ALL=C mount $MEDIUM 2>&1)
	    if [ "$out" != "" ]
	    then
		if echo $out | grep "write-protected" >/dev/null
		then
		    error "$MEDIUM is write-protected"
		else
		    error $out
		fi
	    fi
	fi
    fi
}

az_umount()
{
    if [ "$SKIPMOUNT" = no ]
    then
	# Maybe umount removable medium
	if [ "$WAS_MOUNTED" = 0 ]
	then
	    echo "UnMounting $MEDIUM"
	    umount $MEDIUM
	fi
    fi
}

az_exit()
{
    az_umount
    exithook
}

az_init()
{
    az_loadconf
    az_process_commandline $*
    az_process_options
    check_packages
    az_filter
    check_method
    check_medium
    az_mount
}
#
# Init code
#
trap az_exit EXIT || error "error setting EXIT trap"
az_init $*