//////////////////////////////////////////////////////////////////////
// Filename:		OriginPack.OGS
// Purpose:		Labtalk for Origin Pack dialog
// Creation:		by Scott Bertrand, 08/16/2001
// Modifications:
/// SY 10-27-2003 QA70-5421 v7.0732 FAILED_TO_UNINSTALL_OPK
//////////////////////////////////////////////////////////////////////

[OnOpen]
	dialog.start(OTools.dll,Opk);
	return 0;

[OnInit]
	dll -a OPack Opack;
	opack.reset();
	Opk!UseFolderInfo=1;
	Opk!SaveFolderInfo=1;
	Opk!Overwrite=0;
	Opk!UserAccess.v1$=$OPK.AllUsers;
	Opk!UserAccess.v2$=$OPK.LicensedUsers;
	Opk!UserAccess.v3$=$OPK.RegisteredUsers;
	Opk!UserAccess.Choice=1;
	Opk!FileList.NumCols=1;
	Opk!FileList.ColHead1$=$OPK.FileList;
	Opk!FileList.ColWidth1=285;
	return 0;

[Toolbar.New.OnClick]
	Opk!FileList.Reset();
	opack.reset();
	return 0;

[Toolbar.Add.OnClick]
	run.section(,Init);
	FDLOG.multiopen.sort=0;
	FDLOG.MultiOpen.ComboSel=0;
	FDLOG.multiopen.colview=12;	//By default, display Filename, Size and Modified

	FDLOG.numtypes=6;
	FDLOG.type1$=$Filetype.OriginFiles;
	FDLOG.type2$=$Filetype.OriginTemplates;
	FDLOG.type3$=$Filetype.OriginProg;
	FDLOG.type4$=$Filetype.TextData;
	FDLOG.type5$=$Filetype.Images;
	FDLOG.type6$=$Filetype.AllFiles;
	FDLOG.Message$="";

	if (FDLOG.multiopen()==0/0)
		return 1;

	loop(ii,1,FDlog.MultiOpen.Count)
	{
		FDlog.Get(A, ii);
		if(%[%A]>=%[%Y] && "%[%A,1:%[%Y]]"=="%Y")
		{
			Opk!FileList.Add("%[%A,%[%Y]+1:%[%A]]",0);
		}
		else
		{
			Opk!FileList.Add(%A);
		}
	}
	return 0;

[Init]
	FDlog.NumTypes=0;
	FDLOG.DlgName$=$OPK.OriginPackFiles;
	FDLOG.OptionDLG$="";		//Reset button to off
	FDLOG.checkname$="";		//Reset check box to null
	FDLOG.ShowComment=0;		//close the comments box
	FDLOG.CheckStatus=0;		//Set checkbox to off
	FDLOG.default$="";		//Set contents of "File Name" edit box to nothing
	return 0;

[Toolbar.Delete.OnClick]
	Opk!FileList.Del();
	return 0;

[Toolbar.Open.OnClick]
	run.section(,Init);
	FDLOG.Numtypes=2;
	FDlog.Type1$=$FileType.OPack;
	FDlog.Type2$=$FileType.AllFiles;
	FDlog.DefType=1;
	if(FDLOG.Open()==1)
		break;

	%B=FDLOG.Path$;
	OPack.reset();
	OPack.FileName$=%B%A;
	OPack.TargetFolder$="*";
	Opk!FileList.Reset();
	switch(Opack.list())
	{
		case 0:
			for(ii=1;1==1;ii++)
			{
				if(run.section(,GetNextFile, ii)==0)
					Opk!FileList.v$(ii)_1$=%A;
				else
					break;
			}
			break;
		case 1:
			type -b $OPK.ErrUserAccess;
			break;
		case 2:
			type -b $OPK.ErrFileIO;
			break;
		case 3:
			type -b $OPK.ErrHeaderFile;
			break;
		case 4:
			type -b $OPK.ErrExpandFile;
			break;
	}
	return 0;
	
[GetNextFile]
	%A=Opack.File$(%1)$;
	return 0;

[Toolbar.Extract.OnClick]
	OPack.TargetFolder$="*"; // unpack into temp folder
	i = OPack.Unpack();
	if( i != 0 ) // if unpack error
	{
		Run.Section(,OnOPKError);
		return 1;
	}

	i = OPack.FindSetup(Z);
	if( i > 0 ) // if an OPKSetup.ogs was unpacked
	{
		%A=OPack.TargetFolder$;
		i = Run.Section(%A%Z,OnBeforeSetup);
	}
	else
	{
		i = 0; // continue setup
	}
	
	OPack.Undo(); // always remove temp folder
	
	if( i == 1 ) // if installation should NOT continue
	{
		return 1;
	}
	OPack.TargetFolder$=%Y;
	OPack.UseFolderInfo=Opk!UseFolderInfo;
	Opack.AutoReplace=OPK!OverWrite.Choice;
	i = Opack.UnPack();
	i = Run.Section(,OnOPKError);
	return i;

[OnOPKError]
	switch(i)
	{
		/// SY 10-27-2003 QA70-5421 v7.0732 FAILED_TO_UNINSTALL_OPK
		case 12:
			type -b $OPK.ErrWriteUnInstallInfo;
		/// end FAILED_TO_UNINSTALL_OPK	
		case 0:
			type -b $OPK.UnpackSuccess;
			i = OPack.FindSetup(Z);
			if( i > 0 ) // if an OPKSetup.ogs was unpacked
			{
				%A=OPack.TargetFolder$;
				i = Run.Section(%A%Z);
				i = Run.Section(%A%Z,OnAfterSetup);
				File.Delete(%A%Z); // delete the OPKSetup file
			}
			break;
		case 1:
			type -b $OPK.ErrUserAccess;
			return 1;
			break;
		case 2:
			type -b $OPK.ErrFileIO;
			return 1;
			break;
		case 3:
			type -b $OPK.ErrHeaderFile;
			return 1;
			break;
		case 4:
			type -b $OPK.ErrExpandFile;
			return 1;
			break;
		default:
			type -b $OPK.ErrNoFiles;
			return 1;
			break;
	}
	return 0;

[Toolbar.Save.OnClick]
	if(Opk!FileList.NumItems<1)
	{
		type -b $OPK.NoFiles;
		break;
	}
	Run.Section(,Init);
	FDlog.NumTypes=2;
	FDlog.Type1$=$FileType.OPack;
	FDlog.Type2$=$FileType.AllFiles;
	FDlog.DefType=1;
	%A=*.OPK;
	if(FDlog.SaveAs()==1)
		break;
	%B=FDlog.Path$;
	OPack.Reset();
	OPack.FileName$=%B%A;
	OPack.SaveFolderInfo=Opk!SaveFolderInfo;
	loop(ii,1,Opk!FileList.NumItems)
	{
		OPack.File$(ii)$=Opk!FileList.v$(ii)_1$;
	}
	ii=Opk!UserAccess.choice-1;
	switch(opack.pack(ii))
	{
		case 0:
			type -v -b $OPK.PackSuccess;
			break;
		case 2:
			type -b $OPK.ErrFileIO;
			break;
		case 6:
			type -b $OPK.ErrCreateFile;
			break;
	}
	return 0;
