#! /bin/bash -e

# makedeb - create a Debian archive to contain a number of config files
#
#    Copyright (C) 2003 Kai Henningsen <kai@debian.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

umask 077

ca="$1"
shift
maint="$1"
shift

deb=$1
shift

name=$( basename $deb .deb )
date=$( find "$@" -type f -printf "%TY-%Tm-%Td %TH:%TM:%TS\n" |
	env - sort | tail -n 1 )
date1=$( echo $date | tr -- " :" "--" )
date2=$( date -R -d "$date" )

rm -rf tmp
mkdir -p tmp tmp/openvpn tmp/debian

cp -a "$@" tmp/openvpn

( : enter build dir; cd tmp

cat > debian/changelog	<<EO1
$name ($date1) unstable; urgency=low

  * This changelog exists only to satisfy the build tools.
    Nothing to see here, move on!

 -- $maint  $date2

EO1
cat > debian/compat		<<EO2
4
EO2
cat > debian/control	<<EO3
Source: $name
Maintainer: $maint

Package: $name
Architecture: all
Depends: openvpn, tunneldigger-utils
Description: $ca OpenVPN configs for host $name
 This package was automatically generated by tunneldigger
 on $date.
 Contact $maint for more information.
EO3
cat > debian/rules		<<EO4
#! /bin/sh -e
dh_testdir
dh_testroot
dh_clean -k 
dh_installdirs
dh_install openvpn /etc/
dh_fixperms
find -name "*.key" | xargs chmod 400
dh_installdeb
dh_gencontrol
dh_md5sums
dh_builddeb
EO4
chmod +x debian/rules

fakeroot debian/rules binary

: leave build dir )

mv -fv ${name}_${date1}_all.deb $deb
chmod 400 $deb
rm -rf tmp

