; wpt-get BUILD 124, minx
#include <Process.au3>
#include <Inet.au3>
#include <File.au3>
#include <String.au3>

If $CmdLine[0] > 3 Then 
	ConsoleWrite("ERROR: Too much paramters")
	Exit
ElseIf $CmdLine[0] < 2 Then
	ConsoleWrite("what.")
	Exit
EndIf

HttpSetUserAgent("wpt-get")

$Command = $CmdLine[1]
$Source = $CmdLine[2]

$fPackets = @userprofiledir&"\.packets"
If Not FileExists($fPackets) Then
	MsgBox(0, "wpt", "wpt-get will now setup the default packet list.")
	FileWrite($fPackets, "http://apaste.square7.ch/")
EndIf

Switch $Command	
	Case "list"
		If $Source = "available" Then
			For $i = 1 To _FileCountLines($fPackets)
				$Av = _INetGetSource(filereadline($fPackets, $i)&"packets.txt")
				ConsoleWrite($Av & @crlf)
			Next
			ConsoleWrite("You're welcome.")
		ElseIf $Source = "rep" Then
			For $i = 1 To _FileCountLines($fPackets)
				$Av = filereadline($fPackets, $i)
				ConsoleWrite($Av & @crlf)
			Next
			ConsoleWrite("No problem.")
		EndIf
	Case "remove"
		$Dir = @UserProfileDir & "\" & $Source
		If Not FileExists($Dir) Then
			ConsoleWrite("This packet is not installed yet. (Includes packet can't be uninstalled!")
		Else
			ConsoleWrite("Trying to remove " & $Source & @crlf)
			DirRemove($Dir, 1)
			If Not @error  And Not FileExists($Dir) Then
				ConsoleWrite("== Packet removed. ==")
			Else
				ConsoleWrite("== There was an error. Sorry. ==")
			EndIf
		EndIf
	Case "add"
		ConsoleWrite("You are about to add " & $Source & " to the packet list. I hope it is valid!" & @CRLF)
		FileWriteLine($fPackets, @CRLF & $Source)
		ConsoleWrite("Added.")
		Exit
	Case "install" Or ".install"
		ConsoleWrite("Reading packetlists: ")
		ConsoleWrite(_FileCountLines($fPackets) & @crlf)
		$Name = $Source & ".zip"
		$sProgress = ""
		$Path = @TempDir & "\"&$Name
		$Server = ""
		If FileExists($Path) Then FileDelete($Path)
		For $i = 1 To _FileCountLines($fPackets)
			$Av = _INetGetSource(filereadline($fPackets, $i)&"packets.txt")
			If StringInStr($Av, stringtrimright($Name,4)) Then $Server = filereadline($fPackets, $i)
		Next
		If $Server == "" Then
			ConsoleWrite("The packet was not found on any server. Sorry.")
			Exit
		EndIf

		$hDownload = InetGet($Server & $Name, $Path, 1, 1)
		ConsoleWrite("Downloading " & StringTrimRight($Name,4))
		$Size = InetGetInfo($hDownload, 1)
		$Timer = TimerInit()
		Do
			If TimerDiff($Timer) >= 500 Then
				ConsoleWrite(".")
				$Timer = TimerInit()
			EndIf
		Until InetGetInfo($hDownload, 2)
		$nBytes = InetGetInfo($hDownload, 0)
		InetClose($hDownload)
		ConsoleWrite(@CRLF & "Size was " & Int($nBytes/1000) & "KB" & @CRLF)
		ConsoleWrite("Installing..." & @CRLF)
		If Not FileExists(@ProgramFilesDir&"\AutoIt3\Include\") And $Command = ".install" Then
			ConsoleWrite("Include folder not found! Sorry.")
			FileDelete($Path)
			Exit
		EndIf
		If $Command = ".install" Then
			$Dir = @ProgramFilesDir&"\AutoIt3\Include\"
		Else
			$Dir = @UserProfileDir & "\" & StringReplace($Name,".zip","") & "\"
			If FileExists($Dir) And $CmdLine[3] = "-r" Then DirRemove($Dir)
		EndIf
		$Ret = _BuildInZIP($Path, $Dir, True)
		If $Ret == 1 Then
			ConsoleWrite("== Sucessfully installed " & StringTrimRight($Name,4) & ". ==")
		Else
			ConsoleWrite("== There was an error. Sorry. ==")
		EndIf
		FileDelete($Path)
EndSwitch

Func SSB($ss, $s, $e)
	$a = _StringBetween($ss, $s, $e)
	Return $a[0]
EndFunc

; BugFix
Func _BuildInZIP($sPath, $s_a_ToZip, $fUnZip=False, $iIndexBase=1, $fOverwrite=True)
    Local $fh, $sNul = '', $sZIPHeader, $objShell = ObjCreate("Shell.Application"), $objZipOrdner, $err = 0
    $sPath = FileGetLongName($sPath, 1)
    If $fUnZip Then
        Local $temp, $objZip
        If Not FileExists($s_a_ToZip) Then DirCreate($s_a_ToZip)
        $temp = $objShell.Namespace($s_a_ToZip)
        $objZip = $objShell.Namespace($sPath)
        $temp.CopyHere($objZip.Items)
        Return 1
    EndIf
    If (Not $fOverwrite) And FileExists($sPath) Then Return SetError(1,0,0)
    If StringRight($sPath, 4) <> '.zip' Then Return SetError(2,0,0)
    For $i = 1 To 18
        $sNul &= Chr(0)
    Next
    $sZIPHeader = Chr(80) & Chr(75) & Chr(5) & Chr(6) & $sNul
    $fh = FileOpen($sPath, 10)
    FileWrite($fh, $sZIPHeader)
    FileClose($fh)
    $objZipOrdner = $objShell.Namespace($sPath)
    If Not IsArray($s_a_ToZip) Then
        If Not FileExists($s_a_ToZip) Then Return SetError(3,1,0)
        $objZipOrdner.CopyHere($s_a_ToZip)
        Sleep(1000)
        Return 1
    EndIf
    For $i = $iIndexBase To UBound($s_a_ToZip) -1
        If Not FileExists($s_a_ToZip[$i]) Then
            $err += 1
            ContinueLoop
        EndIf
        $objZipOrdner.CopyHere($s_a_ToZip[$i])
        Sleep(1000)
    Next
    If $err Then Return SetError(3,$err,0)
    Return 1
EndFunc  ;==>_BuildInZIP
	