#!/bin/sh
# makedist - Copyright (C) 1998 by Guido Flohr <gufl0000@stud.uni-sb.de>.
# This file is part of the FreeMiNT distribution.  See the file
# copying in this directory for licensing details.
# 
# Make the source/binary distribution.  This file is intended for the
# maintainer and it may not work on your system.  Don't bother.

#set -x

LC_ALL="C"  # Avoid NLS nuisances.

# We have to be started from the current directory.
if test $0 != "./makedist"; then
  echo "$0 has to be run from the current directory."
  exit 1
fi

here=`pwd`
distdir=`basename $here`

here=`pwd`
distdir=`basename $here`

if test "x$1" = "x-bin"; then
    exclude_file="./exclude.bin"
    manifest="MANIFEST.bin";
    b="binary "
    equal_pad="=======" 
    bdist="-bin"
else
    exclude_file="./exclude" 
    manifest="MANIFEST"
fi

echo "Creating "$b"distribution for FreeMiNT."

tmpdir=$TMP
test x$tmpdir = x && tmpdir=$TEMP
test x$tmpdir = x && tmpdir=/tmp
test -d $tmpdir || tmpdir=..

# Extenders to exclude from the distribution as an extended
# regular expression like egrep likes them.
exclude_ext=`cat $exclude_file | tr -d ' ' | tr '\n' '|'`
exclude_ext='(^total|'"$exclude_ext"MANIFEST.*'$)'

echo "Creating ./$manifest"

echo "Contents of FreeMiNT ${b}distribution"         > $manifest
echo "==================================$equal_pad"  >>$manifest
echo "Generated "`date`"."                           >>$manifest
echo                                                 >>$manifest
echo ".:"                                            >>$manifest
ls -lRL . | grep -E -v $exclude_ext                  >>$manifest

echo "Running 'GZIP=--best tar -czhov --files-from=\"-\" -f $distdir$bdist.tar.gz'"

cd .. && \
    cat $distdir/$manifest | \
    awk 'BEGIN { dir = "'$distdir'"; subdir = ""; manifest="'$manifest'" }
      /^.*:$/ { 
          subdir = substr ($0, 1, length - 1)
        }
      /\.:$/ { subdir = "" }
      NF == 9 && /-[-r][-w][-xs][-r][-w][-sx][-r][-w][-sx].*/ { 
          printf ("%s/%s%s%s\n", dir, subdir, subdir == "" ? "" : "/", $9) 
        }
      END { printf ("%s/%s\n", dir, manifest) }' | \

    GZIP=--best tar -czhov --files-from="-" -f $distdir$bdist.tar.gz \
    && echo "Successfully created "$b"distribution file ../$distdir$bdist.tar.gz" \
    || echo "Creation of distribution file failed."
