#!/bin/sh

# Shellscript mit " chmod u+x dateiname " freigeben!

# Usage: mar_trl_check trlfile marfile   (filenames inclusive path)

# Function: Compares turnnames in marfile and trlfile


m=$2
t=$1

if test -r $m
 then :
 else
  echo "Error:" $m "is not a readable file!"
  exit 1
fi

if test -r $t
 then :
 else
  echo "Error:" $t "is not a readable file!"
  exit 1
fi



rumpf=`echo $m | sed 's/..*\/\([a-z0-9]*\).mar/\1/'`
marfile=`echo $m | sed 's/..*\/\([a-z0-9\.]*\)/\1/'`
trlfile=`echo $t | sed 's/..*\/\([a-z0-9\.]*\)/\1/'`
#echo $rumpf $marfile $trlfile

marturns=`egrep -e "${rumpf}" ${m} | wc -l`
trlturns=`egrep -e "^${rumpf}" ${t} | wc -l`
echo $marfile: $marturns "turns      " $trlfile: $trlturns turns

language=`echo $rumpf | sed 's/[0-9][0-9][0-9][ab][tcr]//'`

if test $language = "m"
 then
  # multilingual
  tnamemar=`cat ${m} | awk '{printf("%s\n", $3)}' | sed 's/_[GJE][EAN][RPG]$//' | sort`
  tnametrl=`cat ${t} | grep "^${rumpf}" | awk '{printf("%s\n", $1)}' | sed 's/_[0-9][0-9][0-9][0-9][0-9][0-9]://' | sort`

  if test "$tnamemar" = "$tnametrl" 
   then
    :
   else
    echo "Error in mar- or trl-file!"
  fi


 else 
  # not multilingual 
  tnamemar=`cat ${m} | awk '{printf("%s\n", $3)}' | sort`
  tnametrl=`cat ${t} | grep "^${rumpf}" | awk '{printf("%s\n", $1)}' | sed 's/_[0-9][0-9][0-9][0-9][0-9][0-9]://' | sort`

  if test "$tnamemar" = "$tnametrl" 
   then
    :
   else
    echo "Error in mar- or trl-file!"
  fi
fi 
