#!/bin/bash

which=$1
version=$2

usage() {
    echo "Usage: $0 {core 1.4.xx | extra 1.4.xx | moh 2.xx}"
    exit 1
}

case $1 in
    core|extra)
	if [ "$version" = "" ]; then
	    usage
	fi
	for stage in 1 2; do
	    for lang in en en_AU en_GB en_NZ es fr it ja ru sv; do
		for fmt in alaw g722 g729 gsm sln16 ulaw wav siren7 siren14; do
		    F=asterisk-${1}-sounds-${lang}-${fmt}
		    if test -f releases/${F}-${version}.tar.gz ; then
			if [ "$stage" = "1" ]; then
			    svn delete ${F}-current.tar.gz
			    svn delete ${F}-current.tar.gz.sha1
			    if test ! -e "releases/${F}-${version}.tar.gz.sha1" ; then
				pushd releases/
				sha1sum ${F}-${version}.tar.gz > ${F}-${version}.tar.gz.sha1
				svn add ${F}-${version}.tar.gz.sha1
				popd
			    fi
			else
			    ln -s releases/${F}-${version}.tar.gz ${F}-current.tar.gz
			    ln -s releases/${F}-${version}.tar.gz.sha1 ${F}-current.tar.gz.sha1
			    svn add ${F}-current.tar.gz
			    svn add ${F}-current.tar.gz.sha1
			fi
		    fi
		done
	    done
	    if [ "$stage" = "1" ]; then
		svn commit -m "Removing symlinks for new $1 sounds release $2 (stage 1)"
	    else
		svn commit -m "Adding symlinks for new $1 sounds release $2 (stage 2)"
	    fi
	done
	;;
    moh)
	if [ "$version" = "" ]; then
	    usage
	fi
	for stage in 1 2; do
	    for fmt in alaw g722 g729 gsm sln16 ulaw wav siren7 siren14; do
		F=asterisk-moh-opsound-${fmt}
		if test -f releases/${F}-${version}.tar.gz ; then
		    if [ "$stage" = "1" ]; then
			svn delete ${F}-current.tar.gz
			svn delete ${F}-current.tar.gz.sha1
			# remove compatibility symlinks
			svn delete releases/${F}.tar.gz
			svn delete releases/${F}.tar.gz.sha1
			svn delete releases/${F/opsound/freeplay}.tar.gz
			svn delete releases/${F/opsound/freeplay}.tar.gz.sha1
			if test ! -e releases/${F}-${version}.tar.gz.sha1 ; then
			    pushd releases/
			    sha1sum ${F}-${version}.tar.gz > ${F}-${version}.tar.gz.sha1
			    svn add ${F}-${version}.tar.gz.sha1
			    popd
			fi
		    else
			ln -s releases/${F}-${version}.tar.gz ${F}-current.tar.gz
			ln -s releases/${F}-${version}.tar.gz.sha1 ${F}-current.tar.gz.sha1
			svn add ${F}-current.tar.gz
			svn add ${F}-current.tar.gz.sha1
			# add compatibility symlinks
			ln -s ${F}-${version}.tar.gz releases/${F}.tar.gz
			ln -s ${F}-${version}.tar.gz.sha1 releases/${F}.tar.gz.sha1
			ln -s ${F}-${version}.tar.gz releases/${F/opsound/freeplay}.tar.gz
			ln -s ${F}-${version}.tar.gz.sha1 releases/${F/opsound/freeplay}.tar.gz.sha1
			svn add releases/${F}.tar.gz
			svn add releases/${F}.tar.gz.sha1
			svn add releases/${F/opsound/freeplay}.tar.gz
			svn add releases/${F/opsound/freeplay}.tar.gz.sha1
		    fi
		fi
	    done
	    if [ "$stage" = "1" ]; then
		svn commit -m "Removing symlinks for new $1 release $2 (stage 1)"
	    else
		svn commit -m "Adding symlinks for new $1 release $2 (stage 2)"
	    fi
	done
	;;
    *)
	usage
	;;
esac
