lbu.in 14.1 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6
#!/bin/sh

# lbu - utility to create local backups.
# Copyright (c) 2006 Natanael Copa
# May be distributed under GPL2

7
VERSION=@VERSION@
Natanael Copa's avatar
Natanael Copa committed
8
sysconfdir=@sysconfdir@
Natanael Copa's avatar
Natanael Copa committed
9

10 11
if [ ! -f ${libalpine:="./libalpine.sh"} ]; then
	libalpine=/usr/share/lbu/libalpine.sh
12 13 14
	if [ ! -f "$libalpine" ]; then
		libalpine=/lib/libalpine.sh
	fi
15 16
fi
. $libalpine || exit 1
Natanael Copa's avatar
Natanael Copa committed
17

Natanael Copa's avatar
Natanael Copa committed
18 19
EXCLUDE_LIST="$sysconfdir"/exclude
INCLUDE_LIST="$sysconfdir"/include
Natanael Copa's avatar
Natanael Copa committed
20

Natanael Copa's avatar
Natanael Copa committed
21 22
DEFAULT_CIPHER="aes-256-cbc"

Natanael Copa's avatar
Natanael Copa committed
23
LBU_CONF="$sysconfdir"/lbu.conf
Natanael Copa's avatar
Natanael Copa committed
24 25
if [ -f "$LBU_CONF" ]; then
	. "$LBU_CONF"
26 27
fi

Natanael Copa's avatar
Natanael Copa committed
28 29
UMOUNT_LIST=

Natanael Copa's avatar
Natanael Copa committed
30 31 32 33 34 35 36 37
usage() {
	echo "$PROGRAM $VERSION"
	echo "usage: $PROGRAM <subcommand> [options] [args]

Available subcommands:
  commit (ci)
  exclude (ex, delete)
  include (inc, add)
38 39
  list (ls)
  package (pkg)
Natanael Copa's avatar
Natanael Copa committed
40
  status (stat, st)
41
  list-backup (lb) 
Natanael Copa's avatar
Natanael Copa committed
42
  revert
Natanael Copa's avatar
Natanael Copa committed
43 44 45 46 47 48 49 50 51

Common options:
 -h	Show help for subcommand.
 -q	Quiet mode.
 -v	Verbose mode.
"
	exit 1
}

Natanael Copa's avatar
Natanael Copa committed
52 53 54 55 56 57 58 59 60 61 62 63 64
cleanup() {
	local i
	for i in $UMOUNT_LIST; do
		umount $i
	done
}

exit_clean() {
	cleanup
	exit 1
}

mount_once() {
65
	if ! grep $1 /proc/mounts >/dev/null; then
Natanael Copa's avatar
Natanael Copa committed
66 67 68 69
		mount $1 && UMOUNT_LIST="$1 $UMOUNT_LIST"
	fi
}

Natanael Copa's avatar
Natanael Copa committed
70 71 72
# create backupfile
backup_apkovl() {
	local outfile="$1"
73
	local d=$( date -u -r "$outfile" "+%Y%m%d%H%M%S" )
Natanael Copa's avatar
Natanael Copa committed
74 75 76 77
	local backup=$(echo "$outfile" | sed "s/\.apkovl\.tar\.gz/.$d.tar.gz/")
	vecho "Creating backup $backup"
	if [ -z "$DRYRUN" ]; then
		mv "$outfile" "$backup"
Natanael Copa's avatar
Natanael Copa committed
78
		APKOVL_BACKUP="$backup"
Natanael Copa's avatar
Natanael Copa committed
79 80 81
	fi
}

Natanael Copa's avatar
Natanael Copa committed
82 83 84 85 86 87 88 89
restore_apkovl() {
	local outfile="$1"
	if [ -n "$DRYRUN" ] || [ -z "$APKOVL_BACKUP" ]; then
		return 0
	fi
	mv "$APKOVL_BACKUP" "$outfile"
}

Natanael Copa's avatar
Natanael Copa committed
90
# verify we have openssl if we want to encrypt
91 92 93
check_openssl() {
	[ -z "$ENCRYPTION" ] && return 0
	OPENSSL=$(which openssl 2>/dev/null) || die "openssl was not found"
Natanael Copa's avatar
Natanael Copa committed
94

95 96 97
	$OPENSSL list-cipher-commands | grep "^$ENCRYPTION$" > /dev/null \
		|| die "Cipher $ENCRYPTION is not supported"
}
Natanael Copa's avatar
Natanael Copa committed
98 99 100 101 102 103 104 105 106

# list_add(char *listfile, char* file...)
list_add() {
	local list="$1"
	shift
	mkdir -p `dirname "$list"`
	while [ $# -gt 0 ] ; do
		filename=`echo "$1" | sed 's:^/\+::'`
		if grep "^$filename$" "$list" >/dev/null 2>&1 ; then
107
			vecho "$filename is already in $list."
Natanael Copa's avatar
Natanael Copa committed
108
		else
109
			vecho "Adding $filename to $list."
Natanael Copa's avatar
Natanael Copa committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			echo "$filename" >> "$list"
		fi
		shift
	done
}

# list_delete(char *listfile, char *file...)
list_delete() {
	local list="$1"
	local tmp="$list.old"
	shift
	[ -f "$list" ] || return 1
	while [ $# -gt 0 ] ; do
		filename=`echo "$1" | sed 's:^/\+::'`
		mv "$list" "$tmp"
125
		vecho "Removing $filename from list."
Natanael Copa's avatar
Natanael Copa committed
126 127 128 129 130 131
		grep -v "^$filename$" "$tmp" > "$list"
		rm "$tmp"
		shift
	done
}

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
# unpack archive on LBU_MEDIA to given dir
unpack_apkovl() {
	local f="$(hostname).apkovl.tar.gz"
	local dest="$1"
	local mnt="/media/$LBU_MEDIA"
	local count=0
	mkdir -p "$dest"
	mount_once "$mnt"
	if [ ! -f "$mnt/$f" ]; then
		return 1
	fi
	if [ -z "$ENCRYPTION" ]; then
		tar -C "$dest" -zxf "$mnt/$f"
		return
	fi
	f="$f.$ENCRYPTION"
	check_openssl
        while [ $count -lt 3 ]; do
		$OPENSSL enc -d -$ENCRYPTION -in "$mnt/$f" | tar \
			-C "$dest" -zx 2>/dev/null && return 0
		count=$(( $count + 1 ))
	done
	cleanup
	die "Failed to unpack $mnt/$f"
}		
157

Natanael Copa's avatar
Natanael Copa committed
158 159 160 161 162
#
# lbu_include - add/remove files to include list
#
usage_include() {
	echo "$PROGRAM $VERSION
Natanael Copa's avatar
Natanael Copa committed
163
Add filename(s) to include list ($sysconfdir/include)
164 165 166

usage: $PROGRAM include|inc|add [-rv] <file> ...
       $PROGRAM include|inc|add [-v] -l
Natanael Copa's avatar
Natanael Copa committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

Options:
  -l	List contents of include list.
  -r	Remove specified file(s) from include list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_include() {
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_include
		show_include
		return
	fi
182

Natanael Copa's avatar
Natanael Copa committed
183 184 185 186 187 188 189 190 191 192 193
	[ $# -lt 1 ] && usage_include
	if [ "$REMOVE" ] ; then
		list_delete "$INCLUDE_LIST" "$@"
	else
		list_add "$INCLUDE_LIST" "$@"
		list_delete "$EXCLUDE_LIST" "$@"
	fi
}

show_include() {
	if [ -f "$INCLUDE_LIST" ] ; then
194
		vecho "Include files:"
Natanael Copa's avatar
Natanael Copa committed
195 196 197 198
		cat "$INCLUDE_LIST"
	fi
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#
# lbu_package - create a package
#
usage_package() {
	echo "$PROGRAM $VERSION
Create backup package.

usage: $PROGRAM package|pkg -v [<dirname>|<filename>]

Options:
  -v	Verbose mode.

If <dirname> is a directory, a package named <hostname>.apkovl.tar.gz will
be created in the specified directory.

If <filename> is specified, and is not a direcotry, a package with the
specified name willbe created.

217
If <dirname> nor <filename> is not specified, a package named
218 219 220 221 222 223 224 225 226
<hostname>.apkovl.tar.gz will be created in current work directory.
"
	exit 1
}

cmd_package() {
	local pkg="$1"
	local rc=0
	local owd="$PWD"
227
	local suff="apkovl.tar.gz"
Natanael Copa's avatar
Natanael Copa committed
228
	local tmpdir tmppkg
229 230

	check_openssl
Natanael Copa's avatar
Natanael Copa committed
231
	init_tmpdir tmpdir
232 233

	[ -n "$ENCRYPTION" ] && suff="$suff.$ENCRYPTION"
234 235 236

	# find filename
	if [ -d "$pkg" ] ; then
237
		pkg="$pkg/$(hostname).$suff"
238
	elif [ -z "$pkg" ]; then
239
		pkg="$PWD/$(hostname).$suff"
240 241
	fi

Natanael Copa's avatar
Natanael Copa committed
242 243
	tmppkg="$tmpdir/$(basename $pkg)"

244
	cd "${ROOT:-/}"
245
	# remove old package.list
246
	if [ -f etc/lbu/packages.list ] && [ -f var/lib/apk/world ]; then
247 248 249 250
		echo "Note: Removing /etc/lbu/packages.list."
		echo "      /var/lib/apk/world will be used."
		rm -f etc/lbu/packages.list
	fi
251
	currentlist=$(apk audit --backup -q)
252 253 254
	if [ -f var/lib/apk/world ]; then
		currentlist="$currentlist var/lib/apk/world"
	fi
255

256 257 258
	# create tar archive
	[ -f "$EXCLUDE_LIST" ] && excl="-X $EXCLUDE_LIST"
	[ -f "$INCLUDE_LIST" ] && incl="-T $INCLUDE_LIST"
Natanael Copa's avatar
Natanael Copa committed
259 260 261 262 263
	if [ -n "$VERBOSE" ]; then
		echo "Archiving the following files:" >&2
		# we dont want to mess the tar output with the
		# password prompt. Lets get the tar output first.
		tar  $excl $incl -c -v $currentlist > /dev/null
Natanael Copa's avatar
Natanael Copa committed
264
		rc=$?
Natanael Copa's avatar
Natanael Copa committed
265 266 267 268
	fi
	if [ $rc -eq 0 ]; then
		if [ -z "$ENCRYPTION" ]; then
			tar $excl $incl -c $currentlist | gzip -c  >"$tmppkg"
Natanael Copa's avatar
Natanael Copa committed
269
			rc=$?
Natanael Copa's avatar
Natanael Copa committed
270
		else
Natanael Copa's avatar
Natanael Copa committed
271 272 273 274 275
			set -- enc "-$ENCRYPTION" -salt
			[ -n "$PASSWORD" ] && set -- "$@" -pass pass:"$PASSWORD"
			tar $excl $incl -c $currentlist | gzip -c \
				| $OPENSSL "$@" > "$tmppkg"
			rc=$?
276
		fi
277
	fi
Natanael Copa's avatar
Natanael Copa committed
278
	cd "$owd"
Natanael Copa's avatar
Natanael Copa committed
279

Natanael Copa's avatar
Natanael Copa committed
280
	# actually commit unless dryrun mode
Natanael Copa's avatar
Natanael Copa committed
281
	if [ $rc -eq 0 ]; then
282 283 284 285
		if [ -z "$DRYRUN" ]; then
			if [ "x$pkg" = "x-" ]; then
				cat "$tmppkg"
			else
Natanael Copa's avatar
Natanael Copa committed
286 287 288 289 290 291 292
				if cp "$tmppkg" "$pkg.new"; then
					mv "$pkg.new" "$pkg"
					rc=$?
				else
					rm -f "$pkg.new"
					rc=1
				fi
293 294
			fi
		fi
Natanael Copa's avatar
Natanael Copa committed
295
		[ $rc -eq 0 ] && vecho "Created $pkg"
Natanael Copa's avatar
Natanael Copa committed
296
	fi
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	return $rc
}

#
# lbu list - list files that would go to archive
#
usage_list() {
	echo "$PROGRAM $VERSION
Lists files that would go to tar package. Same as: 'lbu package -v /dev/null'

usage: $PROGRAM list|ls
"
	exit 1
}

cmd_list() {
	VERBOSE="-v"
	cmd_package /dev/null
}
Natanael Copa's avatar
Natanael Copa committed
316 317 318 319 320 321

#
# lbu_commit - commit config files to writeable media
#
usage_commit() {
	echo "$PROGRAM $VERSION
322 323 324
Create a backup of config to writeable media.

usage: $PROGRAM commit|ci [-nv] [<media>]
Natanael Copa's avatar
Natanael Copa committed
325 326

Options:
Natanael Copa's avatar
Natanael Copa committed
327
  -d	Remove old apk overlay files.
328
  -e	Protect configuration with a password.
Natanael Copa's avatar
Natanael Copa committed
329
  -n	Don't commit, just show what would have been commited.
Natanael Copa's avatar
Natanael Copa committed
330
  -p <password>	Give encryption password on the command-line
Natanael Copa's avatar
Natanael Copa committed
331 332 333 334
  -v	Verbose mode.

The following values for <media> is supported: floppy usb
If <media> is not specified, the environment variable LBU_MEDIA will be used.
Natanael Copa's avatar
Natanael Copa committed
335

336
Password protection will use $DEFAULT_CIPHER encryption. Other ciphers can be
Natanael Copa's avatar
Natanael Copa committed
337 338 339
used by setting the DEFAULT_CIPHER or ENCRYPTION environment variables.
For possible ciphers, try: openssl -v

Natanael Copa's avatar
Natanael Copa committed
340 341
The password used to encrypt the file, can either be specified with the -p
option or using the PASSWORD environment variable.
342

Natanael Copa's avatar
Natanael Copa committed
343
The environment varialbes can also be set in $LBU_CONF
Natanael Copa's avatar
Natanael Copa committed
344 345 346 347 348
"
	exit 1
}

cmd_commit() {
Natanael Copa's avatar
Natanael Copa committed
349
	local media mnt statuslist tmplist currentlist
350 351 352 353
	local incl excl outfile ovls lines

	check_openssl

Natanael Copa's avatar
Natanael Copa committed
354 355 356
	# turn on verbose mode if dryrun
	[ -n "$DRYRUN" ] && VERBOSE="-v"

357
	# find what media to use
Natanael Copa's avatar
Natanael Copa committed
358
	media="${1:-$LBU_MEDIA}"
Natanael Copa's avatar
Natanael Copa committed
359 360
	[ -z "$media" ] && usage_commit

361
	# mount media unles its already mounted
Natanael Copa's avatar
Natanael Copa committed
362
	mnt=/media/$media
Natanael Copa's avatar
Natanael Copa committed
363
	[ -d "$mnt" ] || usage
Natanael Copa's avatar
Natanael Copa committed
364
	mount_once "$mnt" || die "failed to mount $mnt"
Natanael Copa's avatar
Natanael Copa committed
365

Natanael Copa's avatar
Natanael Copa committed
366 367 368 369 370 371 372
	# find the outfile
	outfile="$mnt/$(hostname).apkovl.tar.gz"
	if [ -n "$ENCRYPTION" ]; then
		outfile="$outfile.$ENCRYPTION"
	fi

	# remove old config files
373
	if [ -n "$DELETEOLDCONFIGS" ] ; then
Natanael Copa's avatar
Natanael Copa committed
374 375 376 377
		local rmfiles=$(ls "$mnt/"*.apkovl.tar.gz* 2>/dev/null)
		if [ -n "$rmfiles" ] ; then
			if [ -n "$VERBOSE" ]; then
				echo "Removing old apk overlay files:" >&2
378
				echo "$rmfiles"
Natanael Copa's avatar
Natanael Copa committed
379
				echo "" >&2
380
			fi
Natanael Copa's avatar
Natanael Copa committed
381
			[ -z "$DRYRUN" ] && rm "$mnt/"*.apkovl.tar.gz*
382 383
		fi
	else
Natanael Copa's avatar
Natanael Copa committed
384
       		lines=$(ls -1 "$mnt"/*.apkovl.tar.gz* 2>/dev/null)
Natanael Copa's avatar
Natanael Copa committed
385
		if [ "$lines" = "$outfile" ]; then
Natanael Copa's avatar
Natanael Copa committed
386
			backup_apkovl "$outfile"
Natanael Copa's avatar
Natanael Copa committed
387
		elif [ -n "$lines" ]; then
388
	               	# More then one apkovl, this is a security concern
Natanael Copa's avatar
Natanael Copa committed
389
			cleanup
Natanael Copa's avatar
Natanael Copa committed
390 391 392 393
			eecho "The following apkovl file(s) were found:"
			eecho "$lines"
			eecho ""
	               	die "Please use -d to replace."
394
		fi
Natanael Copa's avatar
Natanael Copa committed
395
	fi
396 397 398

	# create package
	if ! cmd_package "$outfile"; then
Natanael Copa's avatar
Natanael Copa committed
399
		restore_apkovl "$outfile"
Natanael Copa's avatar
Natanael Copa committed
400
		cleanup
Natanael Copa's avatar
Natanael Copa committed
401
		die "Problems creating archive. aborting"
402
	fi
Natanael Copa's avatar
Natanael Copa committed
403

404 405
	# delete old backups if needed
	# poor mans 'head -n -N' done with awk.
406
	ls "$mnt"/$(hostname).[0-9][0-9][0-9][0-9]*[0-9].tar.gz 2>/dev/null \
407 408 409 410 411 412 413
		| awk '{ a[++i] = $0; } END { 
			print a[0]; 
			while (i-- > '"${BACKUP_LIMIT:-0}"') { 
				print a[++j] 
			}
		}' | xargs rm 2>/dev/null

Natanael Copa's avatar
Natanael Copa committed
414
	# remove obsolete file. some older version of alpine needs this
415
	# to be able to upgrade
416 417
	if [ -z "$DRYRUN" ] && [ -f $mnt/packages.list ]; then
		echo "Note: Removing packages.list from $(basename $mnt)."
418
		echo "      /var/lib/apk/world will be used."
419 420
		rm -f $mnt/packages.list
	fi
Natanael Copa's avatar
Natanael Copa committed
421

Natanael Copa's avatar
Natanael Copa committed
422
	# make sure data is written
Natanael Copa's avatar
Natanael Copa committed
423
	sync
Natanael Copa's avatar
Natanael Copa committed
424
	[ "$media" = "floppy" ] && sleep 1
Natanael Copa's avatar
Natanael Copa committed
425 426

	# move current to commited.
427
	vecho "Successfully saved apk overlay files"
Natanael Copa's avatar
Natanael Copa committed
428 429 430 431 432 433 434
}

#---------------------------------------------------------------------------
# lbu_exclude - add remove file(s) from exclude list

usage_exclude() {
	echo "$PROGRAM $VERSION
Natanael Copa's avatar
Natanael Copa committed
435
Add filename(s) to exclude list ($sysconfdir/exclude)
436 437 438

usage: $PROGRAM exclude|ex|delete [-rv] <file> ...
       $PROGRAM exclude|ex|delete [-v] -l
Natanael Copa's avatar
Natanael Copa committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453

Options:
  -l	List contents of exclude list.
  -r	Remove specified file(s) from exclude list instead of adding.
  -v	Verbose mode.
"
	exit 1
}

cmd_exclude() {
	if [ "$LIST" ] ; then
		[ $# -gt 0 ] && usage_exclude
		show_exclude
		return
	fi
454

Natanael Copa's avatar
Natanael Copa committed
455 456 457 458 459 460 461 462 463 464 465
	[ $# -lt 1 ] && usage_exclude
	if [ "$REMOVE" ] ; then
		list_delete "$EXCLUDE_LIST" "$@"
	else
		list_delete "$INCLUDE_LIST" "$@"
		list_add "$EXCLUDE_LIST" "$@"
	fi
}

show_exclude() {
	if [ -f "$EXCLUDE_LIST" ] ; then
466
		vecho "Exclude files:"
Natanael Copa's avatar
Natanael Copa committed
467
		cat "$EXCLUDE_LIST"
468
	fi
Natanael Copa's avatar
Natanael Copa committed
469 470
}

Natanael Copa's avatar
Natanael Copa committed
471
#---------------------------------------------------------------------------
472
# lbu_listbackup - Show old commits
473
usage_listbackup() {
Natanael Copa's avatar
Natanael Copa committed
474 475 476 477
	cat <<EOF
$PROGRAM $VERSION
Show old commits.

478
usage: $PROGRAM list-backup [<media>]
Natanael Copa's avatar
Natanael Copa committed
479 480 481 482 483

EOF
	exit 1
}

484
cmd_listbackup() {
Natanael Copa's avatar
Natanael Copa committed
485 486
	local media=${1:-"$LBU_MEDIA"}
	local mnt="/media/$media"
Natanael Copa's avatar
Natanael Copa committed
487
	[ -z "$media" ] && usage_listbackup
Natanael Copa's avatar
Natanael Copa committed
488 489 490 491 492 493 494 495

	mount_once "$mnt" || die "failed to mount $mnt"
	ls -1 "$mnt"/*.[0-9][0-9]*[0-9][0-9].tar.gz* 2>/dev/null | sed 's:.*/::'
}

#---------------------------------------------------------------------------
# lbu_revert - revert to old config
usage_revert() {
Natanael Copa's avatar
Natanael Copa committed
496 497 498 499 500 501
	cat <<EOF
$PROGRAM $VERSION
Revert to older commit.

usage: $PROGRAM revert <REVISION> [<media>]

Natanael Copa's avatar
Natanael Copa committed
502
The revision should be one of the files listed by 'lbu list-backup'.
Natanael Copa's avatar
Natanael Copa committed
503

Natanael Copa's avatar
Natanael Copa committed
504
EOF
Natanael Copa's avatar
Natanael Copa committed
505 506 507 508
}

cmd_revert() {
	local media=${2:-"$LBU_MEDIA"}
Natanael Copa's avatar
Natanael Copa committed
509 510 511 512 513 514
	[ -z "$media" ] && usage_revert
	local mnt="/media/$media"
	local revertto="$mnt/$1"
	local current="$mnt/$(hostname).apkovl.tar.gz"

	if [ -n "$ENCRYPTION" ]; then
515
		current="$current.$ENCRYPTION"
Natanael Copa's avatar
Natanael Copa committed
516 517 518
	fi
	mount_once "$mnt" || die "failed to mount $mnt"
	[ -f "$revertto" ] || die "file not found: $revertto"
519
	backup_apkovl "$current"
Natanael Copa's avatar
Natanael Copa committed
520
	vecho "Reverting to $1"
521
	[ -z "$DRYRUN" ] && mv "$revertto" "$current"
Natanael Copa's avatar
Natanael Copa committed
522 523
}

Natanael Copa's avatar
Natanael Copa committed
524 525 526 527 528 529
#---------------------------------------------------------------------------
# lbu_status - check what files have been changed since last save
usage_status() {
	echo "$PROGRAM $VERSION
Check what files have been changed since last commit.

530
usage: $PROGRAM status|st [-av]
Natanael Copa's avatar
Natanael Copa committed
531 532 533 534 535 536 537 538

Options:
  -a	Compare all files, not just since last commit.
  -v	Also show include and exclude lists.
"
	exit 1
}

539 540 541 542 543
cmd_status() {
	if [ -n "$USE_DEFAULT" ]; then
		apk audit --backup
		return 0
	fi
544 545 546 547 548 549 550 551 552
	LBU_MEDIA=${1:-"$LBU_MEDIA"}
	[ -z "$LBU_MEDIA" ] && usage_status
	local tmp
	init_tmpdir tmp
	mkdir -p "$tmpdir/a" "$tmp/b"

	# unpack last commited apkovl to tmpdir/a
	unpack_apkovl "$tmp/a"

553
	# generate new apkovl and extract to tmpdir/b
554 555
	cmd_package - | tar -C "$tmp/b" -zx

556
	# show files that exists in a but not in b as deleted
557 558 559 560 561 562 563
	local f
	( cd "$tmp"/a && find ) | while read f; do
		f=${f#./}
		[ "$f" = "." ] && continue
		[ -e "$tmp/b/$f" ] || echo "D $f"
	done
	
564
	# compare files in b with files in a
565 566 567 568 569 570 571
	( cd "$tmp"/b && find ) | while read f; do
		f=${f#./}
		[ "$f" = "." ] && continue
		local a="$tmp/a/$f"
		local b="$tmp/b/$f"
		if [ ! -e "$a" ]; then
			echo "A $f"
572 573
		elif [ -f "$a" ] && [ -f "$b" ] && [ "$b" -nt "$a" ] \
		     && ! cmp -s "$a" "$b"; then
574 575 576
			echo "U $f"
		fi
	done
577 578
}

Natanael Copa's avatar
Natanael Copa committed
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
#-----------------------------------------------------------
# lbu_diff - run a diff against last commit
usage_diff() {
	echo "$PROGRAM $VERSION
Run a diff against last commit

usage: $PROGRAM diff [<media>]
"
	exit 1
}

cmd_diff() {
	LBU_MEDIA=${1:-"$LBU_MEDIA"}
	[ -z "$LBU_MEDIA" ] && usage_diff
	local tmp
	init_tmpdir tmp
	mkdir -p "$tmpdir/a" "$tmp/b"
	unpack_apkovl "$tmp/a"
	cmd_package - | tar -C "$tmp/b" -zx
	cd "$tmp" && diff -ruN a b 
}
	

Natanael Copa's avatar
Natanael Copa committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

#-----------------------------------------------------------
# Main

cmd=`echo "$PROGRAM" | cut -s -d_ -f2`
PROGRAM=`echo "$PROGRAM" | cut -d_ -f1`
if [ -z "$cmd" ] ; then
	cmd="$1"
	[ -z "$cmd" ] && usage
	shift
fi

# check for valid sub command
case "$cmd" in
	include|inc|add)	SUBCMD="include";;
	commit|ci)		SUBCMD="commit";;
	exclude|ex|delete)	SUBCMD="exclude";;
620 621
	list|ls)		SUBCMD="list";;
	package|pkg)		SUBCMD="package";;
Natanael Copa's avatar
Natanael Copa committed
622
	status|stat|st)		SUBCMD="status";;
623
	list-backup|lb)		SUBCMD="listbackup";;
Natanael Copa's avatar
Natanael Copa committed
624
	revert)			SUBCMD="revert";;
625
	diff)			SUBCMD="diff";;
Natanael Copa's avatar
Natanael Copa committed
626 627 628 629
	*)			usage;;
esac

# parse common args
630
while getopts "adehlM:np:qrv" opt ; do
Natanael Copa's avatar
Natanael Copa committed
631 632 633 634
	case "$opt" in
		a) 	[ $SUBCMD = status ] || usage_$SUBCMD
			USE_DEFAULT="-a"
			;;
635 636
		d)	DELETEOLDCONFIGS="yes"
			;;
637
		e)	[ -z "$ENCRYPTION" ] && ENCRYPTION="$DEFAULT_CIPHER"
638
			;;
Natanael Copa's avatar
Natanael Copa committed
639 640 641 642 643 644 645
		h) 	usage_$SUBCMD
			;;
		l)	LIST="-l"
			;;
		n) 	[ $SUBCMD = commit ] || usage_$SUBCMD
			DRYRUN="-n"
			;;
646 647
		p)	PASSWORD="$OPTARG"
			;;
Natanael Copa's avatar
Natanael Copa committed
648 649 650 651 652 653 654 655 656
		q)	QUIET="$QUIET -q"
			;;
		r)	REMOVE="-r"
			;;
		v) 	VERBOSE="$VERBOSE -v"
			;;
	esac
done
shift `expr $OPTIND - 1`
657

Natanael Copa's avatar
Natanael Copa committed
658
trap exit_clean SIGINT SIGTERM
659
cmd_$SUBCMD "$@"
Natanael Copa's avatar
Natanael Copa committed
660 661 662
retcode=$?

cleanup
Natanael Copa's avatar
Natanael Copa committed
663
exit $retcode