#! /bin/bash

#  This script is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License version 2 as
#  published by the Free Software Foundation.
#
#  See the COPYING and AUTHORS files for more details.

# Read in library functions
if [ "$(type -t patch_file_name)" != function ]
then
	if ! [ -r /usr/share/quilt/scripts/patchfns ]
	then
		echo "Cannot read library /usr/share/quilt/scripts/patchfns" >&2
		exit 1
	fi
	. /usr/share/quilt/scripts/patchfns
fi

usage()
{
	echo $"Usage: quilt setup [-d sourcedir] {seriesfile|specfile}"
	if [ x$1 = x-h ]
	then
		echo $"

Initializes a source tree from a patch series file.  The patch series
file must contain the name of the relevant tar archive, in addition to
the list of patches.

-d	The directory that contains the archives and patches. Defaults
	to the directory of the series/spec file.

-l	Make the patches directory a symbolic link. If a series file is
	specified, also create a symlink to the series file.
"
		exit 0
	else
		exit 1
	fi
}

parse_series()
{
	local series="$1"

	perl -e '
		while(<>) {
			if (/^#\s*Sourcedir:\s*(\S+)/) {
				print "SOURCEDIR $1\n";
			} elsif (/^#\s*[Ss]ource:\s*(\S+)\s*(-C\s*(\S+))?/) {
				print "SOURCE $1 ", ($3 ? $3 : "."), "\n";
			} elsif (/^#\s*[Pp]atchdir:\s*(\S+)/) {
				print "PATCHDIR $1\n";
			} elsif (/^([^#\s]+)/) {
				print "PATCH $1\n";
			}
		}
	' $series
	echo "SERIES $series"
}

options=`getopt -o d:lh -- "$@"`

if [ $? -ne 0 ]
then
	usage
fi

eval set -- "$options"

while true
do
	case "$1" in
	-d)
		opt_source=$2
		shift 2 ;;
	-l)
		opt_link=1
		shift ;;
	-h)
		usage -h ;;
	--)
		shift
		break ;;
	esac
done

if [ $# -ne 1 ]
then
	usage
fi

case "$1" in
*.spec)
	spec_file="$1"
	tmpfile=$(gen_tempfile)
	series_file=$tmpfile
	if ! /usr/share/quilt/scripts/spec2series "$spec_file" > $tmpfile
	then
		exit 1
	fi
	;;
*)
	series_file=$1
	if ! [ -e "$series_file" ]
	then
		echo $"Series file $series_file not found"
		exit 1
	fi
	;;
esac

if [ -n "$opt_source" ]
then
	source="$opt_source"
elif [ -n "$spec_file" ]
then
	source="$(dirname "$spec_file")"
else
	source="$(dirname "$series_file")"
fi

status=0
packagedir=.
while read cmd arg arg2
do
	case $cmd in
	SOURCEDIR)
		# Directory for package sources
		if [ -z "$source" ]
		then
			source="$arg"
			echo $"Reading sources from $arg"
		fi
		;;
	SOURCE)
		echo $"Unpacking archive $source/$arg"
		mkdir -p "$arg2" && \
		cat_file "$source/$arg" \
		| tar xf - -C "${arg2:-.}"
		;;
	PATCHDIR)
		# Directory where package is expanded
		packagedir="$arg"
		if [ -d "$packagedir" ]
		then
			echo $"Directory $packagedir exists already."
			status=1
			break
		elif [ -e "$packagedir" ]
		then
			echo $"File $packagedir exists."
			status=1
			break
		fi
		;;
	PATCH)
		if [ -n "$opt_link" ]
		then
			if ! [ -e "$packagedir/patches" ]
			then
				arg="$source"
				if [ "${arg:0:1}" != "/" ]
				then
					arg="$PWD/$arg"
				fi
				echo $"Creating link to patches directory $arg"
				ln -s "$arg" "$packagedir/patches"
			fi
		else
			echo $"Copying patch $source/$arg"
			mkdir -p "$packagedir/patches/" && \
			cp "$source/$arg" "$packagedir/patches/"
		fi
		;;
	SERIES)
		if [ -n "$opt_link" ]
		then
			# The patches dir is a symlink to the directory
			# containing the patches. Don't write the series
			# file into that directory.
			if [ -e "$packagedir/series" ]
			then
				echo $"File $packagedir/series exists; will not override." >&2
				status=1
			elif [ -n "$spec_file" ]
			then
				# There is no series file to link to; the
				# series file we are reading from is a
				# temporary file.
				echo $"Copying series file"
				cp "$arg" "$packagedir/series"
			else
				# Create a symlink to the series file. We don't
				# want to mess with changing relative symlink
				# targets...
				if [ "${arg:0:1}" != "/" ]
				then
					arg="$PWD/$arg"
				fi
				echo $"Creatig link to series file $arg"
				ln -s "$arg" "$packagedir/series"
			fi
		else
			echo $"Copying series file"
			mkdir -p $packagedir/patches
			cp "$arg" "$packagedir/patches/series"
		fi
		;;
	*)
		status=1
		break
		;;
	esac
	s=$?
	if [ $status -eq 0 ]
	then
		status=$s
		[ $status != 0 ] && break
	fi
done \
< <(parse_series "$series_file")

if [ -n "$tmpfile" ]
then
	rm -f $tmpfile
fi

if [ $status -ne 0 ]
then
	exit 1
fi

if [ "$packagedir" != "." ]
then
	echo $"Directory $packagedir set up."
fi
### Local Variables:
### mode: shell-script
### End:
# vim:filetype=sh
