#ifndef ZipBuilderSFXHPP
#define ZipBuilderSFXHPP

#include "Zipbase.h"      // for deleteopts
#include "ZipStructs.h"
#include "CondDefines.h"
//-- user supplied -----------------------------------------------------------

// if used as internal sfx for zipbuilder define INTERNAL_ZIPBUILDER_SFX
// this zipbuilder is not yet available
//#ifdef INTERNAL_SFX // will be defined in Condefines.h
//#define INTERNAL_ZIPBUILDER_SFX
//#endif                               

//When we want to build without self-extracting code in the ZipBuilder component
// then define NO_SFX_IN_ZIPBUILDER
#ifdef NO_SFX
#define NO_SFX_IN_ZIPBUILDER
#endif
//#define ZipBuilderVersion180  // for latest version TZipBuilder

bool CALLBACK ResEnumFunc(HANDLE hMod, char* szType, char* szName, TStrings* lParam);


namespace Zipbuildersfx
{


//-- type declarations -------------------------------------------------------
enum SpanSfxType {stSpanSfx, stMVSfx};
enum TSFXOverwriteMode { somOverwrite, somSkip, somAsk };

enum TSFXOption { soAskCmdLine, soAskFiles, soHideOverWriteBox, soAutoRun, soNoSuccessMsg,
                  soExpandVariables, soInitiallyHideFiles, soForceHideFiles,
                  soCheckAutoRunFileName, soCanBeCancelled, soDetached };
#ifdef NO_SFX_IN_ZIPBUILDER
typedef Set<TSFXOption, soAskCmdLine, soNoSuccessMsg> TBuilderSFXOptions;
#endif
typedef Set<TSFXOption, soExpandVariables, soDetached> TSFXOptionsExt;
typedef Set<TSFXOption, soAskCmdLine, soDetached>  TSFXOptions;

#pragma pack(push, 1)
struct TSFXFileHeader
{
	unsigned Signature;
	Word Size;
	TSFXOptions Options;
	TSFXOverwriteMode DefOVW;
	Byte CaptionSize;
	Byte PathSize;
	Byte CmdLineSize;
	Byte RegFailPathSize;
	Byte StartMsgSize;
	unsigned StartMsgType;
} ;

struct TSFXFileEndOfHeader
{
	unsigned Signature;
	unsigned HeaderSize;
} ;

struct TSFXDetachedHeader
{
  unsigned Signature;
  unsigned NameLen; // length of filename without extension of detached archive
  unsigned ExtLen;  // length of file ext without leading .
} ;

#pragma pack(pop)

//-----------------------------------------------------------------
class PACKAGE TZipBuilderSFX : public Classes::TComponent
{

private:
  bool FUseDelphiBin;
  bool Detached;
	AnsiString FMySFXMessage;
	TSFXOptions FMySFXOptions;
  TSFXOptionsExt FSFXOptionsExt;
	TSFXOverwriteMode FMySFXOverWriteMode;
	unsigned FMySFXMessageFlags;
  TComponent* FBuilder;
//	TZipBuilder* FZipBuilder;
	AnsiString FSFXPath;
  AnsiString FRegFailPath;
	int __fastcall CheckZipBuilder(void);
	AnsiString __fastcall GetVersion(void);
  TStringList* Included_Languages;
  AnsiString FSFXLanguage;
  TMemoryStream* SFXBinStream;
	void __fastcall SetVersion(const AnsiString Value);
  void __fastcall SetSFXPath(AnsiString Value);
  void __fastcall SetRegFailPath(AnsiString Value);
  void __fastcall SetSFXLanguage(const AnsiString Value);
  AnsiString __fastcall GetSFXLanguage(void);
	int __fastcall CopyBuffer(const int InFile, const int OutFile, int ReadLen);
	int __fastcall ReplaceIcon(const int &ExeFile, const int MaxPos, Graphics::TIcon* NewIcon);
	__int64 __fastcall ReadEOC(const int InFile, ZipEndOfCentral &EOC);
	int __fastcall CopyAndAdjustCentralDir(const int InFile, const int OutFile,
                                         const int NumEntries, const int DiskToAdjust,
                                         const int Offset);
	int __fastcall GetFirstLocalHeader(const int InFile, const int NumEntries,
                                     const int WhichDisk, const int Position);
  DWord  __fastcall SearchResDirEntry(pIRD ResStart, pIRDirE entry, int Depth);
  DWord  __fastcall BrowseResDir( pIRD ResStart, pIRD dir, int Depth );
  DWord  __fastcall LookForDirs( const void *SectionData, const DWord SectionVirtualStart,
															const int SectionLen, pIDD directories );
  bool __fastcall CreateSFXBinStream(void);
  int __fastcall EraseFile(const String Fname, DeleteOpts How);
#ifndef NO_SPAN
  AnsiString __fastcall GetLastVolume(AnsiString FileName,ZipEndOfCentral& EOC);
#endif
#ifdef NO_SFX_IN_ZIPBUILDER
  TBuilderSFXOptions FSFXOptions;
  AnsiString				 FSFXCaption;				// dflt = "Self-extracting Archive"
  AnsiString				 FSFXCommandLine;	// dflt = ""
  AnsiString				 FSFXDefaultDir;		// dflt = ""
  TIcon	*FSFXIcon;
  AnsiString				 FSFXMessage;
  TSFXOverwriteMode	 FSFXOverWriteMode;	// OvrConfirm  (others: OvrAlways, OvrNever)
  void   __fastcall SetSFXIcon(TIcon *aIcon);
#endif
protected:
	virtual void __fastcall Notification(Classes::TComponent* aComponent, Classes::TOperation Operation);
	void __fastcall GetPropertiesFromZipBuilder(void);
	int __fastcall DoConvertOrCreateSFX(const AnsiString File, const bool CreateNew);
	int __fastcall ReadSFXProperties(const int InFile, const int MaxPos);
	int __fastcall ReadNewSFXProperties(const int InFile, const int MaxPos);
	int __fastcall ReadSFXIcon(AnsiString File);
public:
	__fastcall virtual TZipBuilderSFX(Classes::TComponent* aOwner);
	int __fastcall ConvertToSFX(void);
	int __fastcall ConvertToZip(void);
	int __fastcall CreateNewSFX(const AnsiString File);
#ifndef NO_SPAN
  int __fastcall ConvertToSpanSFX(AnsiString OutFilePath);
#endif
__published:
  __property bool UseDelphiBin = {read=FUseDelphiBin, write=FUseDelphiBin, default=false};
	__property AnsiString SFXPath = {read=FSFXPath, write=SetSFXPath};
	__property TComponent* ZipBuilder = {read=FBuilder, write=FBuilder};
	__property AnsiString Version = {read=GetVersion, write=SetVersion};
  __property TSFXOptionsExt SFXOptionsExt={read=FSFXOptionsExt, write=FSFXOptionsExt,nodefault};
  __property AnsiString RegFailPath={read=FRegFailPath, write=SetRegFailPath};
  __property AnsiString SFXLanguage={read=GetSFXLanguage, write=SetSFXLanguage};
#ifdef NO_SFX_IN_ZIPBUILDER
  __property TBuilderSFXOptions SFXOptions={read=FSFXOptions, write=FSFXOptions, nodefault};
  __property TSFXOverwriteMode SFXOverWriteMode = {read=FSFXOverWriteMode, write=FSFXOverWriteMode, default=somAsk};
  __property String	 SFXCaption = {read=FSFXCaption, write=FSFXCaption, nodefault};
  __property String	 SFXDefaultDir = {read=FSFXDefaultDir, write=FSFXDefaultDir,  nodefault};
  __property String	 SFXCommandLine = {read=FSFXCommandLine, write=FSFXCommandLine, nodefault};
  __property String  SFXMessage = {read=FSFXMessage, write=FSFXMessage, nodefault};
  __property TIcon *SFXIcon = {read=FSFXIcon, write=SetSFXIcon, nodefault};
#endif

public:
      __fastcall virtual ~TZipBuilderSFX(void);

};


}	/* namespace Zipbuildersfx */
#if !defined(NO_IMPLICIT_NAMESPACE_USE)
using namespace Zipbuildersfx;
#endif

#endif	// ZipBuilderSFX
