#!/bin/bash
#
# 2018/06/19 Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr>

trap '[ -n "${tmp_folder}" -a -d "${tmp_folder}" ] && rm -rf "${tmp_folder}"' QUIT

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
export LANG=C

tmp_folder=$(mktemp --directory /tmp/ddt-XXXXXX)
[ -n "${tmp_folder}" -a -d "${tmp_folder}" ] || exit 1

# Set Version
DDT_VERSION=$(grep 'version->declare' ddt | cut -f 2 -d "'") #'
LOCAL_VERSION=5

# Create future tree
mkdir -p ${tmp_folder}/usr/bin
mkdir -p ${tmp_folder}/usr/share/man/man1
mkdir -p ${tmp_folder}/usr/share/ddt
mkdir -p ${tmp_folder}/etc/bash_completion.d
cp ddt                  ${tmp_folder}/usr/bin/
cp ddt.1.gz             ${tmp_folder}/usr/share/man/man1/
cp LICENSE.txt          ${tmp_folder}/usr/share/ddt/
cp config.sample.yml    ${tmp_folder}/usr/share/ddt/
cp ddt.bash_completion  ${tmp_folder}/etc/bash_completion.d/
(cd ${tmp_folder}/usr/bin;            ln -s ddt      dhcp-dns-tools)
(cd ${tmp_folder}/usr/share/man/man1; ln -s ddt.1.gz dhcp-dns-tools.1.gz)

# Data archive
rm -f ${tmp_folder}/data.tar.gz
(cd ${tmp_folder}; tar czf data.tar.gz ./usr ./etc)

# Control file
cat <<END > ${tmp_folder}/control
Package: ddt
Version: ${DDT_VERSION}-${LOCAL_VERSION}
Section: utils
Tag: implemented-in::perl, interface::commandline, role::program
Priority: optional
Depends: perl, perl-base, perl-modules, libyaml-syck-perl, libnet-netmask-perl, libreadonly-perl, libfile-touch-perl
Suggests: libyaml-shell-perl | yamllint, perl-doc, isc-dhcp-server, bind9
Architecture: all
Installed-Size: $(du -ks ${tmp_folder}|cut -f 1)
Maintainer: Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr>
Description: dhcp-dns-tools management of computer names and IP addresses
 DDT is an acronym for DHCP-DNS-Tools.
 In practise, DDT is an IP Address Management (IPAM) service.
 It has been used in the LEGI laboratory for over 10 years.
 ddt (dhcp-dns-tools) is a small tool to maintain a set of computers/IP.
 In order to help you in this task, ddt command has a set of action
 to generated DHCP and DNS configuration files.
 .
 The tool is quite effective and tries to keep things simple
 but easily configurable for your site like a swiss army knife.
 Everything is saved in a YAML database
 and entries could be added, deleted, or modified by the command line.
Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/DDT
END

# Control archive
rm -f ${tmp_folder}/control.tar.gz
(cd ${tmp_folder}; tar czf control.tar.gz control)

# Format deb package
echo 2.0 > ${tmp_folder}/debian-binary

# Create package (control before data)
ar -r ddt_${DDT_VERSION}-${LOCAL_VERSION}_all.deb ${tmp_folder}/debian-binary ${tmp_folder}/control.tar.gz ${tmp_folder}/data.tar.gz
      
# Clean
rm -rf ${tmp_folder}

# Prepare reprepro upload
echo "# Prepare reprepro upload"
echo "( cd /srv/www/debian/ ; reprepro includedeb wheezy   ~/wheezy/ddt_${DDT_VERSION}-${LOCAL_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro includedeb jessie   ~/jessie/ddt_${DDT_VERSION}-${LOCAL_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro includedeb stretch ~/stretch/ddt_${DDT_VERSION}-${LOCAL_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro dumpreferences ) | grep -i ddt"
