#!/bin/sh
#
# Welcome to the sbox-dtc install maker for BSD!
# Maintainer: Thomas GOIRAND <thomas@goirand.fr>
# please do not ship with you packages
# this is only a small coder tool...
#

set -e
set -x

PKGNAME="sbox-dtc"

# Nothing to be configured after this line
VERS=`head -n 1 debian/changelog | cut -d'(' -f2 | cut -d')' -f1 | cut -d'-' -f1`
REL=`head -n 1 debian/changelog | cut -d'(' -f2 | cut -d')' -f1 | cut -d'-' -f2`
PKGNAME=`head -n 1 debian/changelog | cut -d' ' -f1`

VERSION=${VERS}.${REL}
DIRNAME=$PKGNAME"-"$VERSION

echo " --- Making sbox-dtc BSD package --- "
# Make the directories
rm -rf build
mkdir -p build
cd build
rm -fr $DIRNAME
mkdir -p $DIRNAME

# Copy DTC's files in the good directories
cp -rf ../create_chroot.sh ../Makefile ../README.html ../README.txt ../README.using_sbox.txt ../changelog ../copyright ../env.c ../sbox.h ../sbox.c ../sbox.conf $DIRNAME

# Make the archive
tar -czf ../$DIRNAME".tar.gz" $DIRNAME

# Make the port archive
mkdir $PKGNAME
cp -rf ../bsd/* $PKGNAME
rm -rf $PKGNAME/CVS

# Make the md5 sum file in this distinfo
BSDSUM=`md5sum "../"$PKGNAME"-"$VERSION".tar.gz" | cut -f1 -d" "`
SIZE=`ls -ALln "../"$PKGNAME"-"$VERSION".tar.gz" | awk '{print $5}'`
if [ -e /sbin/sha256 ] ; then
	SHA=`sha256 -r "../"$PKGNAME"-"$VERSION".tar.gz"| cut -f1 -d" "`
else
	SHA=`sha256sum "../"$PKGNAME"-"$VERSION".tar.gz" | cut -f1 -d" "`
fi
echo "MD5 ("$PKGNAME"-"$VERSION".tar.gz) = "${BSDSUM}"
SHA256 ("$PKGNAME"-"$VERSION".tar.gz) = "${SHA}"
SIZE ("$PKGNAME"-"$VERSION".tar.gz) = "$SIZE >>""$PKGNAME"/distinfo"

tar -cvzf ../$PKGNAME"BSDport-"$VERSION".tar.gz" $PKGNAME
cd ..
echo " -- Succesfully made BSD port "$DIRNAME".tar.gz and $PKGNAME"BSDport-"$VERSION".tar.gz" ---"
