#!/bin/tcsh
switch ($#)
	case 0:
	echo "Usage: rotation inputfile(no .inp) totrotation increment"
	breaksw
	case 1:
	echo "Usage: rotation inputfile	totrotation increment"
        breaksw
	case 2:
	echo "Usage: rotation inputfile	totrotation increment"
        breaksw
	case 3:
		sed -n -e '1,/C1/p' $1.inp > rothead.inp
		sed -n -e '/^ $END/,$p' $1.inp > rottail.inp
		sed -n -e '/C1/,/$END/p' $1.inp > rotdata.inp
		sed -i -e '1d' -e '$d' rotdata.inp
		
		echo "How many atoms will rotate (do not include points on point of rotation)"
		set atoms=$<
		
		echo "enter atom number of rotation point 1"
		set atm1=$<
		echo "enter atom number of rotation point 2"
		set atm2=$<
				
		set counter=1
		set rotatms = ( 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
		# current rotation atoms is limited to 40		
		while ($counter <= $atoms)
			echo "enter atom number"
			@ rotatms[$counter] = $<
			@ counter = $counter + 1
		end

		set counter = 1 
		set sedcom = ( "sed -e $atm1' d' -e $atm2' d' ")
		while ($counter <= $atoms)
			set sedcom = ( $sedcom "-e  $rotatms[$counter]' d' " )
			@ counter = $counter + 1 
		end
		set sedcom = ($sedcom "rotdata.inp > cleanedrotdata.inp" )
		eval $sedcom

		sed -i -e 's/[ \t]*[ \t]/,/g' -e 's/.0,/.0\t/' -e 's/,/      /' rotdata.inp
		
		set counter = 1
		sed -n $atm1'p' rotdata.inp > transdata
		sed -n $atm2'p' rotdata.inp >> transdata
		while ($counter <= $atoms)
			sed -n $rotatms[$counter]'p' rotdata.inp >> transdata
			@ counter = $counter + 1 
		end
		
		
		sed -i -e 's/[ \t]*[ \t]/,/g' -e 's/,/, /g' -e 's/, -/,-/g' -e 's/.0,/.0\t/' -e 's/,/     /' -e 's/,/      /g' cleanedrotdata.inp
		
		set tr = $2
		set inc = $3
		set cr = 0
		while ($cr < $tr)
			#echo $cr
			@ cr = $cr + $inc
			cat rothead.inp > $1.$cr.inp
			crot  $atoms $cr transdata >> $1.$cr.inp
			cat cleanedrotdata.inp >> $1.$cr.inp
			cat rottail.inp >> $1.$cr.inp
			sed -i 's/,/\t/' $1.$cr.inp
		end
		
		rm rothead.inp rotdata.inp rottail.inp  cleanedrotdata.inp
		#transdata
	breaksw
	case *:
	echo "To many arugments"
	echo "Usage: rotation inputfile	totrotation increment"
    breaksw
endsw

