/*

   This program serves as a simple front end to the PKARC package.
   It is written in Microsoft C v 4.0 and uses the Hammer Library
   from O.E.S. Systems for screen control

*/

#include <stdio.h>
#include <string.h>

#include <hamdefs.h>      /* This program uses O.E.S. Systems Hammer Library */

extern int menudpth;	  /* Menu depth (0=READY mode, 1..?? = menu level */
static int endprog=FALSE; /* end program flag */

char arcname[128];

main(argc,argv)
   int argc;
   char *argv[];

   {
   void cmdmenu();
   unsigned char *makefnam();

   if (argc<2) {
      printf("USAGE: pkarcm archive_file\n");
      exit(1);
      }
   
   makefnam(argv[1],".arc",arcname);

   strupr(arcname);
   
   clrscn();
   while (!endprog) {
      cmdmenu(argc,argv);
      clrlin(1), clrlin(2);
      locate(6,0), clreos();
      }

   locate(0,0); 
   clreos();
   }

void cmdmenu()
   {
   static struct menu_str mainm[] = {
      "List",   "List contents of archive",
      "Browse", "Display files on screen",
      "Extract","Extract files from archive",
      "Update", "Add/Update files to archive",
      "Move",   "Add/Update files to archive and DELETE files from directory",
      "Delete", "Delete files from archive",
      "Test",   "Test integrity of files in archive",
      "Quit",   "Return to DOS"
      };

   int choice, MYDEPTH=1;

   initmenu(0,2,TRUE,205,(NORMAL | HILITE),NORMAL,REVERSE); /* 205=graphic char */

   menudpth=MYDEPTH;

   for(;;) {
      switch (choice=domenu(mainm,sizeof(mainm),choice)) {
	 case 0:
            docmd("Listing contents of","","pkxarc -v");
	    break;

	 case 1:
            dobrowse();
	    break;

	 case 2:
            docmd("Extracting from","extract","pkxarc -x");
	    break;

         case 3:
            docmd("Updating contents of","update","pkarc -u");
	    break;

	 case 4:
            domove();
	    break;

	 case 5:
            docmd("Deleting files from","delete","pkarc -d");
	    break;

	 case 6:
            docmd("Testing integrity of files in","","pkxarc -t");
	    break;

	 case 7:
	    endprog=TRUE;
       	    return;
         }
      }
   }

domove()
   {
   char cmdline[129],line[129];

   comment(4,"Moving files to");
      
   if (askyn(6,0,"Are you sure you want your files to be DELETED?",0)) {
      prompt(8,"move",line,sizeof(line));
      execute("pkarc -m",line);
      }
   
   clean_up();
   }

dobrowse()
   {
   char line[81],cmdline[129];

   comment(4,"Browsing files in");

   prompt(6,"display",line,sizeof(line));

   locate(0,0);
   clreos();
   
   sprintf(cmdline,"pkxarc -c %s %s | more",arcname,line);
   system(cmdline);
   
   clean_up();
   }

docmd(label,tag,cmd)
   char *label,*tag,*cmd;
   
   {
   char line[129];
   
   line[0]='\0';
   
   comment(4,label);

   if (*tag) 
      prompt(6,tag,line,sizeof(line));
   
   execute(cmd,line);
   clean_up();
   }
   
comment(row,tag)
   int row;
   char *tag;
      
   {
   clrlin(row);
   locate(row,0);
   printf("%s archive %s",tag,arcname);
   }
      
prompt(row,tag,line,size)
   int row,size;
   char *tag,*line;
   
   {
   clrlin(row);
   locate(row,0);
   printf("Enter specification of file(s) to %s: ",tag);
   gets(line,size);
   
   locate(row+2,0);
   }

execute(cmd,list)
   char *cmd,*list;
   
   {
   char cmdline[129];
   
   sprintf(cmdline,"%s %s %s",cmd,arcname,list);
   system(cmdline);
   }

clean_up()
   {
   printf("\n\nPress any key to continue...");
   getch();
   
   locate(3,0);
   clreos();
   }

/*	split up a file name (subroutine for makefnam)
*/

static _makefn(source,dest)
   unsigned char *source;
   unsigned char *dest;
   
   {
   int j,k;

   memset(dest,0,82);		/* clear result field */
   
   if (strlen(source)>1 && source[1]==':')
      for (j=0;j<2;)
         dest[j++]=*source++;
   
   j=3;
   
   while (strrchr(source,'\\') || strrchr(source,'/')) {
      for (k=0;(*source!='\\')&&(*source!='/');++source)
	 if (j<65 && k++<8)
            dest[j++]=*source;
   
         dest[j++] = (*source=='/') ? *source  : '\\';
         ++source;
      }
   
   for (j=67;*source && *source!='.';++source)
      if (j<75)
         dest[j++]=*source;
   
   for (j=76;*source;++source)
       if (j<80)
          dest[j++]=*source;
   }

/*	make a file name using a template
*/

unsigned char *makefnam(rawfn,template,result)
   unsigned char *rawfn;	/* the original file name */
   unsigned char *template;	/* the template data */
   unsigned char *result;	/* where to place the result */

   {
   unsigned char et[82],er[82];

   _makefn(template,et);
   _makefn(rawfn,er);

   *result=0;			/* assure no data */
   strcat(result,er[0]?er:et);
   strcat(result,er[3]?er+3:et+3);
   strcat(result,er[67]?er+67:et+67);
   strcat(result,er[76]?er+76:et+76);
   return result;
   }
