#!/usr/bin/env bash

. lib.bash || exit

usage() {
	echo "Usage: ${0##*/} [-C] [-n] <project>"
	echo
	echo_opt "-C" "get CVS repository root via rsync"
	echo_opt "-n" "dry-run, only print the source URL"
}

project=
type=
onlyurl=0

while getopts :CSn OPT; do
	case $OPT in
	C) type=cvs;;
	S) type=svn;;
	n) onlyurl=1;;
	*) lib:die_getopts;;
	esac
done; shift $((OPTIND-1))

(( $# )) || die "project name not specified"
[[ $type ]] || die "backup type not specified"

for arg; do
	project=${arg,,}

	case $type in
		files|frs)
			# https://sourceforge.net/p/forge/documentation/File%20Management/
			url="frs.sourceforge.net:/home/frs/project/$project/";;
		userweb)
			url="frs.sourceforge.net:/home/user-web/$project/";;
		projectweb)
			url="frs.sourceforge.net:/home/project-web/$project/";;
		cvs)
			# https://sourceforge.net/p/forge/documentation/CVS/
			url="rsync://$project.cvs.sourceforge.net/cvsroot/$project/";;
		oldsvn)
			url="rsync://$project.svn.sourceforge.net/svn/$project/";;
		svn|git|hg)
			url="rsync://$type.code.sf.net/p/$project/";;
		*)
			lib:crash "unknown type '$type'";;
	esac

	if (( onlyurl )); then
		echo "$url"
	else
		dir="$project.$type/"
		info "syncing '$url' to '$dir'"
		rsync -vshzaHAX "$url" "$dir"
		echo "$url" > "$dir/SOURCE.txt"
	fi
done
