#!/bin/sh
# $Id: dlg-symbols,v 1.6 2016/01/26 21:37:57 tom Exp $
# -----------------------------------------------------------------------------
# Copyright 2015,2016 by Thomas E. Dickey
#
#                         All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
# extract symbols from a given patch-date for dialog

failed() {
	echo "$*" 1>&2
	exit 1
}

get_symbols() {
	clean-dialog
	RCS-toggle >/dev/null
	CFG_OPTS="\
		--with-libtool \
		--enable-trace \
		--disable-leaks \
		--enable-rpath"

	MAP_OPTS="-v"

	echo "** BUILD $* $CFG_OPTS" | sed -e 's/[[:space:]][[:space:]]*/ /g'
	if cfg-shared $* $CFG_OPTS
	then
		make
		RC=$?
		echo "DONE $RC"
		if test $RC != 0
		then
			RCS-toggle >/dev/null
			exit 1
		fi

		echo "MAPSYMS $MAP_OPTS"
		ncu-mapsyms $MAP_OPTS # -m

		for CMP in package/$DATE-*.map package/$DATE-*.sym
		do
			case $CMP in
			package/$DATE-[0-9]*)
				continue
				;;
			esac

			REF=$CMP-ref

			if test ! -f $REF
			then
				cp -vp $CMP $REF
			elif cmp -s $CMP $REF
			then
				echo "** map-file unchanged: $CMP"
			else
				echo "** map-file changed: $CMP"
				case $CMP in 
				*.map)
					TST=`compare-mapsyms -s $REF $CMP`
					case "$TST" in
					*\(*)
						echo $TST
						;;
					*)
						echo "?? map-file comments: $CMP"
						diff -u $REF $CMP
						;;
					esac
					;;
				*.sym)
					diff -u $REF $CMP |diffstat
					;;
				esac
				cp -vp $CMP $REF
			fi
		done 
	fi
	unset LIBS
	RCS-toggle >/dev/null
}

get_normal() {
	get_symbols $*
}

zap_tree() {
	clean-dialog

	rm -f VERSION configure *.[ch]
	rcsget -d -q -r$1 || exit
	test -f VERSION  || exit
	test -f configure || autoconf-213
}

get_date() {
	DATE=`head -n 1 VERSION | awk '{ if ( NF == 1 ) { print $1 } else { print $3 } }'`
	test -n "$DATE" || failed "no patch-date found"
}

build_tree() {
	clean-dialog

	get_date
	rm -f package/$DATE-*.map* package/$DATE-*.sym*

	if test -f VERSION
	then
		get_normal
		get_normal --enable-widec
	fi
}

save_symbols() {
	echo "** save_symbols($DATE)"
	for SAVE in package/$DATE-*.map
	do
		sed -i s/$PATCHED/current/ $SAVE
	done

	for SAVE in package/$DATE-*.map package/$DATE-*.sym
	do
		cp -vp $SAVE package/SAVE/
		mv -vf $SAVE package/`basename $SAVE|sed -e s/^$DATE-//`
	done
}

usage() {
	cat >&2 <<EOF
Usage: $0 [-a] [label]

Options:
  -a	process all releases to rebuild .map/.sym files
EOF
	exit 1
}

OPT_A=no

set -- `getopt 'ah' $*`
if [ $? != 0 ]
then
	eval $0 -h
	exit 1
fi

while [ $# != 0 ]
do
	opt=$1
	shift
	case $opt in
	-a)
		OPT_A=yes;;
	-h)
		usage;;
	--)
		break;;
	esac
done


if test $# = 1
then
	rm -vf package/[1-9]*-*.sym package/[1-9]*-*.map
	zap_tree $1
	build_tree
elif test $# != 0
then
	usage
elif test $OPT_A = yes
then
	rm -f package/*.sym package/*.map
	rm -rf package/SAVE
	mkdir -p package/SAVE

	CURRENT=`rlog VERSION | \
		sed \
			-e '1,/^symbolic names:$/d' \
			-e '/^keyword substitution/,999999d' \
			-e 's/^	//' \
			-e 's/:.*//' \
			-e '/^[^t]/d' | \
		sort -r 2>/dev/null | \
		head -n 1`
	PATCHED=`echo "$CURRENT" | sed -e 's/^.*_//'`
	echo "** Current $CURRENT"
	echo "** Patched $PATCHED"

	for TAG in \
		v1_0 \
		v1_1 \
		v1_2 \
		v1_3 \
		$CURRENT
	do
		zap_tree $TAG
		build_tree
		save_symbols
	done
else
	build_tree
fi
