#!/bin/sh

# t2np: convert a Tape/PVM tracefile to a PICL one
#
# Eric Maillet & Yves Arrouye, 1995
# 
# 181295, maillet: updated to support new format introduced in 0.9pl8

ENRLEVT=86
TAPESORT="sort +5n -6 +6n -7 +1n -2"
NPICLSORT="sort +2n -3 +0n -1 +1rn -2"

usage() {
    >&2 echo usage `basename $0` \[ -nomaster \] tapepvm-trace
    exit 1
}

if [ "$1" = "-nomaster" ]
then
    if [ ! -f "$2" ]
    then
	>&2 echo `basename $0`: could not find trace file \"$2\"
	exit 2
    fi
    master=" `grep '^'$ENRLEVT $2 | $TAPESORT | head -1 | awk '{print $3}'` "
    shift
fi

if [ $# != 1 ]
then
     usage
fi

if [ $1 != "-" -a ! -f $1 ]
then
    >&2 echo `basename $0`: could not find trace file \"$1\"
    exit 2
fi

if [ "$master" != "" ]
then
    grep -v "$master" $1 | $TAPESORT |
	tape2npicl | $NPICLSORT
else
    $TAPESORT $1 | tape2npicl | $NPICLSORT
fi

