#!/bin/sh

show_usage ()
{
  echo "*"
  echo "* * USAGE: $0 x y z [no_oem1] 'to pack the version x.y.z'"
  echo "*"
  exit
}

if [ ! $# = 3 -a ! $# = 4 ] ; then
  show_usage
fi

cd ..

if [ ! -d tslinux ] ; then
  echo "*"
  echo "* * Error: directory tslinux does not exist"
  echo "*"
  exit
fi

if [ $# = 4 ] ; then
  if [ "$4" = "no_oem1" ] ; then
    sh ./tslinux/rm_dg > exclude_files
	 sh ./tslinux/rm_others >> exclude_files
  else
    show_usage
  fi
else
	sh ./tslinux/rm_others > exclude_files
fi

# do the versioning

s1="$1_$2_$3"
s2="$1.$2.$3"
s3="$1$2$3"

# include versioning in the correct files.

#cp tslinux/hhl-tslinux.spec .
cp tslinux/cyclades_README_Zimage.txt .
#sed "s/CYCVERSION/$s1/;s/CYCVERSION/$s1/" < hhl-tslinux.spec > tslinux/hhl-tslinux.spec
sed "s/CYC_VERS/$s1/;s/CYC_VERS/$s1/;s/CYCVERS/$s2/;s/CYCVERS/$s2/" < cyclades_README_Zimage.txt > tslinux/cyclades_README_Zimage.txt

# build the rpm

#tar -czf /usr/src/redhat/SOURCES/hhl_tslinux-$s1.tar.gz -X exclude_files tslinux
tar -cf CDK.tar -X exclude_files tslinux
rm -f exclude_files
#ls -la /usr/src/redhat/SOURCES/hhl_tslinux-$s1.tar.gz

#rpm -ba --quiet tslinux/hhl-tslinux.spec
#ls -la /usr/src/redhat/RPMS/i386/hhl_tslinux-$s1-?.i386.rpm

# md5 the rpm
mv tslinux/cyclades_README_Zimage.txt /tftpboot/cyclades_README-$s1.txt
#mv /usr/src/redhat/RPMS/i386/hhl_tslinux-$s1-1.i386.rpm /tftpboot
cp tslinux/cyclades/upgrade_110  /tftpboot/upgrade_$s3
#md5sum /tftpboot/hhl_tslinux-$s1-1.i386.rpm > /tftpboot/hhl_tslinux-$s1-1.i386.rpm.md5
#mv hhl-tslinux.spec tslinux/hhl-tslinux.spec
mv cyclades_README_Zimage.txt tslinux/cyclades_README_Zimage.txt

# build the tars and md5s

cp CDK.tar CDK-$s1.tar
gzip CDK-$s1.tar
md5sum CDK-$s1.tar.gz >CDK-$s1.tar.gz.md5
mv CDK-$s1.tar.gz CDK-$s1.tar.gz.md5 /tftpboot

mv CDK.tar CDK-$s1.tar
bzip2 CDK-$s1.tar
md5sum CDK-$s1.tar.bz2 >CDK-$s1.tar.bz2.md5
mv CDK-$s1.tar.bz2 CDK-$s1.tar.bz2.md5 /tftpboot

