#! /bin/bash

# convert each PAR*/file.xyz to pdb, select one replica, filter through a
#    convenient program, sort by time
#
# syntax: x2p-prog-ord -p <prog> <xyz-file> <pdb-template> <replica-index>
#
#   options: 
#    -p   program to filter PDB data thru; must output time as first field

# ----------------------------------------------------------------------
# help: copy first comments block in this file
# ----------------------------------------------------------------------
function help() {
echo $0
name=`basename "$0"`;
echo "-->" $name;
sed -n "s/\$0/$name/;2,/^\$/p" $0; exit
}


# ----------------------------------------------------------------------
# init and defaults   
# ----------------------------------------------------------------------


# ----------------------------------------------------------------------
# command line options
# ----------------------------------------------------------------------
while getopts "p:dh-" Option
do
  case $Option in
    p     )  PROG=$OPTARG;;
    *     )    help;;   # help: copy first comments block
  esac
done

shift $(($OPTIND - 1))
#  Decrements the argument pointer so it points to next argument.
#  $1 now references the first non option item supplied on the command line
#+ if one exists.

# ----------------------------------------------------------------------
# command line arguments
# ----------------------------------------------------------------------
XYZ=$1
PDB_TEMPLATE=$2
REPLICA_INDEX=$3

# ----------------------------------------------------------------------
# read user's configuration
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# final parameters setting
# ----------------------------------------------------------------------

# ----------------------------------------------------------------------
# engine
# ----------------------------------------------------------------------

( for d in PAR00*; do cd $d; xyz2pdb $XYZ $PDB_TEMPLATE $REPLICA_INDEX; cd ..; done ) | $PROG  | sort -nk1

