#!/bin/sh
##########################################################
#
# makesrcdist	2.26
#
# Makes source distribution
#
# $1 - distribution name
#
##########################################################

echo "MakeSrcDist 2.26"
echo 

#### Make the source distribution for distributing the package

TAR=$1
echo "Making Source Distribution for $TAR"
if [ "$TAR" = "" ];then 
  echo "set variable TAR in Makefile.def first."
  exit
fi

if [ ${GZ:="gz"} = "gz" ]; then
  GZ="gz"
  GZIP=`which gzip`
  T=$?
else 
  T=1
fi

if [ $T = 1 ]; then
  GZ="Z"
  GZIP="compress"
fi

#### Check wether the lsm entry exists

if [ ! -r Makefile.lsm ]; then
  echo "You must make an lsm entry - Makefile.lsm"
  exit
fi

#### Now make the distribution

. .simplemake/Makefile.ver
VERSION=$SIMPLE_VERSION
#VERSION=`pwd | sed -e s%.*/%%`

echo "  creating $TAR-$VERSION.lsm"
echo "  creating $TAR-$VERSION.tar.$GZ"

rm -f $TAR-$VERSION.lsm $TAR-$VERSION.tar.gz $TAR-$VERSION.tar.Z

DATE=`date +%Y-%m-%d`
#cd ..
#mkdir $TAR
#tar cf $TAR.tar $VERSION
#(cd $TAR; tar xf ../$TAR.tar)
#rm -f $TAR.tar
#tar cf $TAR-$VERSION.tar $TAR
#rm -rf $TAR

mkdir ../tmptar
mkdir ../tmptar/$TAR
mkdir ../tmptar/$TAR/$VERSION
tar cf - . | (cd ../tmptar/$TAR/$VERSION; tar xf -)
PAD=`pwd`
(cd ../tmptar;tar cf $PAD/$TAR-$VERSION.tar $TAR)
rm -rf ../tmptar

$GZIP $TAR-$VERSION.tar

cp Makefile.lsm ./$TAR-$VERSION.lsm
LSMSIZE=`ls -l $TAR-$VERSION.lsm | awk '/.*/ { print $5 }'`
TARSIZE=`ls -l $TAR-$VERSION.tar.$GZ | awk '/.*/ { print $5 }'`
TARSIZE=`echo "scale=1;$TARSIZE/1024" | bc`

sed -e s/@VERSION@/$VERSION/g <Makefile.lsm | \
  sed -e s/@DATE@/$DATE/g | \
  sed -e s/@LSMSIZE@/$LSMSIZE/g | \
  sed -e s/@TARSIZE@/$TARSIZE/g | \
  sed -e s/@TAR@/$TAR/g \
  > $TAR-$VERSION.lsm

LSMSIZE=`ls -l $TAR-$VERSION.lsm | awk '/.*/ { print $5 }'`

sed -e s/@VERSION@/$VERSION/g <Makefile.lsm | \
  sed -e s/@DATE@/$DATE/g | \
  sed -e s/@LSMSIZE@/$LSMSIZE/g | \
  sed -e s/@TARSIZE@/$TARSIZE/g | \
  sed -e s/@TAR@/$TAR/g \
  > $TAR-$VERSION.lsm


echo "Putting the distribution in working directory"

#mv $TAR-$VERSION.lsm $VERSION
#mv $TAR-$VERSION.tar.$GZ $VERSION

echo "done."

