summaryrefslogtreecommitdiffstats
path: root/helperscripts/purge-deinstalled
blob: f2992962504a1bb62a0507d3ab603f14a12d0d03 (plain)
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
#!/bin/sh
#
# Purge deinstalled packages on Debian
# Copyright (c) 2012 Michael Buesch <m@bues.ch>
#

die()
{
	echo "$*"
	exit 1
}

usage()
{
	echo "Purge deinstalled packages on Debian"
	echo
	echo "Usage: purge-deinstalled [OPTIONS]"
	echo
	echo "Options:"
	echo " -y|--yes         Don't ask for confirmation"
	echo " --dry-run        Simulate purge"
}

# $1=pkgname
do_purge()
{
	local pkg="$1"

	[ -n "$opt_dryrun" ] && {
		echo "Dryrun: purge $pkg"
		return 0
	}

	dpkg --purge "$pkg" || die "Failed to purge '$pkg'"
	echo
}

opt_yes=
opt_dryrun=

while [ $# -ge 1 ]; do
	case "$1" in
	-h|--help)
		usage
		exit 0
		;;
	-y|--yes)
		opt_yes=1
		;;
	--dry-run)
		opt_dryrun=1
		;;
	*)
		die "Unknown option: $1"
		;;
	esac
	shift
done

[ "$(id -u)" = "0" ] || die "Permission denied"

for pkg in $(dpkg --get-selections |\
	     grep -Ee '[[:space:]]deinstall$' |\
	     grep -Eoe '^[^[:space:]]+'); do
	[ -n "$opt_yes" ] && {
		do_purge "$pkg"
		continue
	}
	res=
	read -p "Purge package '$pkg'? [y] " res || die "aborted"
	[ "x$res" = "x" -o\
	  "x$res" = "xy" -o "x$res" = "xY" -o\
	  "x$res" = "xyes" -o "x$res" = "xYES" -o\
	  "x$res" = "x1" ] && {
		do_purge "$pkg"
		continue
	}
done

exit 0
bues.ch cgit interface