#!/bin/bash
#
# build-client.sh
#
# Copyright (C) 2011-2012 Cesnet z.s.p.o
#
# Use of this source is governed by a BSD-style license, see LICENSE file.

VERSION="1.2"

#-------------------------------------------------------------------------------
#				FUNCTIONS
#-------------------------------------------------------------------------------
err()
{
  echo "FAILED!"
  cat $err
  rm -rf $err $package $tar $sig
  echo
  echo "Build of $package package FAILED!!!"
  exit 1
}


#-------------------------------------------------------------------------------
#				MAIN
#-------------------------------------------------------------------------------

# edit when you build new package
version="2.0.0-beta2"

package_name="warden-client"
package="${package_name}-${version}"
doc="${package}/${package_name}/doc"
etc="${package}/${package_name}/etc"
lib="${package}/${package_name}/lib"
var="${package}/${package_name}/var"
tar="${package}.tar.gz"
sig="${tar}.sig"
err="/tmp/${package}-err"

# make directory structure
echo "Building $package_name package version $version ..."
echo

echo -n "Building 'base' directory ... "
mkdir -p $package 2> $err || err
cp -R ../src/${package_name}/sh/* $package 2> $err || err
cp ../src/${package_name}/doc/CHANGELOG $package 2> $err || err
cp ../src/${package_name}/doc/INSTALL $package 2> $err || err
cp ../src/${package_name}/doc/LICENSE $package 2> $err || err
cp ../src/${package_name}/doc/README $package 2> $err || err
cp ../src/${package_name}/doc/README.cesnet $package 2> $err || err
echo "OK"


echo -n "Building '${doc}' directory ... "
mkdir -p $doc 2> $err || err
cp -R ../src/${package_name}/doc/* $doc 2> $err || err
echo "OK"


echo -n "Building '${etc}' directory ... "
mkdir -p $etc 2> $err || err
cp ../src/${package_name}/etc/package_version $etc 2> $err || err
echo "OK"


echo -n "Building '${lib}' directory ... "
mkdir -p $lib 2> $err || err
cp -R ../src/${package_name}/lib/* $lib 2> $err || err
echo "OK"


echo -n "Building '${var}' directory ... "
mkdir -p $var 2> $err || err
echo "OK"


# create tarball
echo -n "Creating $tar tarball ... "
tar czf $tar $package 2> $err || err
echo "OK"


# create sign of tarball
echo -n "Creating $sig file ... "
sha1sum $tar > $sig 2> $err || err
echo "OK"


echo
echo "Building of $package package was SUCCESSFULL"

# cleanup section
rm -rf $package $err

exit 0
