'===========================================================================
' Subject: UNZIPPER UTILITY                   Date: 04-25-00 (18:14)       
'  Author: Jeremiah "BJ" Hyde                 Code: QB, QBasic, PDS        
'  Origin: fishoffire@yahoo.com             Packet: MISC.ABC
'===========================================================================
'****************************************************************
'* UZ.BAS   - UnZipper v1.0                                     *
'*                                                              *
'* This is

'*        Copyright (c) 2000 Jeremiah "BJ" Hyde                 *
'*      Released under GNU General Public License               *
'****************************************************************
'****************************************************************

' $INCLUDE: 'qb.bi'

' This will tell you if a file or file group exists
' From Abacus QuickBASIC Toolbox, by Stefan A. Dittrich
DECLARE FUNCTION Exists% (FileName$)

' This parses the command line up into it's parts
' Heavily modified from a PD source by J. Derek Lyons(ABC November 1991)
DECLARE FUNCTION PrsCmdLn% (CmdLine$, Args$())

' This Parse()'s a string up into it's parts.
' Lightly modified from code in "The Visual Basic 4.0 Power
' Toolkit", by Richard Mansfield and Evangelos Petroutsos
DECLARE FUNCTION Parse$ (Position&, Work$, Delimit$)

' This will align(left, center, or right) a
' string of text in a certain field. By BJ(That's me!)
DECLARE FUNCTION Align$ (Text$, FieldWidth%, AlignType%, PadWith$)
CONST alnLeft = 1       ' Left-align
CONST alnRight = 2      ' Right-align
CONST alnCenter = 3     ' Center-align

' Print a short, simple help message. By BJ(Me again!)
DECLARE SUB DoHelp ()

IF COMMAND$ = "" THEN DoHelp: END

' Get the command tail(might use a diff. fn() here)
CmdLine$ = COMMAND$
' Grab additional PKZip/UnZip commands(if any)
IF INSTR(CmdLine$, "/CMD:") > 0 THEN
	PKCmdLine$ = MID$(CmdLine$, INSTR(CmdLine$, "/CMD:") + 5)
	CmdLine$ = MID$(CmdLine$, 1, INSTR(CmdLine$, "/CMD:") + 5)
END IF

' Does the user want help?
IF INSTR(CmdLine$, "/?") > 0 THEN DoHelp: END

' Now, we parse out the arguments
DIM Args$(1 TO 10)             'Array to hold the arguments
i% = PrsCmdLn%(CmdLine$, Args$())
IF i% <> 0 THEN PRINT "Problem parsing command line.": END

FOR i% = 1 TO 10
	SELECT CASE Args$(i%)
		CASE "M"            'Don't move the .ZIP file
			DontMoveZIPFile% = -1
		CASE "D"            'UnZIP to the current directory
			DontMakeNewDir% = -1
			DontMoveZIPFile% = -1
		CASE "R"            'Don't recreate stored sub-dirs
			DontRecreateStoredSubDirs% = -1
		CASE ""             ' No argument, ignore
		CASE ELSE
			IF Exists%(Args$(i%)) THEN
				FileName$ = Args$(i%)
			ELSE
				IF Exists%(Args$(i%) + ".ZIP") THEN
					FileName$ = Args$(i%) + ".ZIP"
				ELSE
					' It might be a destination directory!
					DirName$ = Args$(i%)
				END IF
			END IF
	END SELECT
NEXT i%
IF DirName$ > "" AND FileName$ = "" THEN
	PRINT "'" + DirName$ + "' does not exist."
	END
END IF
IF FileName$ = "" THEN DoHelp: END
IF NOT DontMakeNewDir% THEN
	FileTitle$ = Parse$(-1, FileName$, "\")
	FileNoExt$ = Parse$(0, FileName$, ".")
	IF DirName$ > "" THEN FileNoExt$ = DirName$
	ON ERROR GOTO DirExists
	MKDIR FileNoExt$
	FileNoExt$ = FileNoExt$ + "\"
	PKCmdLine$ = " -d " + PKCmdLine$
END IF
PKCmdLine$ = "PKUnZIP.EXE -^ -en" + PKCmdLine$ + " " + FileName$ + " " + FileNoExt$
SHELL PKCmdLine$

IF NOT DontMoveZIPFile% THEN NAME FileName$ AS FileNoExt$ + "\" + FileName$

END

DirExists:
PRINT "Warning: The destination directory already exists."
PRINT "Do you still want to continue?"
DO UNTIL tmpAnswer$ = "Y" OR tmpAnswer$ = "N"
	tmpAnswer$ = UCASE$(INPUT$(1))
LOOP
IF tmpAnswer$ = "Y" THEN RESUME NEXT
END

FUNCTION Align$ (Text$, FieldWidth%, AlignType%, PadWith$)
SELECT CASE AlignType%
		CASE alnLeft
				Align$ = LEFT$(Text$ + STRING$(FieldWidth%, PadWith$), FieldWidth%)
		CASE alnRight
				Align$ = RIGHT$(STRING$(FieldWidth%, PadWith$) + Text$, FieldWidth%)
		CASE alnCenter
				Align$ = STRING$((FieldWidth% - LEN(Text$)) \ 2, PadWith$) + Text$ + STRING$((FieldWidth% - LEN(Text$)) \ 2, PadWith$)
END SELECT
END FUNCTION

SUB DoHelp
PRINT
PRINT Align$("UnZipper v1.0", 80, alnCenter, " ")
PRINT Align$("Copyright (c) 1999 Jeremiah 'BJ' Hyde", 80, alnCenter, " ")
PRINT
PRINT "Unzips a .ZIP file to a new directory, named after the file, and"
PRINT "deletes the .ZIP file."
PRINT
PRINT
PRINT "UZ[.EXE] [d:\][path\]filename[.zip] [destdir\] [/K] [/D] [/R] [/CMD:command]"
PRINT
PRINT "  UZ[.EXE]         Program name"
PRINT "  [d:\][path\]     Location of the .ZIP file"
PRINT "  filename[.ZIP]   Name of the .ZIP file"
PRINT "  destdir\         Directory to unZIP to(Instead of one named after the file"
PRINT "  /M               Don't move the .ZIP file when done"
PRINT "  /D               Don't make a new directory for the files"
PRINT "  /R               Don't recreate stored sub-directories"
PRINT "  /CMD:command     Pass an additional command to PKUnZIP(Not validated)"
PRINT
END SUB

FUNCTION Exists% (FileName$)
 '*** Checks whether file or file group FileName$ exists
 DIM InRegs AS RegType, OutRegs AS RegType
 dta$ = STRING$(80, " ")

 InRegs.ax = &H1A00             '*** SetDTA
 InRegs.dx = SADD(dta$)
 CALL INTERRUPT(&H21, InRegs, OutRegs)

 FilN$ = FileName$ + CHR$(0)
 InRegs.ax = &H4E00             '*** Find first
 InRegs.cx = 0                  '*** File and directories
 InRegs.dx = SADD(FilN$)
 CALL INTERRUPT(&H21, InRegs, OutRegs)

 Exists% = (OutRegs.ax = 0)
END FUNCTION

FUNCTION Parse$ (Position&, Work$, Delimit$)
IF INSTR(Work$, Delimit$) = 0 THEN
	Parse$ = Work$
ELSEIF Position& = 0 THEN
	Parse$ = MID$(Work$, 1, INSTR(Work$, Delimit$) - 1)
ELSE
	Parse$ = Parse$(Position& - 1, MID$(Work$, INSTR(Work$, Delimit$) + LEN(Delimit$)), Delimit$)
END IF
END FUNCTION

' Error codes returned:
'    0      Success
'   -1      Arguments array must start at 1(LBound=1)
'   -2      No arguments to parse
'   -3      Too many arguments on command line(Increase UBound)
FUNCTION PrsCmdLn% (CmdLine$, Args$())
' Initialize
MaxNumArgs% = UBOUND(Args$)
IF LBOUND(Args$) <> 1 THEN PrsCmdLn% = -1: EXIT FUNCTION

NumArgs% = 1: ArgPos% = 1
CLine$ = LTRIM$(RTRIM$(CmdLine$))
LenCmd% = LEN(CLine$)

' No command line arguments, quit processing
IF LenCmd% = 0 THEN PrsCmdLn% = -2: EXIT FUNCTION

' The actual work
FOR i% = 1 TO LenCmd%
	tmpChar$ = MID$(CLine$, i%, 1)
	IF tmpChar$ = " " OR tmpChar$ = "/" THEN
		ArgPos% = ArgPos% + 1
		IF ArgPos% > MaxNumArgs% THEN PrsCmdLn% = -3: EXIT FOR
	ELSE
		Args$(ArgPos%) = Args$(ArgPos%) + tmpChar$
	END IF
NEXT i%

END FUNCTION
