#!/bin/bash

set -ue

echo "* Starting: $0"

echo "Setting up a fake package 'foo'"
mkdir elpa-foo
cat  > elpa-foo/foo.el <<EOF
;;; foo.el --- Fake package for testing dh-make-elpa  -*- lexical-binding: t; -*-
;; Copyright (C) 2023 by Richard Lewis <richard.lewis.debian@googlemail.com>
;; Copyright (C) 2023 by John Doe <john@doe.info>

;; Author: RL <rl@example.com>
;; Version: 1.2a
;; URL: https://example.com/home/page

;; foo 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 2 dated June, 1991.
;;
;; foo 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 foo.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; A fake package for testing dh-make-elpa

;;; Code:
(provide 'foo)
EOF

echo "** Running dh-make-elpa in: $PWD/elpa-foo"
cd elpa-foo
dh-make-elpa

echo "** Checking contents"

check(){
	local file="$2"
	local what="$1"
	local status=0
	output=$(grep "$what" "$file") || status=1
	if [ "$status" = "0" ]; then
		echo "** PASS: generated $file contains [$what]"
		echo "$output"
	else
		echo "* FAIL: generated $file did not contain [$what]" >&2
		cat "$file"
		STATUS=1
	fi
}

STATUS=0
check 'elpa-foo (1.2a-1) unstable; urgency=medium' debian/changelog
check ' * Initial release.' debian/changelog

check 'Source: elpa-foo' debian/control
check 'Section: editors' debian/control
check 'Priority: optional' debian/control
check 'Build-Depends: debhelper-compat (= ' debian/control
check ' dh-elpa' debian/control
check 'Standards-Version: ' debian/control
check 'Homepage: https://example.com/home/page' debian/control
check 'Package: elpa-foo' debian/control
check 'Architecture: all' debian/control
# shellcheck disable=SC2016
check 'Depends: ${elpa:Depends}, ${misc:Depends}' debian/control
check 'Description: Fake package for testing dh-make-elpa' debian/control
check ' A fake package for testing dh-make-elpa' debian/control

check 'Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/' debian/copyright
check 'Upstream-Name: elpa-foo' debian/copyright
check 'Source: https://example.com/home/page' debian/copyright

check 'Files: *' debian/copyright
check 'Copyright:' debian/copyright
check ' (C) 2023 by Richard Lewis <richard.lewis.debian@googlemail.com>' debian/copyright
check ' (C) 2023 by John Doe <john@doe.info>' debian/copyright
check 'License: GPL-2+' debian/copyright
check 'Files: debian/*' debian/copyright

check '*.el' debian/elpa

check '#!/usr/bin/make -f' debian/rules
check 'dh $@ --with elpa' debian/rules

check "3.0 (quilt)" debian/source/format


echo "* All done: exit=$STATUS"
echo "Generated files follow for info:"
ls -laR

shopt -s dotglob globstar
for x in ./**/*; do
	echo "$x"
	if [ -f "$x" ]; then
		cat "$x"
		echo
	fi
done

exit $STATUS
