Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
#!/bin/sh
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
#
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
set -o errexit -o noglob;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
glob() { set +o noglob; echo ${@}; set -o noglob; };
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
map() { local _ifs="${IFS}" _sep="${1}"; shift; IFS="${_sep}"; echo "${*}"; IFS="${_ifs}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
rc() { echo "${@}"; "${@}"; };
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
rotate_build() {
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	local _build_dname="${1}" _hostname="${2}" _limit="${3}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	local _dist_dates="";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	for _dist_fname in $(glob			\
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			${_build_dname}/*.tar.xz	\
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			${_build_dname}/*.tar.xz.asc); do
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		if [ -e "${_dist_fname}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			_dist_date="${_dist_fname#*@${_hostname}-}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			_dist_date="${_dist_date%.tar.xz*}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			_dist_date="${_dist_date%-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			_dist_dates="${_dist_dates:+${_dist_dates} }${_dist_date}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	if [ -n "${_dist_dates}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		rotate_build_dates "${_build_dname}" "${_dist_dates}" "${_limit}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
};
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
rotate_build_dates() {
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	local _build_dname="${1}" _dist_dates="${2}" _limit="${3}";
Lucio Andrés Illanes Albornoz 907bd3
	local _dist_dates_count="" _dist_dates_count_limit="" _dist_fname="" _nl="
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
";	_dist_dates="$(map "${_nl}" ${_dist_dates} | sort | uniq)";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	_dist_dates_count="$(echo "${_dist_dates}" | wc -l)";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	if [ "${_dist_dates_count}" -gt "${_limit}" ]; then
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		_dist_dates_count_limit=$((${_dist_dates_count}-${_limit}));
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		_dist_dates="$(echo "${_dist_dates}" |\
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
				sed -n "1,${_dist_dates_count_limit}p")";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		for _dist_date in ${_dist_dates}; do
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			for _dist_fname in $(glob	\
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
					${_build_dname}/*-${_dist_date}-*.tar.xz*); do
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
				rc rm -f "${_dist_fname}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
			done;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		done;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	fi;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
};
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
rotate_builds() {
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	local _build_dnames="${1}" _limit="${2}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	local _hostname="$(hostname)";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	for _build_dname in ${_build_dnames}; do
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
		rotate_build "${_build_dname}" "${_hostname}" "${_limit}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
	done;
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
};
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
rotate_builds "${1}" "${2:-3}";
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
Lucio Andrés Illanes Albornoz (arab, vxp) ed54b7
# vim:filetype=sh noexpandtab sw=8 ts=8