#!/bin/bash
#
# A script to build Debian packages from Voikko's Git repository.
#
# Author: Teemu Likonen <tlikonen@iki.fi>
#
# This script is placed in the public domain.

component=$1
ref_src=${2:-HEAD}
ref_deb=${3:-HEAD}

cmd_line="$(basename -- "$0") $*"

make_url() {
	local component=$1
	local ref=$2
	printf 'https://github.com/voikko/%s/archive/%s.tar.gz' "$component" "$ref"
}

debs() {
	local package not_installed
	local i=0
	for package in "$@"; do
		if ! dpkg --list "$package" 2>/dev/null | grep -q ^ii; then
			not_installed[i++]=$package
		fi
	done
	if [ "$not_installed" ]; then
		echo "ERROR: The following packages are required:"
		printf '  %s\n' "${not_installed[@]}"
		exit 1
	fi
}

get_remote() {
	# $1 = Repository in Github.
	# $2 = The Git ref to download.
	# $3 = File pattern for extracting the needed files from tar archive.
	# $4 = Number of leading directory compotents to strip from tar'ed
	#      files.
	# $5 = Destination directory for extracting.
	local repo=$1
	local ref=$2
	local pattern=$3
	local strip=$4
	local dest_dir=$5
	local url=$(make_url "$repo" "$ref")
	mkdir -p -- "$dest_dir"
	cd "$dest_dir" || exit 1
	wget -O - -- "$url" | \
		tar --extract --gzip --wildcards \
		--no-wildcards-match-slash \
		--strip="$strip" -- "$pattern"
	cd - >/dev/null
}

update_changelog() {
	local source=$(dpkg-parsechangelog | awk '/^Source:/ {print $2}')
	local stamp=$(date +%s)
	local version=99.dev.$(date --utc --date="@$stamp" +%Y%m%dT%H%M%SZ)
	local changelog_new=$(mktemp) || exit 1

	printf "%s (%s-1) unstable; urgency=low\n\n" \
		"$source" "$version" > "$changelog_new"
	printf "  * A development snapshot. Built with command line\n" \
		>> "$changelog_new"
	printf "    \"%s\".\n\n" "$cmd_line" >> "$changelog_new"
	printf " -- %s <%s>  %s\n\n" \
		"${DEBFULLNAME:-Nobody}" "${DEBEMAIL:-nobody@invalid}" \
		"$(date -R --date="@$stamp")" >> "$changelog_new"
	cat debian/changelog >> "$changelog_new"
	mv -f -- "$changelog_new" debian/changelog
}

build_deb() {
	update_changelog
	dpkg-checkbuilddeps || exit 1
	fakeroot debian/rules clean || exit 1
	fakeroot debian/rules binary || exit 1
	echo
	dpkg-parsechangelog
}

libvoikko() {
	local ref_src=$1
	local ref_deb=$2
	debs libtool automake hfst-ospell-dev pkg-config gettext dh-autoreconf dh-python
	get_remote corevoikko "$ref_src" "*/libvoikko/*" 2 libvoikko
	get_remote debian-packages "$ref_deb" \
		"*/libvoikko+debian/*" 2 libvoikko/debian
	cd libvoikko && ./autogen.sh && build_deb
}

foma() {
	local ref_src=$1
	local ref_deb=$2
	debs libreadline-gplv2-dev libtool automake bison flex
	wget -O - -- https://github.com/hatapitk/foma/archive/autotools2.tar.gz | \
		tar --extract --gzip --wildcards \
		--no-wildcards-match-slash \
		--strip="1"
	get_remote debian-packages "$ref_deb" \
		"*/foma+debian/*" 2 foma/debian
	cd foma && autoreconf --force --install && build_deb
}

mozvoikko() {
	local ref_src=$1
	local ref_deb=$2
	get_remote mozvoikko "$ref_src" "*" 1 mozvoikko
	get_remote debian-packages "$ref_deb" \
		"*/mozvoikko+debian/*" 2 mozvoikko/debian
	cd mozvoikko && build_deb
}

voikko-fi() {
	local ref_src=$1
	local ref_deb=$2
	debs foma-bin libvoikko-dev
	get_remote corevoikko "$ref_src" "*/voikko-fi/*" 2 voikko-fi
	get_remote debian-packages "$ref_deb" \
		"*/voikko-fi+debian/*" 2 voikko-fi/debian
	cd voikko-fi && build_deb
}

libreoffice-voikko () {
	local ref_src=$1
	local ref_deb=$2
	get_remote libreoffice-voikko "$ref_src" "*" 1 libreoffice-voikko
	get_remote debian-packages "$ref_deb" \
		"*/libreoffice-voikko+debian/*" 2 libreoffice-voikko/debian
	cd libreoffice-voikko && build_deb
}

print_usage() {
	local name=$(basename -- "$0")
	cat <<EOF
$name - Debian build tool for Voikko components

Description: This tool obtains Voikko's source code from pre-defined
remote location and creates a ready-to-install .deb package from it. The
.deb package as well as the building directory is created under the
current directory.

Usage: $name <component> [source ref] [debian ref]

  <component>  = voikko-fi | libvoikko | libreoffice-voikko | mozvoikko

  [source ref] = A reference to the commit of the source code to be
                 downloaded. It must be a valid Git commit reference
                 (examples: branch, tag, 5af3d232, HEAD~4). The
                 default is HEAD.

  [debian ref] = Like above but a reference to the debian-packages
                 repository. The default is HEAD.

EOF
}

case "$component" in
libvoikko|foma|libreoffice-voikko|voikko-fi|mozvoikko)
	debs build-essential fakeroot wget debhelper python3
	"$component" "$ref_src" "$ref_deb"
	;;
*)
	print_usage
	exit 2
	;;
esac