#!/bin/sh
# $Id: save-tin,v 1.7 2002/04/17 23:37:19 tom Exp $
#
# save the built-stuff from configuring & building tin, to make it simpler
# to do rebuilds
#
SRC=`pwd`
if test -d ../../save-tin
then
	DST=../../save-tin
else
	DST=../save-tin
fi

THIS=$SRC
THAT=`echo $THIS|sed -e s/tin//`
if test "$THIS" = "$THAT"
then
	echo '? cannot save '$SRC
	exit 1
fi

FILES="config.status include/autoconf.h"
if ( egrep '^s%@host_os@%.*%g' config.status >/dev/null 2>/dev/null )
then
	# autoconf 2.13
	GRPL='^s%@'
	GRPR='@%.*%g'
	SED1='s/s%@'
	SED2='@%//'
	SED3='s/%g//'
	FILES="$FILES config.cache"
elif ( egrep '^s,@host_os@,.*,;t' config.status >/dev/null 2>/dev/null )
then
	# autoconf 2.52
	GRPL='^s,@'
	GRPR='@,.*,;t'
	SED1='s/s,@'
	SED2='@,//'
	SED3='s/,;t t//'
else
	echo 'Cannot parse config.status'
	exit 1
fi

for f in config.status include/autoconf.h src/Makefile
do
	if test ! -f $f
	then
		echo '? nothing to save: '$f
		exit 1
	fi
done

if test ! -d $DST
then
	mkdir $DST
fi

cd $SRC

#Host=`egrep '^# on host .*:$' config.status | sed -e 's/# on host //' -e 's/://'`
Host=`egrep "${GRPL}host_os${GRPR}" config.status | sed -e "${SED1}host_os${SED2}" -e "${SED3}" -e 's/ //g'`
Comp=`egrep "${GRPL}CC${GRPR}"      config.status | sed -e "${SED1}CC${SED2}"      -e "${SED3}"`
Libs=`egrep "${GRPL}LIBS${GRPR}"    config.status | sed -e "${SED1}LIBS${SED2}"    -e "${SED3}"`
if ( egrep '^#define[ 	]*USE_CURSES[ 	]*1' include/autoconf.h 2>&1 >/dev/null )
then
	Conf=C
else
	Conf=T
fi

case "$Libs" in
*ncurses*)	Libs=ncurses;;
*termcap*)	Libs=termcap;;
*termlib*)	Libs=termlib;;
*curses*)	Libs=curses;;
*)			Libs=unknown;;
esac
OUT=${Host}-${Comp}-${Conf}-${Libs}

rm -f $DST/$OUT
echo Creating ${OUT}:

chmod u+wx $DST
rm -f $DST/${OUT}.tgz
rm -rf $DST/$OUT

mkdir $DST/$OUT
test -f src/LINT && FILES="$FILES src/LINT"
tar cvf - $FILES *.out *.log */*.out */*.log src/Makefile |(cd $DST/$OUT; tar xf -)

cd $DST
archive $OUT
chmod -w .
