#!/bin/sh
##########################################################
#
# makesrcdist	3.01
#
# Makes source distribution
#
# $1 - distribution name
# $* - distribution aliases
#
##########################################################

echo "MakeSrcDist 3.01"
echo 

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

TAR=$1
shift
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

for i in $*
do
  rm -f $i-$VERSION.lsm $i-$VERSION.tar.gz $i-$VERSION.tar.Z
done

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 }'`

TARALIAS=$1
if [ "$TARALIAS" = "" ]; then
  TARALIAS=$TAR
fi

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

#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 "Making distribution aliases"

for i in $*
do 
  echo "  $i-$VERSION.lsm"
  echo "  $i-$VERSION.tar.$GZ"
  sed -e s/Primary-site/Prim-site/g \
      -e s/Alternate-site/Primary-site/g \
      -e s/Prim-site/Alternate-site/g \
      <$TAR-$VERSION.lsm >$i-$VERSION.lsm
  cp $TAR-$VERSION.tar.$GZ $i-$VERSION.tar.$GZ
done

echo "done."

