/*
   This program may be freely
  distributed , copied,and modified so long
as a fee is not charged. However,
I reserve all rights available to
me under The Copyright Act.
If you find any bugs please feel free to contact me.
  David DeGeorge
  Princeton,New Jersey
  March 1987

USENET:  ....princeton!idacrd!dld
COMPUSERVE: 74176,3210
GENIE: DEGEORGE
*/

#define MAIN
#include "makersh.h"
#include "globals.h"
#ifdef GEM
    main()
  {
#else
      main(argc,argv)
      char **argv;
{
#endif	     
	 int i,*p,j;
         char *q,*r1,mesag[80];
         int oneflag;
         int size;
         long where,z,w,*r,lseek();
         
#ifdef GEM         
        begin();
        getfiles(&in,&out);        
#else
          if ( argc != 3)       /* get input and output */
             error("",USAGE);
         if(( in  = fopen(argv[1],"rb"))== NULL){
           sprintf(mesag,"Can't open %s for reading\n",argv[1]);
           error(mesag,FATAL);
        }
        
        if((out = fopen(argv[2],"w"))==NULL){
           sprintf(mesag,"Can't open %s for writing\n",argv[2]);
           error(mesag,FATAL);
        }
        if ( access(argv[2],EXISTS) == 0){
          sprintf(mesag,"%s exists",argv[2]);
          error(mesag,WARN);
        }
#endif         			
         	
         size = fread(&rheader,1,sizeof(rheader),in);    /* read the header */
         	
         if ( size != sizeof(rheader))
         	error("Error in|reading header",FATAL);
         	/* check if resource file */
         	
         if((long)(rheader.rsh_rssize) != lseek(fileno(in),0L,2))
               error("Resource file is not|the correct length",WARN);
               
         stradds =(long *)malloc(MAXSTR*4);
         nullnes(stradds,"Space for string addresses"); 

           
  
         head = malloc(rheader.rsh_rssize);
         nullnes(head,"Resource file");
         fseek(in,0L,0);
         
         /* Read the whole file into memory */
         
         fread(head,1,rheader.rsh_rssize,in);
         
#ifdef GEM	 
         makeitems();
         display();
#endif         
         bbadds = (long *)malloc(4*rheader.rsh_nbb);
         
          nullnes(bbadds,"Bitblock addresses");
         objs = (OBJECT *)( head + rheader.rsh_object);	
         teds = (TEDINFO *)( head + rheader.rsh_tedinfo);
         bitblks = (BITBLK *)( head + rheader.rsh_bitblk);
         iconblks = ( ICONBLK *)(head + rheader.rsh_iconblk);
         
   /* lets do the strings   */     
        q = ( char *) ( head + rheader.rsh_string);
        r1 = ( char *) ( head + rheader.rsh_imdata);
         j = 0;        
        oneflag = 1;  /* for the first time thru */
        
        fprintf(out,"\nchar *rs_strings[]={");
        
        /* apparently the images are right after the strings */
        /* So that's how we know when to stop */
        
        while( q  < r1 ){
        	
        if( q == r1-1) *q = '\0';    /* There may be an extra byte 
                                     because the images must start
                                     on a word boundary
                                     */	
        if (oneflag == 0)
        	 fprintf(out,",\n\"%s\"",q);
        	else{
        	    oneflag = 0;
        	    fprintf(out,"\n\"%s\"",q);
        	}
        stradds[nstrings]=(long)(q-head);
         j += 1 + strlen(q);
         
#ifdef GEM         
         progress(0,j);
#endif         
         q += 1 + strlen(q);
        nstrings++;
        if ( nstrings == strmax){
        	strmax += MAXSTR;
        stradds= (long *)realloc(stradds,4*strmax);
        nullnes(stradds,"Realloting string adds");
        }
    }
        fprintf(out,"\n};\n");

     
    
   
        /* write out the objects next */
        fprintf(out,"\nOBJECT rs_object[]={\n");
        for ( i=0 ; i <rheader.rsh_nobs ; i++){
        	
#ifdef GEM        	
           progress(1,i+1);
#endif       	
        printobj(objs + i);
        if ( i != rheader.rsh_nobs -1 ) fprintf(out,",");        
        fprintf(out,"\n");
}
        fprintf(out,"};\n");	
        
        /*get the offsets of the images so that
           we can get the indices right
         by using findbb()
         */ 
      
              
        for ( i =0 ; i < rheader.rsh_nbb; i++){
         bbadds[i] = (long)(&bitblks[i])-(long)head;	
         /* size is how many words */
         
         size=(bitblks[i].bi_hl)*(bitblks[i].bi_wb)/2;
              where = bitblks[i].bi_pdata;
              
         /* makeimage returns the index in the image array
            cast to a long
            */
         bitblks[i].bi_pdata=makeimage(where,size);
        }
        
        for ( i=0 ; i <rheader.rsh_nib ; i++){
         size=(iconblks[i].ib_hicon)*(iconblks[i].ib_wicon)/16;
         iconblks[i].ib_pmask=makeimage(iconblks[i].ib_pmask,size);
         iconblks[i].ib_pdata=makeimage(iconblks[i].ib_pdata,size);
         iconblks[i].ib_ptext = findstr(iconblks[i].ib_ptext);
        }
                fprintf(out,"\nBITBLK rs_bitblk[]={\n");
        for ( i=0 ; i <rheader.rsh_nbb ; i++){
        	
#ifdef GEM        	
        progress(3,i+1);
#endif
        
        printbit( bitblks + i);
        if ( i != rheader.rsh_nbb -1 ) fprintf(out,",");
        fprintf(out,"\n");
}
        fprintf(out,"};\n");

                fprintf(out,"\nICONBLK rs_iconblk[]={\n");
        for ( i=0 ; i <rheader.rsh_nib ; i++){
#ifdef GEM        	
        progress(4,i+1);
#endif        
        printicon( iconblks + i);
        if ( i != rheader.rsh_nib -1 ) fprintf(out,",");
        fprintf(out,"\n");
}
        fprintf(out,"};\n");	
              fprintf(out,"\nTEDINFO rs_tedinfo[]={\n");
        for ( i=0 ; i <rheader.rsh_nted ; i++){
        	
#ifdef GEM        	
        progress(5,i+1);	
#endif        
        printted( teds + i);
        if ( i != rheader.rsh_nted -1 ) fprintf(out,",\n");
}
       fprintf(out,"\n};\n");
       r =( head + rheader.rsh_trindex);
       w = (long)( (char *)objs - head );  
       fprintf(out,"long rs_trindex[]={\n");
       for ( i=0; i < rheader.rsh_ntree ; i++){
       	
#ifdef GEM       	
       progress(6,i+1);
#endif       
       z = r[i];
       if ( i != rheader.rsh_ntree -1 )
       fprintf(out,"%DL,\n",( z-w )/sizeof(OBJECT));
       else
       fprintf(out,"%DL\n",(z-w)/sizeof(OBJECT));
}
       fprintf(out,"};\n");
       fprintf(out,"rs_frstr[]={\n");
       r = (head + rheader.rsh_frstr);
       for ( i= 0 ; i < rheader.rsh_nstring ; i++){
       
#ifdef GEM       	
       progress(7,i+1);	
#endif       
       z = r[i];	
       if( i != rheader.rsh_nstring - 1)fprintf(out,"%DL,\n",findstr(z));
        else fprintf(out,"%DL\n",findstr(z));
}
       fprintf(out,"};\n");
       r = ( head + rheader.rsh_frimg);
       fprintf(out,"long rs_frimg[]={\n");
       for ( i= 0; i < rheader.rsh_nimages ; i++){
       	
#ifdef GEM       	
        progress(8,i+1);	
#endif        
       if ( i != rheader.rsh_nimages -1)fprintf(out,"%DL,\n",findbb(r[i]));
        else fprintf(out,"%DL\n",findbb(r[i]));
        }
        fprintf(out,"};\n");
 fprintf(out,"struct foobar {\n\tint dummy;\n\tint *image;\n\t} rs_imdope[] = {\n");
       for ( i =0 ; i < nimages ; i++){
       if ( i != nimages -1)fprintf(out,"0,&IMAG%d[0],\n",i);
           else fprintf(out,"0,&IMAG%d[0]\n",i);
        }
       fprintf(out,"};\n");
       fprintf(out,"#define NUM_TI %d\n",rheader.rsh_nted);
       fprintf(out,"#define NUM_BB %d\n",rheader.rsh_nbb);
       fprintf(out,"#define NUM_OBS %d\n",rheader.rsh_nobs);
       fprintf(out,"#define NUM_IB %d\n",rheader.rsh_nib);
       fprintf(out,"#define NUM_FRSTR %d\n",rheader.rsh_nstring);
       fprintf(out,"#define NUM_FRIMG %d\n",rheader.rsh_nimages);
       fprintf(out,"#define NUM_TREE %d\n",rheader.rsh_ntree);
       fprintf(out,"#define NUM_STRINGS %d\n",nstrings); 
       fprintf(out,"#define NUM_IMAGES  %d\n",nimages);	
#ifdef GEM       
       flee() ;
#endif       

}
 
