#!/bin/sh
# This is an example script that can be hooked into reprepro
# to either generate a hierachy like packages.debian.org/changelogs/
# or to generate changelog files in the "third party sites"
# location apt-get changelogs looks if it is not found in
# Apt::Changelogs::Server.
#
# All you have to do is to:
# - copy it into you conf/ directory,
# - if you want "third party site" style changelogs, edit the
#   CHANGELOGDIR variable below,
# and
# - add the following to any distribution in conf/distributions
#   you want to have changelogs and copyright files extracted:
#Log:
# --type=dsc changelogs.example
# (note the space at the beginning of the second line).
# This will cause this script to extract changelogs for all
# newly added source packages. (To generate them for already
# existing packages, call "reprepro rerunnotifiers").

# DEPENDENCIES: dpkg >= 1.13.9

if test "x${REPREPRO_OUT_DIR:+set}" = xset ; then
	# Note: due to cd, REPREPRO_*_DIR will no longer
	# be usable. And only things relative to outdir will work...
	cd "${REPREPRO_OUT_DIR}" || exit 1
else
	# this will also trigger if reprepro < 3.5.1 is used,
	# in that case replace this with a manual cd to the
	# correct directory...
	cat "changelog.example needs to be run by reprepro!" >&2
	exit 1
fi

# CHANGELOGDIR set means generate full hierachy
# (clients need to set Apt::Changelogs::Server to use that)
#CHANGELOGDIR=changelogs

# CHANGELOGDIR empty means generate changelog (and only changelog) files
# in the new "third party site" place apt-get changelog is using as fallback:
CHANGELOGDIR=

# Set to avoid using some predefined TMPDIR or even /tmp as
# tempdir:

# TMPDIR=/var/cache/whateveryoucreated

if test -z "$CHANGELOGDIR" ; then
addsource() {
	DSCFILE="$1"
	CANONDSCFILE="$(readlink --canonicalize "$DSCFILE")"
	CHANGELOGFILE="${DSCFILE%.dsc}.changelog"
	BASEDIR="$(dirname "$CHANGELOGFILE")"
	if ! [ -f "$CHANGELOGFILE" ] ; then
		EXTRACTDIR="$(mktemp -d)"
		(cd -- "$EXTRACTDIR" && dpkg-source --no-copy -x "$CANONDSCFILE" > /dev/null)
		install --mode=644 -- "$EXTRACTDIR"/*/debian/changelog "$CHANGELOGFILE"
		chmod -R u+rwX -- "$EXTRACTDIR"
		rm -r -- "$EXTRACTDIR"
	fi
	if [ -L "$BASEDIR"/current."$CODENAME" ] ; then
		# should not be there, just to be sure
		rm -f -- "$BASEDIR"/current."$CODENAME"
	fi
	# mark this as needed by this distribution
	ln -s -- "$(basename "$CHANGELOGFILE")" "$BASEDIR/current.$CODENAME"
	JUSTADDED="$CHANGELOGFILE"
}
delsource() {
	DSCFILE="$1"
	CHANGELOGFILE="${DSCFILE%.dsc}.changelog"
	BASEDIR="$(dirname "$CHANGELOGFILE")"
	BASENAME="$(basename "$CHANGELOGFILE")"
	if [ "x$JUSTADDED" = "x$CHANGELOGFILE" ] ; then
		exit 0
	fi
#	echo "delete, basedir=$BASEDIR changelog=$CHANGELOGFILE, dscfile=$DSCFILE, "
 	if [ "x$(readlink "$BASEDIR/current.$CODENAME")" = "x$BASENAME" ] ; then
 		rm -- "$BASEDIR/current.$CODENAME"
 	fi
 	NEEDED=0
 	for c in "$BASEDIR"/current.* ; do
 		if [ "x$(readlink -- "$c")" = "x$BASENAME" ] ; then
 			NEEDED=1
 		fi
 	done
 	if [ "$NEEDED" -eq 0 -a -f "$CHANGELOGFILE" ] ; then
 		rm -r -- "$CHANGELOGFILE"
		# to remove the directory if now empty
		rmdir --ignore-fail-on-non-empty -- "$BASEDIR"
 	fi
}

else # "$CHANGELOGDIR" set:

addsource() {
	DSCFILE="$1"
	CANONDSCFILE="$(readlink --canonicalize "$DSCFILE")"
	TARGETDIR="${CHANGELOGDIR}/${DSCFILE%.dsc}"
	SUBDIR="$(basename $TARGETDIR)"
	BASEDIR="$(dirname $TARGETDIR)"
	if ! [ -d "$TARGETDIR" ] ; then
		echo "extract $CANONDSCFILE information to $TARGETDIR"
		mkdir -p -- "$TARGETDIR"
		EXTRACTDIR="$(mktemp -d)"
		(cd -- "$EXTRACTDIR" && dpkg-source --no-copy -x "$CANONDSCFILE" > /dev/null)
		install --mode=644 -- "$EXTRACTDIR"/*/debian/copyright "$TARGETDIR/copyright"
		install --mode=644 -- "$EXTRACTDIR"/*/debian/changelog "$TARGETDIR/changelog"
		chmod -R u+rwX -- "$EXTRACTDIR"
		rm -r -- "$EXTRACTDIR"
	fi
	if [ -L "$BASEDIR"/current."$CODENAME" ] ; then
		# should not be there, just to be sure
		rm -f -- "$BASEDIR"/current."$CODENAME"
	fi
	# mark this as needed by this distribution
	ln -s -- "$SUBDIR" "$BASEDIR/current.$CODENAME"
	JUSTADDED="$TARGETDIR"
}
delsource() {
	DSCFILE="$1"
	TARGETDIR="${CHANGELOGDIR}/${DSCFILE%.dsc}"
	SUBDIR="$(basename $TARGETDIR)"
	BASEDIR="$(dirname $TARGETDIR)"
	if [ "x$JUSTADDED" = "x$TARGETDIR" ] ; then
		exit 0
	fi
#	echo "delete, basedir=$BASEDIR targetdir=$TARGETDIR, dscfile=$DSCFILE, "
 	if [ "x$(readlink "$BASEDIR/current.$CODENAME")" = "x$SUBDIR" ] ; then
 		rm -- "$BASEDIR/current.$CODENAME"
 	fi
 	NEEDED=0
 	for c in "$BASEDIR"/current.* ; do
 		if [ "x$(readlink -- "$c")" = "x$SUBDIR" ] ; then
 			NEEDED=1
 		fi
 	done
 	if [ "$NEEDED" -eq 0 -a -d "$TARGETDIR" ] ; then
 		rm -r -- "$TARGETDIR"
		# to remove the directory if now empty
		rmdir --ignore-fail-on-non-empty -- "$BASEDIR"
 	fi
}
fi # CHANGELOGDIR

ACTION="$1"
CODENAME="$2"
PACKAGETYPE="$3"
if [ "x$PACKAGETYPE" != "xdsc" ] ; then
# the --type=dsc should cause this to never happen, but better safe than sorry.
	exit 1
fi
COMPONENT="$4"
ARCHITECTURE="$5"
if [ "x$ARCHITECTURE" != "xsource" ] ; then
	exit 1
fi
NAME="$6"
shift 6
JUSTADDED=""
if [ "x$ACTION" = "xadd" -o "x$ACTION" = "xinfo" ] ; then
	VERSION="$1"
	shift
	if [ "x$1" != "x--" ] ; then
		exit 2
	fi
	shift
	while [ "$#" -gt 0 ] ; do
		case "$1" in
			*.dsc)
				addsource "$1"
				;;
			--)
				exit 2
				;;
		esac
		shift
	done
elif [ "x$ACTION" = "xremove" ] ; then
	OLDVERSION="$1"
	shift
	if [ "x$1" != "x--" ] ; then
		exit 2
	fi
	shift
	while [ "$#" -gt 0 ] ; do
		case "$1" in
			*.dsc)
				delsource "$1"
				;;
			--)
				exit 2
				;;
		esac
		shift
	done
elif [ "x$ACTION" = "xreplace" ] ; then
	VERSION="$1"
	shift
	OLDVERSION="$1"
	shift
	if [ "x$1" != "x--" ] ; then
		exit 2
	fi
	shift
	while [ "$#" -gt 0 -a "x$1" != "x--" ] ; do
		case "$1" in
			*.dsc)
				addsource "$1"
			;;
		esac
		shift
	done
	if [ "x$1" != "x--" ] ; then
		exit 2
	fi
	shift
	while [ "$#" -gt 0 ] ; do
		case "$1" in
			*.dsc)
				delsource "$1"
				;;
			--)
				exit 2
				;;
		esac
		shift
	done
fi

exit 0
# Copyright 2007,2008,2012 Bernhard R. Link <brlink@debian.org>
#
# This program 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
