builder/repo-clean

84 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

2017-11-20 02:12:54 +00:00
#!/usr/bin/bash
shopt -s nullglob
2017-11-20 02:12:54 +00:00
parse_pkg_filename() {
declare filename=$1
declare -a filename_array
IFS='-' read -r -a filename_array <<< "$filename"
for (( i=${#filename_array[@]}; i>=0; i-- )); do
printf '%s ' "${filename_array[$i]}"
done
}
is_latest() {
declare filename=$1 p
for p in "${latest[@]}"; do
if [[ "$filename" == "$p" ]]; then
return 0
fi
done
return 1
}
_pacsort() {
declare i
for i in *.pkg.tar.xz; do
printf '%s\n' "$i"
done | pacsort -f
}
while (( $# )); do
case "$1" in
-r) flag_rm=1;;
2017-11-20 02:45:42 +00:00
-d) flag_debug=1;;
2017-11-20 02:12:54 +00:00
esac
shift
done
if ! (( flag_rm )); then
if ! [[ -d old ]]; then
mkdir -p old || {
return 1
}
fi
fi
read -d '' -r -a package_files < <( _pacsort )
for f in "${package_files[@]}"; do
unset revision version end package_name
read -r end revision version _ < <(parse_pkg_filename "$f")
package_name=${f%%-$version-$revision*}
declare -g -A latest["$package_name"]="$package_name-$version-$revision-$end"
done
for f in ${package_files[@]}; do
if is_latest "$f"; then
2017-11-20 02:45:42 +00:00
(( flag_debug )) && printf '%s is the latest version available\n' "$f"
2017-11-20 02:12:54 +00:00
else
printf 'Removing: %s\n' "$f"
if (( flag_rm )); then
rm -f "$f" "${f}.sig"
else
mv "$f" old/
if [[ -f "${f}.sig" ]]; then
mv "${f}.sig" old/
fi
fi
fi
done
if [[ -d old ]]; then
cd old
exec bash "$0" -r
fi