/*
 * mk_sws_bat.c
 *
 * last modified  6 May 02 - to change paths to raw and final data
 * last modified  1 Apr 02 - to change names of log files
 * last modified 31 Jan 02 - to change subdirectories
 * last modified 16 Aug 01 - can take 8-digit TDT numbers of FITS file names
 * last modified 15 Aug 01 - added lines for swsmake2 and log files
 * last modified 28 Feb 01
 *
 * program to generate commands to write into a batch file
 * could probably do this using awk, but I'll do it this way
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main(int argc, char *argv[]) {

      char file_in[81],file_out[81],test_str[100],tdt[9],tdt_in[29],
           tdt_out[24],tdt_1[24],tdt_2[24],tdt_3[24],tdt_4[24];
      FILE *fd_in,*fd_out;
/*
 *    check command line arguments and read in file name
 */
      if (argc != 2) {
        printf("use:  mk_sws_bat input_file\n");
        exit(1);
      } else strcpy(file_in,argv[1]);

      fd_in=fopen(file_in,"r");
      if (fd_in == NULL) {
        printf("Error.  File %s not found.\n",file_in);
        exit(1);
      }

      while (fgets(test_str,20,fd_in) != NULL) {
        strcpy(tdt_in,"../olp10.1/");
        if (strncmp(test_str,"swaa",4) != 0) {
          strcat(tdt_in,"swaa");
          strncat(tdt_in,test_str,8);  tdt_in[23]='\0';
          strcat(tdt_in,".fit");
          strncpy(tdt,test_str,8);     tdt[8]='\0';
        }
        else {
          strncat(tdt_in,test_str,17); tdt_in[29] = '\0';
          strncpy(tdt,test_str+4,8);   tdt[8]= '\0';
        }
        printf("\n$flslws -o all.dat %s\n",tdt_in);
        printf("$runall\nswsmake1\nswsmake2,type=0\n");
        strcpy(tdt_out,"../v4.3/");
        strcat(tdt_out,tdt);
        strcpy(tdt_1,tdt_out);
        strcat(tdt_1,".pws");
        strcpy(tdt_2,tdt_out);
        strcat(tdt_2,".sws");
        strcpy(tdt_3,tdt_out);
        strcat(tdt_3,".log");
        printf("$mv all.pws %s\n",tdt_1);
        printf("$mv all.sws %s\n",tdt_2);
        printf("$mv all.log %s\n",tdt_3);
      }
      printf("\n$/bin/rm *.dat\n");
}
