#!/bin/bash
#
# __copy1__
# __copy2__
#
CMD=$(basename $0)
CMDVER="1.1"
CMDSTR="$CMD v$CMDVER (2023-03-14)"

set -e -u

usage()
{
	echo "
== $CMDSTR == remove old version files from \$PRJ/sources dir ==

usage: $CMD [-x]

options:
  -x		execute (default: dry-run)
" >&2
	exit 1
}


cleanup()
{
	rm -f $tmp
}


# (MAIN)

f_exec=false

[ "X${1:-}" == "X-x" ] && {
	f_exec=true
	shift
}
[ $# != 0 ] && usage

tmp=$(mktemp /tmp/$CMD-XXXXXXXXX)

trap "echo '*INTR*'; cleanup; exit 255" 1 2 3

cd $PRJ/sources || {
	cleanup
	exit $?
}

echo "  purging \$PRJ/sources dir ..."

for file in $(ls *.tar.gz 2>/dev/null)
do
	eval $(jtdeb-version $file)
	echo $pkg_name >>$tmp
done
sort -u -o $tmp $tmp

action="purge"
$f_exec || action="(dummy) purge"

for file in $(cat $tmp)
do
	files=$(ls -t ${file}_*)
	set $(echo "$files" | wc -l)
	case $1 in
	  1)	;; # ok
	  *)	files=$(echo "$files" | sed -e '1d')
	  	echo "   $action " $files
		$f_exec && rm -f $files
		;;
	esac
done

cleanup
exit 0
