#!/bin/bash
#
#    $Id$
#    Copyright (C) 2010 Sternberg Astronomical Institute, MSU
#
#    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 3 of the License, 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.  If not, see <http://www.gnu.org/licenses/>.
#

AWK=/bin/awk

if [ ! -x $AWK ]
then
	echo "$AWK seems to be not installed." 1>&2
	exit 1
fi

if [ $# -lt 2 ]
then
	echo "Usage: `basename $0` MASSFILE STMFILE" 1>&2
	exit 1
fi

MASSFILE=$1
STMFILE=$2

if [ ! -e $MASSFILE -a "x$MASSFILE" != "x-" ]
then
	echo "$MASSFILE isn't readable or doesn't exist" 1>&2
	exit 1
fi

if [ ! -e $STMFILE -a "x$STMFILE" != "x-" ]
then
	echo "$STMFILE isn't readable or doesn't exist" 1>&2
	exit 1
fi

$AWK 'BEGIN {
	channel2bicounter_map["A"]="1"
	channel2bicounter_map["B"]="1"
	channel2bicounter_map["C"]="2"
	channel2bicounter_map["D"]="2"
	channel2counter_map["A"]="A"
	channel2counter_map["B"]="A"
	channel2counter_map["C"]="B"
	channel2counter_map["D"]="B"
	IGNORECASE = 1
}
{
	if( $1 == "P" ) {
		if( $0 ~ /Operations\\Background measurement\\Background/ ) {
			split( $0, background, /(,|=)/ )
			print "M",$2,$3,"Background measurement"
			print "m",$2,$3,background[2],background[3],background[4],background[5],0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
			next
		} else if( $0 ~ /Bicounters\\Channels\\Channel([ABCD])=Counter ([12])([AB])/ ) {
			match( $0, /Bicounters\\Channels\\Channel([ABCD])=Counter ([12])([AB])/, arr )
			channel2bicounter_map[arr[1]]=arr[2]
			channel2counter_map[arr[1]]=arr[3]
			print "P",$2,$3,"Channels/Configuration/Channel"arr[1]" = Counter "channel2bicounter_map[arr[1]]""channel2counter_map[arr[1]]
			next
		} else if( $0 ~ /Segmentator\\Channel ([ABCD])\\(Inner|Outer)/ ) {
			sub(/Segmentator/,"Optics",$0)
			sub(/Channel/,"Segment",$0)
		} else if( $0 ~/Channels\\Channel([ABCD])\\NonPoisson/ ) {
			match( $0, /Channels\\Channel([ABCD])\\NonPoisson/, arr)
			sub(/Channels\\Channel([ABCD])\\NonPoisson/,"Devices/Bicounter "channel2bicounter_map[arr[1]]"/NonPoisson"channel2counter_map[arr[1]])
			print "P",$2,$3,"Channels/Configuration/Channel"arr[1]" = Counter "channel2bicounter_map[arr[1]]""channel2counter_map[arr[1]]
		} else if( $0 ~/Channels\\Channel([ABCD])\\NonLinearity/ ) {
			match( $0, /Channels\\Channel([ABCD])\\NonLinearity/, arr)
			sub(/Channels\\Channel([ABCD])\\NonLinearity/,"Devices/Bicounter "channel2bicounter_map[arr[1]]"/NonLinearity"channel2counter_map[arr[1]])
			print "P",$2,$3,"Channels/Configuration/Channel"arr[1]" = Counter "channel2bicounter_map[arr[1]]""channel2counter_map[arr[1]]
		} else if( $0 ~/Channels\\Channel([ABCD])\\Scatter/ ) {
			match( $0, /Channels\\Channel([ABCD])\\Scatter/, arr)
			sub(/Channel([ABCD])\\Scatter/,"Scatter/Channel"arr[1])
		}
		gsub(/(\\)/,"/",$0)
		print $0
	} else if( $1 == "O" ) {
		print $1,$2,$3,$4,$5"_"$6,$7,$8,$9,$10,$11,$12,$14,$15,$13
	} else if( $1 == "M" ) {
		if( $4 == "Normal" ) {
			print $1,$2,$3,"Normal mode"
		} else {
			print $0
		}
	} else if( $1 == "m" ) {
		print $0
	}
}' $MASSFILE | sort --merge --stable --key=2,3 - $STMFILE

exit 0
