#!/bin/sh

# Shell script to extract data with certain classifications from a snapshot.
# 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.44 $


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

umask 077

# 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

# Do not cd to ${SCC_TMP} in case of a relative pathname for the class-file.

TMP_FILE=${SCC_TMP}/scc_base_$$;		export TMP_FILE

trap "rm -f ${TMP_FILE}" 0
trap "exit 2" 1 2 3 15

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

if [ ! -f ${1} ]
then
	echo "${ProgName}: file ${1} not found" >&2
	exit 2
fi

# Rebuild the class-file to an awk-script that filters lines with classifications
# that contain text out of the class-file.
# Ignore comment-lines, then escape all special-characters for 
# the regular expressions in the awk-script.
# The classifications will be used as regular expressions in the awk-script.
# Therefore we have to escape the meta-characters of the regular expressions and
# also the delimiter of the regular expressions. The full list is: /.[\(*+$|
sed	-e '/^#/d'		\
	-e 's@/@\\/@g'		\
	-e 's@\.@\\.@g'		\
	-e 's@\[@\\[@g'		\
	-e 's@\\@\\@g'		\
	-e 's@(@\\(@g'		\
	-e 's@*@\\*@g'		\
	-e 's@+@\\+@g'		\
	-e 's@\$@\\$@g'		\
	-e 's@|@\\|@g'		${1}		|
awk '{
		# Add fix to the classifications to ensure that
		# - only fixed data is extracted
		# - only matching toplevel classes are extracted
		prefix="fix:"
		if ( $0 ~ "^:" )
		{
			prefix="fix"
		}
		postfix=":"
		if ( $0 ~ ":$" )
		{
			postfix=""
		}
		printf( "/%s%s%s/	{ print; next }\n", prefix, $0, postfix );
	}' >${TMP_FILE}

# Get the data and limit the result to "fixed" data.
awk -F: -f ${TMP_FILE} -

exit 0
