#!/bin/bash
#
# Script to create a debian package for SNFServer.
#
# Usage:  create_snf-server_debian X.Y.Z
#
# where X.Y.Z is the version of SNFServer.
#
# $Id: configure.in,v 1.33 2008/02/08 15:10:17 adeniz Exp $
#
# Author: Alban Deniz
#
# Copyright (C) 2013 by MicroNeil Corporation. All rights reserved.
# See www.armresearch.com for the copyright terms.
#
#######################################################################

VERSION=$1

# Output message and exit.
error_exit() {
    echo Error running \'$1\' in directory $(pwd).
    exit 1
}

# Create the tarball distribution.
make dist
if [ $? -ne 0 ]
then
    error_exit "make dist"
fi

# Prepare the directory.
rm -rf snf-server-$VERSION
if [ $? -ne 0 ]
then
    error_exit "rm -rf snf-server-$VERSION"
fi

tar xvzf snf-server-$VERSION.tar.gz
if [ $? -ne 0 ]
then
    error_exit "tar xvzf snf-server-$VERSION.tar.gz"
fi

cd snf-server-$VERSION
if [ $? -ne 0 ]
then
    error_exit "cd snf-server-$VERSION"
fi

# Initialize the package.
echo | dh_make --single --copyright artistic -f ../snf-server-$VERSION.tar.gz
if [ $? -ne 0 ]
then
    error_exit "echo | dh_make --single --copyright artistic -f ../snf-server-$VERSION.tar.gz"
fi

# Remove unneeded directory.
mv debian debian.orig
if [ $? -ne 0 ]
then
    error_exit "mv debian debian.orig"
    exit $ERROR_EXIT
fi

# Copy package files.
cp --archive ../Packages/Ubuntu/snf-server debian
if [ $? -ne 0 ]
then
    error_exit "cp --archive ../Packages/Ubuntu/snf-server debian"
    exit $ERROR_EXIT
fi

# Build the source package.
debuild -S -us -uc -rfakeroot
if [ $? -ne 0 ]
then
    error_exit "debuild -S -us -uc -rfakeroot"
fi

# Build the binary package.
dpkg-buildpackage -rfakeroot -us -uc
if [ $? -ne 0 ]
then
    error_exit "dpkg-buildpackage -rfakeroot -us -uc"
fi

# Success.
exit 0


