#!/bin/bash
atatdir=$(sed 's/.*=\(.*\)/\1/g' ~/.atat.rc)

if [[ ! -e ${atatdir}/data/prototypes.in ]]
then
  echo Prototype database not found.
  echo Either your ~/.atat.rc file in not properly configured or you were not given access to the prototype database.
  echo Contact Axel van de Walle at avdw@alum.mit.edu to check if you can have access to the database.
  exit
fi

if [[ $# -eq 0 || "x$1" == "x-h" ]]
then
  cat - <<-EOF
GET PROTOtypes from database, by Axel van de Walle
Syntax:
  getproto [-s] [-1] [-w] string
Returns all structure prototypes matching string in ATAT's prototype database.
[string] can be a regular expression, e.g. ^A3
The last 2 columns of the output can be used as arguments to the wycked code
to generate the structure
Options:
  -s: sort output in decreasing order of symmetry
  -1: only report the first match
  -w: only report the space group and wyckoff positions

ACKNOWLEDGEMENTS
EOF
grep "^#" ${atatdir}/data/prototypes.in | sed 's/#//g'
exit

fi

sortcmd="cat -"
headcmd="cat -"
colcmd="cat -"

while [ 1 ]
do
    case $1 in
	-s) sortcmd="sort -k 3 | sort -s -k 2 -r -n" ;;
	-1) headcmd="head -n 1" ;;
	-w) colcmd="sed 's/^[^ ]* //g'" ;;
	*) break ;;
    esac
    shift
done

grep -v "^#" ${atatdir}/data/prototypes.in | grep "$*" | eval $sortcmd | $headcmd | eval $colcmd
