#!/bin/csh
if ( 'x'$3 == 'x' ) then
  echo "Utility to convert structure files into xyz files suitable for viewing with rasmol"
  echo "Syntax: str2xyz [-v] na nb nc [scale] infile"
  echo "-v (optional) calls rasmol (otherwise, xyz file is written to stdout"
  echo "na nb nc define the number of periodic repetition to plot"
  echo "scale (optional) scales the structure to adjust which bonds are plotted"
  exit 1
endif

if ( 'x'$1 == 'x-v' ) then
  set doview
  shift;
endif

if ( 'x'$5 == 'x' ) then
    set scale=1
    set infile=$4
else
    set scale=$4
    set infile=$5
endif

if ( "$1" == "1" && "$2" == "1" && "$3" == "1" ) then
  cat $infile | cellcvrt -f | cellcvrt -c -sc=$scale | awk '{print $4,$1,$2,$3}' | tail -n +7 >! tmptmp.str
else
  (echo $1 0 0 ; echo 0 $2 0 ; echo 0 0 $3) >! supercell.tmp
  cat $infile | cellcvrt -f | cellcvrt -uss=supercell.tmp | cellcvrt -c -sc=$scale | awk '{print $4,$1,$2,$3}' | tail -n +7 >! tmptmp.str
endif

( echo `cat tmptmp.str | wc -l` ; echo Title ; cat tmptmp.str ) >! tmptmp.xyz

if ( $?doview ) then
	rasmol -xyz tmptmp.xyz
else
	cat tmptmp.xyz
endif

rm -f tmptmp.xyz tmptmp.str supercell.tmp
