#!/bin/sh

# This script expects the following variables to be in the environment:
# DESTDIR
# LIBRESWANSRCDIR
# ARCH
# IPSECVERSION

# set -x

cd $DESTDIR 
rm -f *.tar.gz 

mkdir -p $LIBRESWANSRCDIR/packaging/ipkg/ipkg
cp $LIBRESWANSRCDIR/packaging/ipkg/debian-binary . 
cp $LIBRESWANSRCDIR/packaging/ipkg/conffiles . 

cat $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan.dist | sed s/VERSION/$IPSECVERSION/ |sed s/ARCH/$ARCH/ > $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan 

cp $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan control 

tar -czf ./control.tar.gz ./conffiles ./control  

tar -czf ./data.tar.gz ./* --exclude=control.tar.gz --exclude=conffiles --exclude=control --exclude=debian-binary 

tar -czf $LIBRESWANSRCDIR/packaging/ipkg/ipkg/libreswan-utils-$IPSECVERSION.$ARCH.ipk ./debian-binary ./control.tar.gz ./data.tar.gz  
mkdir -p $LIBRESWANSRCDIR/packaging/ipkg/kernel-module 
cd $LIBRESWANSRCDIR/packaging/ipkg/kernel-module 

rm -f *.tar.gz 
cp $LIBRESWANSRCDIR/packaging/ipkg/debian-binary . 

cat $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan-module.dist | sed s/VERSION/$IPSECVERSION/ |sed s/ARCH/$ARCH/ > $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan-module 

cp $LIBRESWANSRCDIR/packaging/ipkg/control-libreswan-module control 

tar czf ./control.tar.gz ./control  

tar czf ./data.tar.gz * --exclude=control.tar.gz --exclude=control --exclude=debian-binary 

tar czf $LIBRESWANSRCDIR/packaging/ipkg/ipkg/libreswan-module-$IPSECVERSION.$ARCH.ipk ./debian-binary ./control.tar.gz ./data.tar.gz  
rm -rf $LIBRESWANSRCDIR/packaging/ipkg/ipkg/binaries/* 
rm -rf $LIBRESWANSRCDIR/packaging/ipkg/ipkg/kernel-module/*
