#!/bin/bash
#
# Target dist: Debian Buster
DIST=buster

if [ ! -d ../../pkg/deb ] ; then
  echo "script needs to be executed at pkg/deb dir" >&2
  exit 1
fi

rm -rf ${DIST}
cp -r debian ${DIST}

# No libbcg729-dev package
sed -i -e '/libbcg729-dev/d' ${DIST}/control
# No liburing-dev package
sed -i -e '/liburing-dev/d' ${DIST}/control

# Update for buster debhelper
sed -i -e 's/debhelper-compat.*/debhelper-compat (= 12),/' ${DIST}/control
# Replace execute_* hooks with old-style overrides
sed -i -e 's/^execute_before_dh_auto_configure:/override_dh_auto_configure:/' ${DIST}/rules
sed -i -e 's/^execute_after_dh_installsystemd:/override_dh_installsystemd:\n\tdh_installsystemd/' ${DIST}/rules
sed -i -e 's/^execute_before_dh_auto_install-indep:/override_dh_auto_install-indep:/' ${DIST}/rules
sed -i -e '/^\tgzip -9 < README.md/a\\n\tdh_auto_install' ${DIST}/rules

# fix links
sed -i -e 's#${env:deb_systemdsystemunitdir}#/lib/systemd/system/#g' \
  ${DIST}/rtpengine-recording-daemon.links
sed -i -e 's#${env:deb_systemdsystemunitdir}#/lib/systemd/system/#g' \
  ${DIST}/rtpengine-daemon.links

# Add dwz override
cat >> ${DIST}/rules <<'EOF'

override_dh_dwz:
	# Disable, as dwz cannot cope with some of the plugins generated.
EOF

# dkms stuff (see MT#56627)
cp old-dkms/rtpengine-kernel-dkms.postinst "${DIST}"/
cp old-dkms/rtpengine-kernel-dkms.prerm "${DIST}"/
cp old-dkms/dkms.conf.in "${DIST}"/
rm -f "${DIST}"/rtpengine-kernel-dkms.install "${DIST}"/rtpengine-kernel-dkms.dkms
sed -i '/ngcp-rtpengine source:/d' "${DIST}"/source/lintian-overrides
sed -i '1n;/^Package: rtpengine-kernel-dkms/,/^Depends:/{ s/Depends:/Depends:\n dkms (>= 1.95),/;}' "${DIST}"/control
sed -i '/^ dh-sequence-dkms,/d' "${DIST}"/control
sed -i '/^Testsuite: autopkgtest-pkg-dkms/d' "${DIST}"/control
sed -i '/^override_dh_dkms/d' "${DIST}"/rules
sed -i '/dh_dkms /d' "${DIST}"/rules
sed -i '/^export DEB_VERSION_UPSTREAM/d' "${DIST}"/rules
sed -i '/^export DEB_SOURCE/d' "${DIST}"/rules
sed -i 's;^include /usr/share/dpkg/pkg-info.mk;include /usr/share/dpkg/pkg-info.mk\
## kernel package specific stuff\
# Name of the dkms package\
pdkms:=rtpengine-kernel-dkms\
# short upstream name, used for module source directory\
sname:=rtpengine\
# Source version\
sversion:=$(DEB_VERSION_UPSTREAM)\
## end of kernel package specific stuff\
;' "${DIST}"/rules
sed -i 's;^override_dh_auto_install-indep:;override_dh_auto_install-indep:\
\t# Create the directories to install the source into\
\tdh_installdirs -p$(pdkms)    usr/src/$(sname)-$(sversion)\
\t# Copy only the driver source to the proper locations\
\tcd kernel-module \&\& cp Makefile *.c *.h *.inc ../debian/$(pdkms)/usr/src/$(sname)-$(sversion)\
\t# Prepare dkms.conf from the dkms.conf.in template\
\tsed "s/__VERSION__/$(sversion)/g" debian/dkms.conf.in > debian/$(pdkms)/usr/src/$(sname)-$(sversion)/dkms.conf\
;' "${DIST}"/rules

if command -v wrap-and-sort &>/dev/null ; then
  wrap-and-sort -sat -d ${DIST}
else
  echo "WARN: wrap-and-sort (Debian package devscripts) not available."
fi

# clean backports scripts
rm -rf ${DIST}/backports
exit 0
