#!/bin/bash

# Substitue NEW, OLD as appropriate for your target.
NEW=${NEW:-build-ppc64/}
OLD=${OLD:-build-pre-ppc64/}

OUTP_NEW=${OUTP_NEW:-new_status.txt}
OUTP_OLD=${OUTP_OLD:-old_status.txt}

# Determine toolchain by peeking at config.make
# frail, but good enough.
NM=$(grep "NM = " ${NEW}/config.make)
OD=$(grep "OBJDUMP = " ${NEW}/config.make)
NM=${NM##NM = }
OD=${OD##OBJDUMP = }

# Generated object files g_*.o{,s}
OBJS="cabs creal conj carg cimag"

# types
TYPES="FLT DBL LDBL"
declare -A TYPE_SUF
TYPE_SUF[FLT]=f
TYPE_SUF[DBL]=
TYPE_SUF[LDBL]=l

# Dup stdout to 3 for informative nitting
exec 3>&1

for obj in $OBJS; do
for type in $TYPES; do
	# With the addition of generated files, all interesting
	# math objects have a prefix of some sort.
	if [ -e ${NEW}math/g_${obj}_${TYPE_SUF[$type]}.o ]; then
		obj_f=${NEW}math/g_${obj}_${TYPE_SUF[$type]}
	elif [ -e ${NEW}math/${obj}${TYPE_SUF[$type]}.o ]; then
		obj_f=${NEW}math/${obj}${TYPE_SUF[$type]}
	else
		echo "Error: Cannot determine objfile for ${obj} of type ${type}" >&2
		exit 1
	fi
	echo "looking at new ${obj_f}.*" >&3
	# fixup filenames to minimize diff noise
	# nm command might need tweaking to avoid spurious debug symbol noise.
	${NM} -S ${obj_f}.o ${obj_f}.os | sed -e "s|${obj_f}|${obj}_${type}|" | gawk 'NF != 3 {print}'
	${OD} -d ${obj_f}.o ${obj_f}.os | sed -e "s|${obj_f}|${obj}_${type}|"
done
done > ${OUTP_NEW}

for obj in $OBJS; do
for type in $TYPES; do
	# Similar to above, just for the existing changes
	if [ -e ${OLD}math/${obj}${TYPE_SUF[$type]}.o ]; then
		obj_f=${OLD}math/${obj}${TYPE_SUF[$type]}
	elif [ -e ${OLD}math/w_${obj}${TYPE_SUF[$type]}.o ]; then
		obj_f=${OLD}math/w_${obj}${TYPE_SUF[$type]}
	else
		echo "Error: Unknown old ojbfile ${obj} of type ${type}" >&2
		exit 1
	fi
	echo "looking at old ${obj_f}.*" >&3
	${NM} -S ${obj_f}.o ${obj_f}.os | sed -e "s|${obj_f}|${obj}_${type}|" | gawk 'NF != 3 {print}'
	${OD} -d ${obj_f}.o ${obj_f}.os | sed -e "s|${obj_f}|${obj}_${type}|"
done
done > ${OUTP_OLD}

echo
echo "Verbose output for old objects placed in ${OUTP_OLD}, new ${OUTP_NEW}"
echo "Difference between new and old objects should be trivial or non-existent"
echo "....."
diff -u ${OUTP_OLD} ${OUTP_NEW}
echo "....."

exec 3>&-
