

/*
 *  CPXLINK.TOS
 *
 *  Author: Arvin Schnell
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <osbind.h>
#include <ostruct.h>
#include <gemfast.h>

#include "xcontrol.h"



CPX_HEADER header;
void *buffer;
FILE *f;
long size;



void abort( void )
{
  if( buffer ) free( buffer );
  if( f ) fclose( f );
  exit( 1 );
}



void make_header( void )
{
  int i;
  int icon_data[48] = { 0x0000, 0x0000, 0x0001, 0x8000,
                        0x0002, 0x4000, 0x0002, 0x4000,
                        0x0004, 0x2000, 0x0004, 0x2000,
                        0x0008, 0x1000, 0x0008, 0x1000,
                        0x0010, 0x0800, 0x0010, 0x0800,
                        0x0020, 0x0400, 0x0020, 0x0400,
                        0x0040, 0x0200, 0x0040, 0x0200,
                        0x0092, 0x4100, 0x008A, 0x8100,
                        0x0107, 0x0080, 0x011D, 0xFC80,
                        0x0207, 0x0040, 0x020A, 0x8040,
                        0x0412, 0x4020, 0x0200, 0x0040,
                        0x01FF, 0xFF80, 0x0000, 0x0000  };
  
  header.magic = 100;
  
  header.flags.boot_init = 1;
  header.flags.set_only = 0;
  header.flags.resident = 0;
  
  strncpy( header.cpx_id, "HP4L", 4 );
  
  header.cpx_version = 0x0100;
  
  strcpy( header.icon_name, "LASERJET" );
  
  for( i = 0; i < 48; i++ )
    header.icon_data[i] = icon_data[i];
  
  header.icon_info = 0x7000;
  
  strcpy( header.cpx_name, "HP4L Config" );
  
  header.obj_state = 0x1180;
}



void load_header( void )
{
  f = fopen( "hp4l.hdr", "rb" );
  if( !f ) abort();
  fread( &header, sizeof( CPX_HEADER ), 1, f );
  fclose( f );
}



void load_program( void )
{
  _DTA *dta;
  
  dta = Fgetdta();
  Fsfirst( "hp4l.cp", 0 );
  size = dta->dta_size;
  buffer = malloc( size );
  if( !buffer ) abort();
  
  f = fopen( "hp4l.cp", "rb" );
  if( !f ) abort();
  fread( buffer, size, 1, f );
  fclose( f );
}



void save_cpx( void )
{
  f = fopen( "hp4l.cpx", "wb" );
  if( !f ) abort();
  fwrite( &header, sizeof( CPX_HEADER ), 1, f );
  fwrite( buffer, size, 1, f );
  fclose( f );
  
  free( buffer );
}



int main( void )
{
  make_header();
  load_program();
  
  save_cpx();
  
  return( 0 );
}
