#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

	AutoIt Version: 3.3.6.1
	Author:         aldoniel cf http://aldoniel.free.fr


#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#include <Array.au3>
#include "ListDir.au3"
#include "Console.au3"

Global $ini = IniReadSection("html_patch_config.ini", "global")
If @error Or UBound($ini) < 3 Then
	Dim $ini[3][2]
	firstrun()
EndIf

Func firstrun()
	$iniwrite = FileOpen("html_patch_config.ini", 2)
	If @error Then
		MsgBox(0, "", "erreur d'écriture ini")
		fin()
	Else
		MsgBox(0, "", "Merci de sélectionner le répertoire racine, celui qui contient localhost et les répertoires en www.site.com")
		$ini[1][1] = FileSelectFolder("Racine ?", "", 4, @ScriptDir)
		If @error Then fin()
		MsgBox(0, "", "Merci de sélectionner la racine du site, celui qui contient index.html")
		$ini[2][1] = FileSelectFolder("Racine site ?", "", 4, $ini[1][1])
		If @error Then fin()
		FileWrite($iniwrite, "[global]" & @CRLF & "root=" & $ini[1][1] & @CRLF & "geekdir=" & $ini[2][1])
		FileClose($iniwrite)
		MsgBox(0, "", "html_patch_config.ini devrait avoir été crée dans le répertoire du script.")
	EndIf
EndFunc   ;==>firstrun


Global $root = $ini[1][1]
Global $geekdir = $ini[2][1]
main()

Func main()
	Local $tbl_files, $directorylist
	ListDir($tbl_files, $directorylist, $root, "*.*", False, False)
	$tbl_files = 0 ;don't need it
	If (BitAND(@error, 4) = 4) Then
		cout("Pas de répertoire dans " & $root & @LF, $FOREGROUND_RED)
		fin()
	EndIf

	;_ArrayDisplay($directorylist)
	; purge non domain.smthg like directories
	Local $tmpdirlist = ""
	For $i = 0 To UBound($directorylist) - 1
		If StringRegExp($directorylist[$i], ".+\..+") Then $tmpdirlist &= $directorylist[$i] & "*"
	Next
	$tmpdirlist = StringTrimRight($tmpdirlist, 1) ; remove last * ; entre nous, ça marche tout ça mais c'est dégueu comme code
	$directorylist = StringSplit($tmpdirlist, "*", 2)
	If @error Then
		cout("Pas de répertoire valide dans " & $root & @LF, $FOREGROUND_RED)
		fin()
	EndIf
	;_ArrayDisplay($directorylist)
	$tmpdirlist = ""

	Local $sizeoftable = UBound($directorylist)
	cout($sizeoftable & " répertoires trouvés." & @LF)

	Local $tbl_replace[$sizeoftable][2] ;creating a replacement table (decreases cpu load a bit...)
	For $i = 0 To ($sizeoftable - 1)
		$directorylist[$i] = StringTrimRight($directorylist[$i], 1)
		$tbl_replace[$i][1] = 'src="http://' & $directorylist[$i]
		$tbl_replace[$i][0] = 'src="../../' & $directorylist[$i]
	Next
	;_ArrayDisplay($tbl_replace)
	Local $nb_files = 1
	ListDir($tbl_files, $directorylist, $geekdir, "*.html", False) ; creating html list
	If (BitAND(@error, 2) = 2) Then
		cout("Pas de html dans " & $geekdir & @LF, $FOREGROUND_RED)
		fin()
	Else
		$nb_files = UBound($tbl_files)
		cout($nb_files & " fichiers html trouvés" & @LF)
	EndIf
	$directorylist = 0
	Local $filebuffer, $h_file
	For $i = 0 To ($nb_files - 1) ;patching html
		$filebuffer = FileRead($tbl_files[$i]) ; read only, one shot
		If @extended > 0 And Not @error Then ; if you read smthng; silentious error if you try to read a file open in write mode (2)...
			;$filebuffer=FileRead($h_file)
			;cout('@@ Debug(' & @ScriptLineNumber & ') : $filebuffer = ' & $filebuffer & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
			For $j = 0 To ($sizeoftable - 1)
				$filebuffer = StringReplace($filebuffer, $tbl_replace[$j][0], $tbl_replace[$j][1], 0, 2) ; tester 2 = quickreplace
			Next
			$h_file = FileOpen($tbl_files[$i], 130) ; write mode 2 + utf8 128
			If $h_file = -1 Then
				cout("Erreur d'ouverture : " & $tbl_files[$i] & @LF, $FOREGROUND_RED)
				FileClose($h_file)
				ContinueLoop
			EndIf
			If Not FileWrite($h_file, $filebuffer) Then
				cout("Erreur d'écriture : " & $tbl_files[$i] & @LF, $FOREGROUND_RED)
			Else
				cout(Int(100 / $nb_files * $i) & "% ", $FOREGROUND_GREEN)
				FileClose($h_file)
			EndIf
		Else
			cout("Erreur de lecture : " & $tbl_files[$i] & @LF, $FOREGROUND_RED)
			If FileGetSize($tbl_files[$i]) = 0 Then cout($tbl_files[$i] & " est vide" & @LF)
			ContinueLoop
		EndIf
	Next

EndFunc   ;==>main

;If FileExists("htmlcompressor-1.5.3.jar") Then compression()

Func compression()
	cout(@LF & "Lancement de htmlcompressor-1.5.3.jar" & @LF)
	$tempdir = @ScriptDir & "\htlmcompressor_temp"
	FileRecycle($tempdir)
	$dirsize = DirGetSize($geekdir, 2)
	If DirCreate($tempdir) Then
		system("java -jar htmlcompressor-1.5.3.jar -o " & $tempdir & " " & $geekdir)
		FileMove($tempdir & "\*.html", $geekdir, 1) ;overwrite
		If @error Then cout("Des erreurs copies sont survenues... utiser htlmcompressor à la main", $FOREGROUND_RED)
		FileRecycle($tempdir)
	Else
		cout("Erreur de création de " & $tempdir, $FOREGROUND_RED)
	EndIf
	cout("On a gagné environ " & Int(100 - (100 / $dirsize * DirGetSize($geekdir, 2))) & "% sur les html" & @LF)
EndFunc   ;==>compression

fin()
Func fin()
	cout(@LF)
	system("pause")
	Exit
EndFunc   ;==>fin