#!/bin/bash
#
# 2019/12/13 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/secdapt-XXXXXX)
[ -n "${tmp_folder}" -a -d "${tmp_folder}" ] || exit 1

# Set Name and Version
PKG_NAME=secdapt
CODE_VERSION=$(grep '^VERSION=' secdapt | cut -f 2 -d "'") # '
PKG_VERSION=2

# Create future tree
mkdir -p ${tmp_folder}/usr/bin
mkdir -p ${tmp_folder}/usr/share/man/man1
mkdir -p ${tmp_folder}/usr/share/secdapt
mkdir -p ${tmp_folder}/etc/bash_completion.d
mkdir -p ${tmp_folder}/etc/sudoers.d
cp secdapt                 ${tmp_folder}/usr/bin/
cp secdapt.1.gz            ${tmp_folder}/usr/share/man/man1/
cp LICENSE.txt             ${tmp_folder}/usr/share/secdapt/
cp secdapt.bash_completion ${tmp_folder}/etc/bash_completion.d/
cp secdapt-sudoers         ${tmp_folder}/etc/sudoers.d/
chmod -R a+rx        ${tmp_folder}/usr/bin/secdapt
chmod -R a+rX,go-w   ${tmp_folder}/usr
chmod -R a+rX,go-w   ${tmp_folder}/etc
chmod    o-rwx       ${tmp_folder}/etc/sudoers.d/*

cp postinst postrm ${tmp_folder}/

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

# Control file
cat <<END > ${tmp_folder}/control
Package: ${PKG_NAME}
Version: ${CODE_VERSION}-${PKG_VERSION}
Section: admin
Tag: implemented-in::bash, interface::commandline, interface::text-mode, role::program
Priority: optional
Depends: dpkg, apt, coreutils, awk, grep, sudo
Suggests: aptitude
Architecture: all
Installed-Size: $(du -ks ${tmp_folder}|cut -f 1)
Maintainer: Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr>
Description: secure delegate apt upgrade
 secdapt can be use to launch upgrade apt command
 on remote host with a parallel tool like tssh, pconsole...
Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/secdapt
END

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

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

# Create package (control before data)
ar -r ${PKG_NAME}_${CODE_VERSION}-${PKG_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/${PKG_NAME}_${CODE_VERSION}-${PKG_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro includedeb jessie   ~/jessie/${PKG_NAME}_${CODE_VERSION}-${PKG_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro includedeb stretch ~/stretch/${PKG_NAME}_${CODE_VERSION}-${PKG_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro includedeb buster   ~/buster/${PKG_NAME}_${CODE_VERSION}-${PKG_VERSION}_all.deb )"
echo "( cd /srv/www/debian/ ; reprepro dumpreferences ) | grep -i secdapt"
