#!/bin/bash
#
# Target dist: Debian Jessie
DIST=jessie

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

# use old libfreeradius
sed -i 's/libradcli-dev/libfreeradius-client-dev/' ${DIST}/control
sed -i -e 's/RADCLI/FREERADIUS/' ${DIST}/rules

# old mysql
sed -i -e 's/default-mysql-client/mysql-client/' \
  -e 's/default-libmysqlclient-dev/libmysqlclient-dev/' ${DIST}/control
echo "kamailio source: build-depends-on-obsolete-package build-depends: libmysqlclient-dev => default-libmysqlclient-dev" >> \
	${DIST}/source.lintian-overrides
echo "kamailio-mysql-modules: depends-on-obsolete-package depends: mysql-client => default-mysql-client" >> \
	${DIST}/kamailio-mysql-modules.lintian-overrides

# No geoip2 module
sed -i -e '/libmaxminddb-dev/d' -e '/^Package: kamailio-geoip2-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += geoip2/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += geoip2' ${DIST}/rules

# No phonenum module
sed -i -e '/libphonenumber-dev/d' -e '/^Package: kamailio-phonenum-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += phonenum/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += phonenum' ${DIST}/rules

# Mo mongodb
sed -i -e '/libmongoc-dev/d' -e '/^Package: kamailio-mongodb-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += mongodb/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += mongodb' ${DIST}/rules

# No secsipid module
sed -i -e '/libsecsipid-dev/d' -e '/^Package: kamailio-secsipid-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += secsipid/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += secsipid' ${DIST}/rules

# libwebsockets >= 3.1
sed -i -e '/libwebsockets-dev/d' -e '/^Package: kamailio-lwsc-modules/,/^$/d' \
	${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += lwsc/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += lwsc' ${DIST}/rules

# no libnats-dev
sed -i -e '/^ libnats-dev,/d' \
  -e '/^Package: kamailio-nats-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += nats/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += nats' ${DIST}/rules

# no libpcre2-dev
sed -i -e 's/libpcre2-dev/libpcre3-dev/' ${DIST}/control

# libwolfssl-dev < 5.2
sed -i -e '/^ libwolfssl-dev,/d' \
  -e '/^Package: kamailio-wolftls-modules/,/^$/d' \
  ${DIST}/control
sed -i -e '/^PACKAGE_GROUPS += tls_wolfssl/d' ${DIST}/rules
sed -i -e '/--EXCLUDED--/i EXTRA_EXCLUDED_MODULES += tls_wolfssl' ${DIST}/rules

wrap-and-sort -sat -d ${DIST}

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