#!/bin/sh
#
# $Id: //droopi/main/utils/make_distrib#27 $
#
# This script builds a compressed archive suitable for distribution.
#
# Usage: make_distrib [-Dn] [-svn] [-b branch] dir
#
#   -D    : build and package documentation
#   -n    : assume a checkout has already been done in dir
#   -svn  : use Subversion to extract files
#   -b branch : use that CM branch
#   dir   : the distribution will unpack in directory <dir> and will be
#           named <dir>.tar.gz
#

subversion=false
nocheckout=false
prepare_distrib_options=

###################################################
# Usage information
###################################################

usage() {
   echo "Usage: $0 [-Dks] [-svn] [-bBRANCH] -[ncs] tag dir"
   exit 1
}

set -e

###################################################
# Parse commande line
###################################################

while getopts Dns:b: opt; do
  case "$opt" in
    D) prepare_distrib_options="${prepare_distrib_options} -D" ;;
    s)
      case "s$OPTARG" in
        svn) subversion=true ;;
      esac
      ;;
    n) nocheckout=true;;
    b) branch="$OPTARG" ;;
    *) usage ;;
  esac
done

shift `expr $OPTIND - 1`

if [ $# != 1 ]; then
  usage
fi

dir=$1

###################################################
# Prepare temporary directory
###################################################

prev=`pwd`
tmp=${TMPDIR-/var/tmp}/make_distrib.$$
mkdir -p ${tmp}/${dir}
trap "cd /; rm -fr ${tmp}" 0

###################################################
# Checkout from repository, if required
###################################################

# Subversion

if $subversion; then
  # Tag is always ignored

  if [ "${branch}" != "" ]; then
    case ${branch} in
      */global/*)
        view_root=${branch}/polyorb
        ;;
      *)
        view_root=${branch}
        ;;
    esac

  else
    view_root=/trunk/polyorb
  fi

  cd ${tmp}/${dir}
  tmp=`/bin/pwd`
  set +e
  svn checkout svn+ssh://svn.eu.adacore.com/Dev${view_root} .
  rc=$?
  set -e

  if [ $rc != 0 ]; then
    exit $rc
  fi

  # Get last change for this tree
  prepare_distrib_options="${prepare_distrib_options} -c "`svn log --limit 1 | sed -n '2s/^r\([0-9]*\) .*$/\1/p'`

  cd ..

# Do no checkout

elif $nocheckout; then
  # Edits will be done in place directly in this case
  # Note that this assumes that the specified directory contains a
  # clean checkout of the repository.

  : Nothing to do

else
  echo "Specify either -svn or -n"
  exit 1
fi

${dir}/utils/prepare_distrib $prepare_distrib_options $dir

###################################################
# Packaging
###################################################

echo Packaging

local_filelist=${tmp}/filelist
rm -f ${local_filelist}

for f in `cat ${dir}/MANIFEST`; do
  if [ ! -f ${dir}/${f} ]; then
    echo "FATAL: ${dir}/${f} is not a regular file."
    exit 1
  fi
  echo ${dir}/${f} >> ${local_filelist}
done

# Check for GNU tar
tar --version 2> /dev/null | grep "GNU tar" > /dev/null && \
GTAR_OPTS=--portability

# Create package
tar ${GTAR_OPTS} -cf ${dir}.tar -T ${local_filelist}
gzip --best ${dir}.tar

ls -l ${dir}.tar.gz

if [ "`pwd`" != "${prev}" ]; then
  mv ${dir}.tar.gz ${prev}
  cd ${prev}
fi

rm -fr ${tmp}
