summaryrefslogtreecommitdiff
blob: bfec62300319b381e0c16e8c8e8ae28612842991 (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
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
#!/bin/bash
# Distributed under the terms of the GNU General Public License v2
# $Header: $

ATOM=
CAT=
PN=

ACTION=
PRETEND=

CMD="emerge -Cav"
WEBAPP_DIR="/usr/share/webapps"
WEBAPP_CONFIG=

[[ -z ${RC_GOT_FUNCTIONS} ]] && source /etc/init.d/functions.sh

function help() {
	echo "Remove obsolete and unused versions of web applications"
	echo
	echo "Usage:"
	echo "   $0 [options] [action] app_name"
	echo
	echo "Options:"
	echo "   -p, --pretend"
	echo "       Instead of cleaning, simply show the commands to be executed"
	echo
	echo "Actions:"
	echo "   -P, --prune"
	echo "       Removes all but the latest version of a package. Similar to"
	echo "       emerge --prune"
	echo
	echo "   -C, --clean-unused"
	echo "       Removes all versions of a web application that have not been"
	echo "       installed into a virtual host"
	echo
	echo "   -h, --help"
	echo "       Displays this help message"
	echo
	echo "If multiple actions are given, only the action specified last will"
	echo "be executed"
}

function sanity_checks() {
	WEBAPP_CONFIG=$(which webapp-config)
	if [ "${WEBAPP_CONFIG}x" == "x" ]; then
		eerror "webapp-config not found"
		exit 1
	fi

	if [[ -z "${ACTION}" ]]; then
		eerror "Please specify a valid action"
		exit 1
	fi

	if [[ -z ${PN} && ${ACTION} != "help" ]]; then
		eerror "Please specify a web application to be cleaned"
		exit 1
	fi

	if [[ "${CAT}x" == "x" || "${PN}x" == "x" ]]; then
		eerror "Package name must be in the form CATEGORY/PN"
		exit 1
	fi

	if [[ ! -d "${WEBAPP_DIR}/${CAT}/${PN}" && ! -d  "${WEBAPP_DIR}/${PN}" ]]; then
		eerror "${PN} not found"
		exit 1
	fi
}


function prune() {

	if [[ $(ls -1 "${WEBAPP_DIR}/${PN}" | wc -l) == "1" ]]; then
		einfo "Nothing to clean"
		return
	else
		local BEST_VERSION=$(ls ${WEBAPP_DIR}/${PN} | sort -nr | head -n 1)
		for x in $(ls ${WEBAPP_DIR}/${PN}); do
			if [[ ${BEST_VERSION} != ${x} ]]; then
				CMD="${CMD} =${PN}-${x}"
			fi
		done

		einfo "Multiple versions of ${PN} detected."
		if [[ -z ${PRETEND} ]]; then
			einfo "Running ${CMD}"
			${CMD}
		else
			einfo "To prune, run the following command:"
			einfo "${CMD}"
		fi
	fi
}

function clean_unused() {
	local output=$(${WEBAPP_CONFIG} --lui ${PN})

	if [[ -z ${output} ]] ; then
		einfo "Nothing to clean"
		return
	else
		CMD="${CMD} =${output}"
		einfo "Unused versions of ${PN} detected."
		if [[ -z ${PRETEND} ]]; then
			einfo "Running ${CMD}"
			${CMD}
		else
			einfo "To clean, run the following command:"
			einfo "${CMD}"
		fi
	fi
}

function process_opts() {

	if [[ -z $1 ]]; then
		help
		exit 0
	fi

	while [ "$1+" != "+" ]; do
		case "$1" in
			-p|--pretend)
				PRETEND="yes"
				;;
			-P|--prune)
				ACTION="prune"
				;;
			-C|--clean-unused)
				ACTION="clean_unused"
				;;
			-h|--help)
				ACTION="help"
				;;
			*)
				ATOM="${1}"
				parse_atom
				;;
		esac

		shift
	done
}

parse_atom() {
	local pos=$(expr index "${ATOM}" "/")
	CAT=${ATOM:0:$pos - 1}
	PN=${ATOM:$pos}
}

process_opts $@

sanity_checks

${ACTION}