#!/bin/sh

# Shell script to compare parts of 2 SCC snapshots.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004-2005 OpenEyeT Professional Services.
# Copyright (C) 2005-2009 QNH.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Contact information: http://scc.qnh-infrastructure.nl

# SCC-release:	1.7.5
# file-version:	$Revision: 1.49 $


ProgName=${0##*/};			export ProgName

set -u

SCC_BIN=/opt/scc-srv/bin
SCC_TOP=/var/opt/scc-srv
export SCC_BIN SCC_TOP

export SCC_DATA=${SCC_TOP}/data
export SCC_TMP=${SCC_TOP}/tmp
export SCC_CONF=${SCC_TOP}/conf

export TMPDIR=${SCC_TMP}
export TMP=${SCC_TMP}

export SHELL=/bin/sh
export LANG=C

PATH=/sbin:/usr/sbin:/usr/bin:/bin:${SCC_BIN};	export PATH

# Use the following utilities for SunOS.
if [ -x /usr/ccs/bin/what ]
then
	alias what=/usr/ccs/bin/what
fi
if [ -x /usr/xpg4/bin/awk ]
then
	alias awk=/usr/xpg4/bin/awk
fi
if [ -x /usr/xpg4/bin/grep ]
then
	alias grep=/usr/xpg4/bin/grep
fi

umask 077

# Do not cd to ${SCC_TMP} in case of relative pathnames for the snapshots.

logfile=""
if [ "x${1}" = "x-l" ]
then
	logfile="${2}"
	shift 2
fi

if [ $# -ne 3 ]
then
	echo "Syntax error, use: ${ProgName} [ -l <log> ] <class> <snapshot-1> <snapshot-2>" >&2
	exit 1
fi

if [ ! -s "${2}" -o ! -s "${3}" ]
then
	echo "${ProgName}: non-existing or empty file(s): ${2} ${3}" >&2
	exit 2
fi

host1=${2%.*}
host1=${host1#*.}

host2=${3%.*}
host2=${host2#*.}

if [ "x${host1}" = "x" -o "x${host2}" = "x" ]
then
	echo "${ProgName}: hostnames not found in snapshots" >&2
	exit 2
fi

trap "rm -f ${SCC_TMP}/${host1}.base ${SCC_TMP}/${host2}.base" 0
trap "exit 2" 1 2 3 15

# Extract for both snapshots the baseline according to the classfile.
${SCC_BIN}/scc-baseline ${1} <${2} >${SCC_TMP}/${host1}.base
${SCC_BIN}/scc-baseline ${1} <${3} >${SCC_TMP}/${host2}.base

if [ "${logfile}" ]
then
	wc -l <${SCC_TMP}/${host1}.base >"${logfile}"
	wc -l <${SCC_TMP}/${host2}.base >>"${logfile}"
fi

# Determine the longest name of both hosts.
m=$(echo "${host1} ${host2}"	|
	awk '{
		m1 = length( $1 );
		m2 = length( $2 );
		if ( m1 > m2 )
		{
			print m1;
		}
		else
		{
			print m2;
		}
	}' )

label1=$(printf "%-${m}s:" ${host1})
label2=$(printf "%-${m}s:" ${host2})

diff ${SCC_TMP}/${host1}.base ${SCC_TMP}/${host2}.base		|
sed	-e '/^[0-9\-]/d'		\
	-e "s/< fix:/${label1}   /"	\
	-e "s/> fix:/${label2}   /"

exit 0
