#!/bin/sh

# Paul Gevers <elbrus@debian.org>, 2016
# Graham Inggs <ginggs@debian.org>, 2017

# Stop if an error occurs
set -e

# Use the same flags as the package build
export DEB_CFLAGS_MAINT_APPEND="-D_FILE_OFFSET_BITS=64 -std=gnu17"
export DEB_BUILD_MAINT_OPTIONS="hardening=+all"

# Patch the build tree to only build tools and demos
patch -p1 -i debian/tests/only-tools-and-demos.patch

# remove generated file (see upstream bug #1651)
rm -f lib/Xm/Xm.h

# copy files not distributed in libmotif-dev
# MrmWidget.h is needed by wmldbcreate.c
cp lib/Mrm/MrmWidget.h tools/wml/
cp lib/Mrm/MrmWidget.h demos/lib/Exm/wml/
# UniqueEvnI.h is needed by MenuB.c
cp lib/Xm/UniqueEvnI.h demos/lib/Exm/
# XmI.h is needed by getsubres.c and Container.c
cp lib/Xm/XmI.h demos/programs/getsubres/
cp lib/Xm/XmI.h demos/unsupported/Exm/
# XmStrDefsI.h is needed by XmI.h
# autogenerated in lib/Xm/
cp debian/tests/XmStrDefsI.h demos/programs/getsubres/
cp debian/tests/XmStrDefsI.h demos/unsupported/Exm/
# XmTabListI.h is needed by Container.c
cp lib/Xm/XmTabListI.h demos/unsupported/Exm/

dh_autoreconf --as-needed
dh_auto_configure

dh_auto_build

# Clean up after succesful build
dh_auto_clean
dh_autoreconf_clean
dh_clean
rm -f demos/unsupported/Exm/XmTabListI.h
rm -f demos/unsupported/Exm/XmStrDefsI.h
rm -f demos/programs/getsubres/XmStrDefsI.h
rm -f demos/unsupported/Exm/XmI.h
rm -f demos/programs/getsubres/XmI.h
rm -f demos/lib/Exm/UniqueEvnI.h
rm -f demos/lib/Exm/wml/MrmWidget.h
rm -f tools/wml/MrmWidget.h
patch -p1 --force --reverse -i debian/tests/only-tools-and-demos.patch

# Shouldn't this always be cleaned up?
find . -name Makefile -delete
rm include/config.h include/stamp-h1 lib/Xm/xmstring.list
