#! /bin/bash

#
# syntax: x2p [-R { <replica> | "REM" }] <pdb-reference> <xyzfile> ...
#
#   this is just a wrapper for `xyz2pdb' allowing multiple xyz files

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


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



# ----------------------------------------------------------------------
# command line options
# ----------------------------------------------------------------------
while getopts "R:" Option
do
  case $Option in
    R     )  REPLICA_INDEX=$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
# ----------------------------------------------------------------------
PDB_TEMPLATE=$1
shift;   # now the rest is xyz file(s) 


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

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

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

for f in $*; do 
    cd `dirname $f`; 
    xyz2pdb `basename $f` $PDB_TEMPLATE $REPLICA_INDEX; 
    cd - 2>&1 1>/dev/null;
done
