#!/bin/bash
#
###########################################################################
#                                                                         #
# deb-repository  -  v1.2.0  -  Debian Repository Generator               #
#                                                                         #
# This file is part of the afulinux.de Debian repository.                 #
#                                                                         #
# Copyright (C) 2003-2011 Andreas Stempfhuber <andi@afulinux.de>          #
#                                                                         #
# This program is free software: you can redistribute it and/or modify    #
# it under the terms of the GNU General Public License as published by    #
# the Free Software Foundation, either version 3 of the License, or       #
# (at your option) any later version.                                     #
#                                                                         #
# 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, see <http://www.gnu.org/licenses/>.   #
#                                                                         #
###########################################################################

# scanpackages search-directory package-directory
scanpackages () {
	local search_dir="$1"
	local file="$2/binary-$arch/$packages"

	mkdir -p "${file%/*}"
	rm -f "$file.tmp"
	apt-ftparchive packages "$search_dir" > "$file.tmp"
	use_new "$file.tmp"
}

# scansources search-directory package-directory
scansources () {
	local search_dir="$1"
	local file="$2/source/$sources"

	mkdir -p "${file%/*}"
	rm -f "$file.tmp"
	apt-ftparchive -qq sources "$search_dir" > "$file.tmp"
	use_new "$file.tmp"
}

# scan search-directory
scan () {
	local search_dir="$1"
	local package_dir="$search_dir"

	echo -e "\n$search_dir:"

	if [ "${search_dir##*/}" = "$all" ]; then
		search_dir="${search_dir%/*}"
	fi

	scanpackages "$search_dir" "$package_dir"
	scansources "$search_dir" "$package_dir"
}

# search start-directory
# Search for packages
search () {
	local start_dir=$1

	for dir in `ls -1 $start_dir`; do
		if [ -d "$start_dir/$dir" ]; then
			scan "$start_dir/$dir"
		fi
	done
}

# use_new_contents file
# Compare the old gziped file with the new non-compressed file and use the
# non-compressed one if different
use_new_contents () {
	local old="$1.gz"
	local new="$1"

	[ -f "$old" ] && gunzip -c "$old" | cmp -s "$new"

	if [ $? -ne 0 ] || [ ! -f "$old" ] || [ $force -ge 1 ]; then
		# The files are different or $old does not exist
		changed=1
		chmod +r "$new"
		gzip -9 -f "$new"
		echo_bold "${old##*/} created."
	else
		# $new is the same as $old, remove the new one, don't touch the
		# old one
		echo "${old##*/} unchanged."
		rm -f "$new"
	fi
}

# use_new file
# Compare the old file with the new one. If different use the new one and 
# create also the gzip and bzip2 compressed files
use_new () {
	local old="${1%.*}"
	local new="$1"

	[ -f "$old" ] && cmp -s "$old" "$new"

	if [ $? -ne 0 ] || [ ! -f "$old" ] || [ $force -ge 1 ]; then
		# The files are different or $old does not exist
		changed=1
		mv "$new" "$old"
		chmod +r "$old"
		cat "$old" | gzip -9 > "$old.gz"
		cat "$old" | bzip2 -9 > "$old.bz2"
		echo_bold "${old##*/} created."
	else
		# $new is the same as $old, remove the new one, don't touch the
		# old one
		echo "${old##*/} unchanged."
		rm -f "$new"
	fi
}

# echo_bold text...
# Print text in bold
echo_bold () {
	echo -e "\E[1m$@\E[0m"
}


# Main

# Source configuration
if [ -r "$0.conf" ]; then
	. "$0.conf"
else
	echo -e "\nConfiguration file '$0.conf' missing or not readable!\n"
	exit 1
fi

all="all"
dists="dists"
packages="Packages"
sources="Sources"
contents="Contents-$arch"
release="Release"
changed=0		# marker to remember if files are changed
force=0			# force updating of unchanged files

if [ "$1" = "-f" ]; then
	force=1
	shift
fi

if [ -z "$1" ]; then
	echo -e "\nDistribution name not set on the command line, using '$defaultdist'."
	dist="$dists/$defaultdist"
else
	dist="$dists/$1"
fi

mkdir -p "$dist/$all"
search "$dist"

echo -e "\n$dist:"
# generate Contents file
apt-ftparchive contents "$dist" > "$dist/$contents"
use_new_contents "$dist/$contents"

# generate and sign Release file
if [ ! -f "$dist/$release" -o $changed -ne 0 ]; then
	apt-ftparchive \
		-o APT::FTPArchive::Release::Origin="$origin" \
		-o APT::FTPArchive::Release::Label="$label" \
		-o APT::FTPArchive::Release::Suite="$suite" \
		-o APT::FTPArchive::Release::Codename="${dist##*/}" \
		-o APT::FTPArchive::Release::Architectures="$arch" \
		-o APT::FTPArchive::Release::Components="$components" \
		-o APT::FTPArchive::Release::Description="$description" \
		release "$dist" > "$dist/$release"
	rm -f "$dist/$release.gpg"
	gpg -abs -o "$dist/$release.gpg" "$dist/$release"
	echo_bold "$release created."
else
	echo "$release unchanged."
fi

