File mkdist of Package fate

#!/bin/sh

opt_repotype=""
opt_version=""
opt_format=tar
opt_sign=gpg
opt_destdir=/tmp
opt_autoconf=true

while [ $# -gt 0 ]; do
	next=$1; shift

	case $next in
	--format)
		opt_format=$1; shift
		: ;;
	--repotype)
		opt_repotype=$1; shift
		: ;;
	--sign)
		opt_sign=$1; shift
		: ;;
	--destdir)
		opt_destdir=$1; shift
		: ;;
	--no-autoconf)
		opt_autoconf=false;;
	--no-sign)
		opt_sign=none;;

	zip|tar)
		opt_format=$next
		: ;;
	cvs|git)
		opt_repotype=$next
		: ;;
	[0-9]*)
		opt_version=$next
		: ;;
	snap|--snap)
		opt_version=""
		: ;;
	/*|[a-z]*)
		module=$next
		case $module in
		git://*)
			# module=${module#git://*}
			opt_repotype=git;;
		esac
		: ;;
	*)
		echo "usage: mkdist [zip|tar] [cvs|git] [package] [version]" >&2
		exit 1
	esac
done

if [ -z "$opt_repotype" ]; then
	if [ -z "$module" -a -d CVS ]; then
		if [ ! -f CVS/Repository ]; then
			echo "Current directory seems to be CVS based, but no Repository file" >&2
			exit 1
		fi
		module=`cat CVS/Repository`

		if [ -f CVS/Root ]; then
			export CVSROOT=`cat CVS/Root`
		fi

		opt_repotype=cvs
	fi

	if [ -z "$module" -a -d .git ]; then
		unset URL
		eval `git remote show origin | sed '/.*Fetch *URL: */!d;s//URL=/'`
		if [ -z "$URL" ]; then
			echo "Current directory seems to be GIT based, but no origin URL" >&2
			exit 1
		fi
		module=$URL
		opt_repotype=git
	fi
fi

if [ -z "$opt_repotype" ]; then
	echo "Cannot determine repo type" >&2
	exit 1
fi

subdir=""

case $opt_repotype in
cvs)
	if [ `dirname $module` != '.' ]; then
		subdir=/`dirname $module`
	fi
	package=`basename $module`
	: ;;
git)
	package=`basename $module .git`
	: ;;
esac

set -e
checkoutdir=/tmp/dist.$$
trap "rm -rf $checkoutdir" 0 1 2 15

packagedir=$checkoutdir$subdir
rm -rf $packagedir

mkdir -p $packagedir

curdir=`pwd`

case $opt_destdir in
/*)	: ;;
".")	opt_destdir=$curdir;;
*)	opt_destdir=$curdir/$opt_destdir;;
esac

cd $checkoutdir
pwd

case $opt_repotype in
cvs)
	cvs co $module
	cd `dirname $module`
	find $package -name CVS | xargs rm -rf

	if [ -z "$opt_version" ]; then
		opt_version=`date +%Y%m%d`
	fi
	: ;;

git)
	git clone $module
	headrev=`cd $package && git log --format="format:%h" | head -1`
	find $package -name '.git*' | xargs rm -rf

	if [ -z "$opt_version" ]; then
		opt_version=`date +%Y%m%d`-$headrev
	fi
	: ;;
esac

case $opt_format in
tar)
	archive="$opt_destdir/$package-$opt_version.tar.bz2"
	: ;;
zip)
	archive="$opt_destdir/$package-$opt_version.zip"
	: ;;
esac


cat <<EOF
----------------------------------------
Module:         $module
Version:        $opt_version
Repo:           $opt_repotype
Checkout dir:   $checkoutdir
Package dir:    $packagedir
Dist archive:   $archive
----------------------------------------
EOF


mv $package $package-$opt_version
(
	cd $package-$opt_version
	if [ -f configure.ac -o -f configure.in ] && $opt_autoconf; then
		echo "Running autoconf"
		autoconf
		if [ ! -f config.h.in ]; then
			echo "Running autoheader"
			autoheader
		fi
	fi
)

case $opt_format in
tar)
	tar cf - $package-$opt_version/* | bzip2 -9 > "$archive"
	: ;;
zip)
	zip -r "$archive" $package-$opt_version/*
	: ;;
esac

chmod 644 "$archive"

case $opt_sign in
gpg)
	gpg --detach-sign "$archive"
	chmod 644 "$archive".sig
	: ;;
none)
	: ;;
*)
	echo "Don't know how to sign this tarball" >&2
	: ;;
esac