##############################################################################
##   Author:     C. Rhodes  (design advice by E.A.O'Brien)
## 
##   Date:       11/1/93
## 
##   Purpose:    This shell script performs the elementary processing needed
##               to supply p-group generation input to pq, process the compact 
##               descriptions output and use this output to generate both
##               CAYLEY and GAP group libraries.
## 
##   Revisions:  update to Cshell compatible                           18.1.93
##               both GAP and CAYLEY produced from command line        18.1.93
## 
##############################################################################
## This code remains the property of ANU. If you use it you are bound to our
## conditions. If you use this it is at your own risk.
## Copyright ANU 11/1/93.
##############################################################################
## 
# begin with the headers
  echo '######################################################################'
  echo '#                  PQ to CAYLEY & GAP Library conversion             #'
  echo '######################################################################'
  echo '# Authors: C. Rhodes (shell scripting and pq modifications)          #'
  echo "#          E.A. O'Brien (the rest)                                   #"
  echo '#          School of Mathematical Sciences, ANU                      #'
  echo '#                                                                    #'
  echo '# Last revised February 1993                                         #'
  echo '#                                                                    #'
  echo '# Flags:  -g for gap, -c for cayley, -h for GAP headers, -F for file #'
#  echo '# Caveat: These files are for the use of any interested party but    #'
#  echo '#         are not to be used for commercial purpose without ANUs     #'
#  echo '#         consent. Copyright ANU 13/1/93                             #'
  echo '######################################################################'

# initialise
  set GAP = false
  set CAYLEY = false
  set HEADER = false
  set FILENAME = false
  set FILE 
  set HEADNAME

# check that the user has supplied some parameters! we need _at least_ an
# input file name.
switch ($#argv)
case 0:
  echo There are several aspects of this program with which 
  echo you should be familiar before you proceed. Please read this...
  echo ' '
  echo The -c or -g options select CAYLEY or GAP output respectively and may 
  echo be used concurrently. The -h option allows a user to supply the
  echo name of a header file to be used for each GAP file generated. 
  echo The -F option is used to supply the file used as input to pq.
  echo When invoked with an argument this program calls pq, 
  echo then streams the output into the format required by CAYLEY 
  echo and/or GAP. The directory *temp_lib_files_pq* is created and 
  echo deleted during this process.
  echo It is assumed that pq, cayley, and gap are in your default path.
  exit (1)
breaksw

default:
   while ($#argv != 0 ) 
     switch ($argv[1])
	case '-g':  
	   set GAP = true
	   echo 'GAP data files will be output in directory gap_lib' 
	breaksw
	case '-c':  
	   set CAYLEY = true
	   echo 'CAYLEY data files will be output in directory cayley_lib' 
	breaksw
	case '-h':
	   set HEADER = true
	   echo 'Headers will be output with the GAP code'
	   if ($#argv == 0) then
	     echo 'You must specify a header'
	     echo 'look at the file header for an example'
	     exit (-1)
	   endif
	   shift argv
	   set HEADNAME = $argv[1]
	   echo 'GAP headers come from' $HEADNAME
	   endif
	breaksw
	case '-F':
	   set FILENAME = true
	   if ($#argv == 0) then
	     echo 'You must specify an input filename'
	     exit (-1)
	   endif
	   shift argv
	   set FILE = $argv[1]
	   test -f $FILE
	   if ($status != 1) then
  	     echo 'pq input will be read from ' $FILE
	   else
	      echo 'No such file ...'  $FILE
	      exit (-1)
	   endif
	breaksw
	default: 
          echo 'Unknown option'
	  exit (-1) 
	breaksw
     endsw  
     shift argv
   end
endsw

#exceptional cases. Can't do these!
if (($HEADER == true)&&($GAP == false)) then
   echo "You must set the GAP flag to output headers on GAP files!"
   exit (-1)
endif


if (($FILE == false)) then
  echo "There is no pq input file! Use the -F option to select one"
  exit (-1)
endif

#we'll work in a temporary file so make sure there is nothing in it and move
#the appropriate shell scripts into that directory.

   unalias rm                     # in case they've got -something on
   rm -R temp_lib_files_pq
   rm -R gap_lib cayley_lib
   mkdir temp_lib_files_pq temp_lib_files_pq/cayley_lib
   mkdir temp_lib_files_pq/gap_lib   

   cd temp_lib_files_pq
   
# now run pq and check the exit status.

   echo 'Running pq - please wait...' 
   pq < ../$FILE >  ../temp_lib_files_pq/pq_out

  if ($status) then
	echo 'pq has run successfully. Look in temp_lib_files_pq/pq_output for output'
  else
	echo 'pq has failed. Please check the file temp_lib_files_pq/pq_output for details'
	exit (-1)
  endif

# and turn the output into CAYLEY/GAP output

    if ($GAP == true) then
      echo Now working on the GAP side of things...
      cp ../To_gap_format ../gap_padic_compress .
      foreach gp (gps*) 
        sh To_gap_format $gp  >> gap_lib/$gp.grp
        echo "processed file" $gp
      end
      if ($HEADER == true) then
        foreach gp (gap_lib/*)
          cat ../$HEADNAME > $gp.h
	  cat $gp >> $gp.h
	  mv -f $gp.h $gp
	  echo 'added header to' $gp
	end
      endif
    endif

   if ($CAYLEY == true) then
     echo Now working on the CAYLEY side of things...
     cp ../To_cayley_format ../cay_padic_compress .
     foreach gp (gps*) 
       sh To_cayley_format $gp >> cayley_lib/$gp
       echo "processed file" $gp
     end
   endif

# clean up
cd ..
mv temp_lib_files_pq/gap_lib .
mv temp_lib_files_pq/cayley_lib .
rm -R temp_lib_files_pq

echo $0 has completed successfully. 
echo See the directories cayley_lib and gap_lib if you have requested output

