aboutsummaryrefslogtreecommitdiff
path: root/apkg-clean
blob: e0a329f44c4d5981cb137df82d30028059e49b79 (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
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
#!/bin/sh
#
# Cleanup old packages and sources
#
# usage:
#    apkg-clean [-p] [-s] | xargs rm

# Given a source entry from an abuild, return the filename that would be stored.
source_filename() {
	s=${1%::noextract}
	case $s in
		*::*) printf '%s' "${s%%::*}";;
		   *) printf '%s' "${s##*/}";;
	esac
}

# Print stale .spm package files (old versions that don't match current name#version-release.spm).
stale_packages() {
	if [ "$APKG_PACKAGE_DIR" ]; then
		# Collect all current .spm names, then check the centralized dir once.
		keep=""
		for i in $(apkg -s 2>/dev/null); do
			dir=$(apkg -p "$i" 2>/dev/null) || continue
			unset name version release
			. "$dir/abuild" 2>/dev/null
			keep="$keep $name#$version-$release.spm"
		done
		for f in "$APKG_PACKAGE_DIR"/*.spm; do
			[ -f "$f" ] || continue
			case " $keep " in
				*" ${f##*/} "*) continue;;
			esac
			printf '%s\n' "$f"
		done
	else
		for i in $(apkg -s 2>/dev/null); do
			dir=$(apkg -p "$i" 2>/dev/null) || continue
			unset name version release
			. "$dir/abuild" 2>/dev/null
			current="$name#$version-$release.spm"
			for f in "$dir"/*.spm; do
				[ -f "$f" ] || continue
				[ "${f##*/}" != "$current" ] && printf '%s\n' "$f"
			done
		done
	fi
}

# Print stale source files (not referenced by any package's current source variable).
stale_sources() {
	if [ "$APKG_SOURCE_DIR" ]; then
		# Collect all current source filenames across all recipes.
		keep=""
		for i in $(apkg -s 2>/dev/null); do
			dir=$(apkg -p "$i" 2>/dev/null) || continue
			unset source
			. "$dir/abuild" 2>/dev/null
			for s in $source; do
				keep="$keep $(source_filename "$s")"
			done
		done

		for f in "$APKG_SOURCE_DIR"/*; do
			[ -f "$f" ] || continue
			case " $keep " in
				*" ${f##*/} "*) continue;;
			esac
			printf '%s\n' "$f"
		done
	else
		# Check each recipe directory, filtering out recipe files and current sources.
		for i in $(apkg -s 2>/dev/null); do
			dir=$(apkg -p "$i" 2>/dev/null) || continue
			unset source
			. "$dir/abuild" 2>/dev/null

			keep=""
			for s in $source; do
				keep="$keep $(source_filename "$s")"
			done

			(cd "$dir" 2>/dev/null || continue
			for f in *; do
				[ -f "$f" ] || continue
				case $f in
					abuild|depends|preinstall|postinstall|info|\
					*.spm|.shasum|.files) continue;;
				esac
				case " $keep " in
					*" $f "*) continue;;
				esac
				printf '%s\n' "$PWD/$f"
			done
			)
		done
	fi
}

do_pkg=true
do_src=true

for arg; do
	case $arg in
		-p) do_src=false;;
		-s) do_pkg=false;;
		-h) cat <<EOF
usage:
    $0 [-p] [-s] | xargs rm -v

options:
    -p  print packages only
    -s  print sources only

EOF
		exit 0;;
	esac
done

$do_pkg && stale_packages
$do_src && stale_sources
exit 0