/*	SkBackup Frontend ©1994 Dario Rosati	*/
/*	SkBackup is ©1994 Alessandro Pozzoli	*/

#include <exec/types.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <libraries/asl.h>
#include <intuition/intuition.h>
#include <intuition/classes.h>
#include <intuition/classusr.h>
#include <intuition/imageclass.h>
#include <intuition/gadgetclass.h>
#include <libraries/gadtools.h>
#include <graphics/displayinfo.h>
#include <graphics/gfxbase.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <clib/gadtools_protos.h>
#include <clib/graphics_protos.h>
#include <clib/utility_protos.h>
#include <clib/dos_protos.h>
#include <clib/asl_protos.h>
#include <string.h>
#include <stdio.h>

#define GetString(g)      (((struct StringInfo *)g->SpecialInfo)->Buffer)
#define GetNumber(g)      (((struct StringInfo *)g->SpecialInfo)->LongInt)

#define GD_COmm                                0
#define GD_Delete                              1
#define GD_Recurs                              2
#define GD_Verbose                             3
#define GD_Ask                                 4
#define GD_Size                                5
#define GD_GO                                  6
#define GD_Gadget70                            7
#define	GD_NewAr			       8

#define GDX_COmm                               0
#define GDX_Delete                             1
#define GDX_Recurs                             2
#define GDX_Verbose                            3
#define GDX_Ask                                4
#define GDX_Size                               5
#define GDX_GO                                 6
#define GDX_Gadget70                           7
#define GDX_NewAr			       8

#define BackupWin_CNT 9

 
extern char __stdiowin[]="CON:20/114/600/142/SkandalBackup"; 

extern struct IntuitionBase *IntuitionBase;
extern struct Library       *GadToolsBase;
extern struct Library       *DiskFontBase;
extern struct Library	    *IconBase;
extern struct Library	    *AslBase;
extern struct Library	    *DOSBase;

int SetupScreen(void);
void CloseDownScreen(void);
void HandleBackupWinIDCMP(void);
int BackupWinCloseWindow();
int BackupWinVanillaKey();
int OpenBackupWinWindow(void);
void CloseBackupWinWindow(void);

/*
 *  Source machine generated by GadToolsBox V2.0b
 *  which is (c) Copyright 1991-1993 Jaba Development
 *
 *  GUI Designed by : Xenon
 */

struct Screen         *Scr = NULL;
APTR                   VisualInfo = NULL;
UBYTE		      *PubScreenName=NULL;
struct Window         *BackupWinWnd = NULL;
struct Gadget         *BackupWinGList = NULL;
struct IntuiMessage    BackupWinMsg;
struct Gadget         *BackupWinGadgets[9];
UWORD                  BackupWinLeft = 98;
UWORD                  BackupWinTop = 11;
UWORD                  BackupWinWidth = 462;
UWORD                  BackupWinHeight = 91;
UBYTE                 *BackupWinWdt = (UBYTE *)"Skandal Backup FrontEnd ©1994 by Dario Rosati";

UBYTE *COmm0Labels[] = {
	(UBYTE *)"Add",
	(UBYTE *)"Extract",
	(UBYTE *)"List",
	(UBYTE *)"Statistic",
	NULL };

struct TextAttr topaz8 = {
	( STRPTR )"topaz.font", 8, 0x00, 0x01 };

UWORD BackupWinGTypes[] = {
	CYCLE_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	CHECKBOX_KIND,
	INTEGER_KIND,
	BUTTON_KIND,
	BUTTON_KIND,
	CHECKBOX_KIND
};

struct NewGadget BackupWinNGad[] = {
	214, 33, 101, 13, (UBYTE *)"Command", NULL, GD_COmm, PLACETEXT_RIGHT, NULL, NULL,
	18, 6, 26, 11, (UBYTE *)"Delete Files ?", NULL, GD_Delete, PLACETEXT_RIGHT, NULL, NULL,
	18, 20, 26, 11, (UBYTE *)"Recursive Dir ?", NULL, GD_Recurs, PLACETEXT_RIGHT, NULL, NULL,
	18, 35, 26, 11, (UBYTE *)"Verbose ?", NULL, GD_Verbose, PLACETEXT_RIGHT, NULL, NULL,
	18, 50, 26, 11, (UBYTE *)"Ask Disks ?", NULL, GD_Ask, PLACETEXT_RIGHT, NULL, NULL,
	214, 50, 100, 12, (UBYTE *)"Size in Bytes", NULL, GD_Size, PLACETEXT_RIGHT, NULL, NULL,
	17, 71, 95, 12, (UBYTE *)"GO!", NULL, GD_GO, PLACETEXT_IN, NULL, NULL,
	337, 71, 95, 12, (UBYTE *)"About...", NULL, GD_Gadget70, PLACETEXT_IN, NULL, NULL,
	214, 6, 26,11, (UBYTE *)"New Archive ?", NULL, GD_NewAr, PLACETEXT_RIGHT, NULL, NULL
};

ULONG BackupWinGTags[] = {
	(GTCY_Labels), (ULONG)&COmm0Labels[0], (GT_Underscore), '_', (TAG_DONE),
	(GT_Underscore), '_', (TAG_DONE),
	(GTCB_Checked), TRUE, (GT_Underscore), '_', (TAG_DONE),
	(GTCB_Checked), TRUE, (GT_Underscore), '_', (TAG_DONE),
	(GTCB_Checked), FALSE, (GT_Underscore), '_', (TAG_DONE),
	(STRINGA_Justification),GACT_STRINGRIGHT,(GTIN_Number), 8900000, (GTIN_MaxChars), 20, (GT_Underscore), '_', (TAG_DONE),
	(TAG_DONE),
	(TAG_DONE),
	(GTCB_Checked), TRUE, (GT_Underscore), '_', (TAG_DONE)
};


struct TagItem stags[4];

 
LONG __OSlibversion = 37;	/* Needed by libraries initilizers */

/* Various flags settings */

BOOL	FLG_DeleteFiles = FALSE;
BOOL	FLG_Verbose	= TRUE;
BOOL	FLG_AskDisks	= FALSE;
BOOL	FLG_Recursive	= TRUE;
BOOL	FLG_NewArchive	=TRUE;
char	Commands[4]	= {'a','e','l','s'};
UBYTE	ActualCMD	= 0;			/* default action: Add */ 
int	FileSize	= 890000;		/* default size: 880k  */  
char	CMDLine[256];

struct	EasyStruct AboutRequest = {
	sizeof(struct EasyStruct),
	0,
	"SkandalBackup",
	"SkandalBackup v1.1 ©1994 by Alessandro Pozzoli (Willow)\n"
	"SkandalBackup Frontend v1.03 by Dario Rosati   (Xenon) \n\n"
	"This program is freeware. Not for commercial use. \n"
	"Be sure to have SkBackup anywhere in your PATH !! \n",
	"Understood"
	};

struct	EasyStruct WrongSel = {
	sizeof(struct EasyStruct),
	0,
	"SkandalBackup v1.1",
	"Invalid selection!",
	"Try Again"
	};
	
struct	FileRequester *FileReq;

/* Functions calls from here... */

int SetupScreen(void)
{
	if (!(Scr = LockPubScreen(NULL)))
		return(1L);

	if (!(VisualInfo = GetVisualInfo(Scr, TAG_DONE)))
		return(2L);

	return(0L);
}

void CloseDownScreen(void)
{
	if (VisualInfo) {
		FreeVisualInfo(VisualInfo);
		VisualInfo = NULL;
	}

	if (Scr) {
		UnlockPubScreen(NULL, Scr);
		Scr = NULL;
	}
}

void HandleBackupWinIDCMP(void)
{
	struct IntuiMessage	*m;
	BOOL			done = FALSE;
	struct Gadget		*PressedGad;
	BPTR			ExtractDir;

while(!done)
{
        Wait(1L << BackupWinWnd->UserPort->mp_SigBit);
	while((m = GT_GetIMsg(BackupWinWnd->UserPort)) && (done==FALSE))
	{
		CopyMem((char *)m,(char *)&BackupWinMsg,(long)sizeof(struct IntuiMessage));
		GT_ReplyIMsg(m);

		switch (BackupWinMsg.Class) {

			case	IDCMP_REFRESHWINDOW:
				GT_BeginRefresh(BackupWinWnd);
				GT_EndRefresh(BackupWinWnd, TRUE);
				break;

			case	IDCMP_CLOSEWINDOW:
				done=TRUE;
				break;

			case	IDCMP_VANILLAKEY:
				break;

			case	IDCMP_GADGETUP:
				PressedGad = ((struct Gadget *)m->IAddress);
				switch (PressedGad->GadgetID)
				{
					case GD_COmm:
					ActualCMD = m->Code;

					switch (ActualCMD)
					{
						case 0:	/* Add */
						GT_SetGadgetAttrs(BackupWinGadgets[1],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[2],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[4],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[5],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[8],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						
						break;
						
						case 1: /* Extract */
						GT_SetGadgetAttrs(BackupWinGadgets[5],BackupWinWnd,0,(GA_Disabled),TRUE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[8],BackupWinWnd,0,(GA_Disabled),TRUE,(TAG_END));

						break;
						
						case 2: /* List */
						GT_SetGadgetAttrs(BackupWinGadgets[1],BackupWinWnd,0,(GA_Disabled),TRUE,(TAG_END));
						break;
						
						case 3: /* Statistic */
						GT_SetGadgetAttrs(BackupWinGadgets[5],BackupWinWnd,0,(GA_Disabled),FALSE,(TAG_END));
						GT_SetGadgetAttrs(BackupWinGadgets[4],BackupWinWnd,0,(GA_Disabled),TRUE,(TAG_END));
						break;
					}	
							
					break;
					case GD_Delete:
					if (FLG_DeleteFiles)
						FLG_DeleteFiles=FALSE;
					else	FLG_DeleteFiles=TRUE;
					break;
					
					case GD_Recurs:
					if (FLG_Recursive)
						FLG_Recursive=FALSE;
					else	FLG_Recursive=TRUE;
					break;
					
					case GD_Verbose:
					if (FLG_Verbose)
						FLG_Verbose=FALSE;
					else	FLG_Verbose=TRUE;
					break;
					
					case GD_Ask:
					if (FLG_AskDisks)
						FLG_AskDisks=TRUE;
					else	FLG_AskDisks=FALSE;
					break;
					
					case GD_NewAr:
					if (FLG_NewArchive)
						FLG_NewArchive=FALSE;
					else	FLG_NewArchive=TRUE;
					break;
					
					case GD_Size:
					FileSize = (((struct StringInfo *)PressedGad->SpecialInfo)->LongInt);
					break;
					
					case GD_GO:

					stccpy(CMDLine,"SkBackup ",256);
				        stccpy(&CMDLine[strlen(CMDLine)],"-b",256-strlen(CMDLine));
					if(FLG_DeleteFiles&&(ActualCMD!=2)&&(ActualCMD!=3))
					    stccpy(&CMDLine[strlen(CMDLine)],"d",256-strlen(CMDLine));
					if(FLG_Recursive&&(ActualCMD!=2)&&(ActualCMD!=3))
					    stccpy(&CMDLine[strlen(CMDLine)],"r",256-strlen(CMDLine));
					if(FLG_AskDisks&&(ActualCMD!=3))
					    stccpy(&CMDLine[strlen(CMDLine)],"k",256-strlen(CMDLine));
					if(FLG_NewArchive&&(ActualCMD==0))
					    stccpy(&CMDLine[strlen(CMDLine)],"n",256-strlen(CMDLine));
					if(FLG_Verbose)
					    stccpy(&CMDLine[strlen(CMDLine)],"v",256-strlen(CMDLine));
					stccpy(&CMDLine[strlen(CMDLine)]," ",256-strlen(CMDLine));
					stccpy(&CMDLine[strlen(CMDLine)],&Commands[ActualCMD],2);
					if (ActualCMD!=3)
						stccpy(&CMDLine[strlen(CMDLine)]," \"",256-strlen(CMDLine));
					
					if(ActualCMD!=3)
					{										
					   if (FileReq = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
                				ASL_TopEdge,50,
                				ASL_LeftEdge,200,
                				ASL_Hail,(ULONG)"Choose File",
                				ASL_Dir,(ULONG)"",
						ASL_File,(ULONG)"Stuffs",
               	 				ASL_Pattern,(ULONG)"~(.info)",
                				ASL_FuncFlags,FILF_PATGAD,
                				ASL_Window,BackupWinWnd,
                				TAG_DONE))
        				   {
                				if (AslRequest(FileReq,0L))
                				{
							stccpy(&CMDLine[strlen(CMDLine)],FileReq->rf_Dir,256-strlen(CMDLine));
							stccpy(&CMDLine[strlen(CMDLine)],FileReq->rf_File,256-strlen(CMDLine));
							stccpy(&CMDLine[strlen(CMDLine)],"\"",256-strlen(CMDLine));
						}
						else
						{
							EasyRequest(BackupWinWnd,&WrongSel,0,0,0);
							break;
						}
					   }
					}
					
					stccpy(&CMDLine[strlen(CMDLine)]," ",256-strlen(CMDLine));
					if (ActualCMD!=1)
					   stci_d(&CMDLine[strlen(CMDLine)],FileSize);
					if((ActualCMD!=1)&&(ActualCMD!=2))
					{
					   stccpy(&CMDLine[strlen(CMDLine)]," \"",256-strlen(CMDLine));
   					   if (FileReq = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
                				ASL_TopEdge,50,
                				ASL_LeftEdge,200,
                				ASL_Hail,(ULONG)"Choose Directory to backup",
                				ASL_Dir,(ULONG)"",
               	 				ASL_Pattern,(ULONG)"~(.info)",
                				ASL_FuncFlags,FILF_PATGAD,
						ASL_ExtFlags1,FIL1F_NOFILES,
                				ASL_Window,BackupWinWnd,
                				TAG_DONE))
        				   {
                				if (AslRequest(FileReq,0L))
						{
							stccpy(&CMDLine[strlen(CMDLine)],FileReq->rf_Dir,256-strlen(CMDLine));
							stccpy(&CMDLine[strlen(CMDLine)],"\"",256-strlen(CMDLine));
						}
						else
						{
							EasyRequest(BackupWinWnd,&WrongSel,0,0,0);
							break;
						}
					   }
					}					
					
					else if (ActualCMD==1)
					{
   					   if (FileReq = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
                				ASL_TopEdge,50,
                				ASL_LeftEdge,200,
                				ASL_Hail,(ULONG)"Choose where to Extract",
                				ASL_Dir,(ULONG)"",
               	 				ASL_Pattern,(ULONG)"~(.info)",
                				ASL_FuncFlags,FILF_PATGAD,
						ASL_ExtFlags1,FIL1F_NOFILES,
                				ASL_Window,BackupWinWnd,
                				TAG_DONE))
        				   {
                				if (AslRequest(FileReq,0L))
						{
							ExtractDir=Lock(FileReq->rf_Dir,ACCESS_READ);
							CurrentDir(ExtractDir);
						}
						
						else
						{
							EasyRequest(BackupWinWnd,&WrongSel,0,0,0);
							break;
						}
					   }
					}
					
					/* printf("%s\n",CMDLine); */

					stags[0].ti_Tag = SYS_Input;
 					stags[0].ti_Data = Input();
					stags[1].ti_Tag = SYS_Output;
    					stags[1].ti_Data = Output();
    					stags[3].ti_Tag = TAG_DONE;
 										
					System(CMDLine,stags);
					UnLock(ExtractDir);
					printf("\nClick on the close gadget to return!\n");

					break;
					
					case GD_Gadget70:
					EasyRequest(BackupWinWnd,&AboutRequest,0,0,0);
					break;
					}
				break;
		}
	}
}
}

int OpenBackupWinWindow(void)
{
	struct NewGadget	ng;
	struct Gadget	*g;
	UWORD		lc, tc;
	UWORD		offx = Scr->WBorLeft, offy = Scr->WBorTop + Scr->RastPort.TxHeight + 1;

	if ( ! ( g = CreateContext( &BackupWinGList )))
		return( 1L );

	for( lc = 0, tc = 0; lc < BackupWin_CNT; lc++ ) {

		CopyMem((char * )&BackupWinNGad[ lc ], (char * )&ng, (long)sizeof( struct NewGadget ));

		ng.ng_VisualInfo = VisualInfo;
		ng.ng_TextAttr   = &topaz8;
		ng.ng_LeftEdge  += offx;
		ng.ng_TopEdge   += offy;

		BackupWinGadgets[ lc ] = g = CreateGadgetA((ULONG)BackupWinGTypes[ lc ], g, &ng, ( struct TagItem * )&BackupWinGTags[ tc ] );

		while( BackupWinGTags[ tc ] ) tc += 2;
		tc++;

		if ( NOT g )
			return( 2L );
	}

	if ( ! ( BackupWinWnd = OpenWindowTags( NULL,
				WA_Left,	BackupWinLeft,
				WA_Top,		BackupWinTop,
				WA_Width,	BackupWinWidth,
				WA_Height,	BackupWinHeight + offy,
				WA_IDCMP,	CYCLEIDCMP|CHECKBOXIDCMP|INTEGERIDCMP|BUTTONIDCMP|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY|IDCMP_REFRESHWINDOW,
				WA_Flags,	WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SMART_REFRESH|WFLG_ACTIVATE,
				WA_Gadgets,	BackupWinGList,
				WA_Title,	BackupWinWdt,
				WA_ScreenTitle,	"Original program ©1994 by Alessandro Pozzoli",
				TAG_DONE )))
	return(4L);

	GT_RefreshWindow(BackupWinWnd, NULL);

	return(0L);
}

void CloseBackupWinWindow( void )
{
	if ( BackupWinWnd        ) {
		CloseWindow( BackupWinWnd );
		BackupWinWnd = NULL;
	}

	if ( BackupWinGList      ) {
		FreeGadgets( BackupWinGList );
		BackupWinGList = NULL;
	}
}


void main()
{

	if(!SetupScreen())
	{
		if(!OpenBackupWinWindow())
		{
			HandleBackupWinIDCMP();
			CloseBackupWinWindow();
			CloseDownScreen();
		}
		else printf("cannot open window!\n");
	}
	else printf("cannot open screen!\n");
}
