# FHTTP Kit by Xianur0
# Copyright (C) 2011 Oscar García López (http://hackingtelevision.blogspot.com) 
#  
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#   
#    xianur0.null@gmail.com
#    http://hackingtelevision.blogspot.com/


sub instalado {
	my $nombre = $_[0];
	$nombre =~ s/::/\//g;
	foreach my $dir (@INC) {
		if(open(TMP,"<".$dir."/".$nombre.".pm") && close(TMP)) {
			return 1; 
		}
	}
	return 0;
}
our %mods= ("Filter::Util::Call",0,"Gtk2",1,"Socket",0,"Switch",0,"POSIX",0,"IO::Socket::INET",0,"IO::Socket::SSL",1,"threads",0,"Term::ANSIColor",1,"IO::Uncompress::Gunzip",1,"IO::Uncompress::Inflate",1,"threads::shared",0);
print "Comprobando dependencias...\n";
my $contadordeps = 0;
while(($key, $opcional) = each(%mods)){
	if(instalado($key)) {
		$mods{$key} = 1;
	} else {
		$mods{$key} = 0;
		if(!$opcional) {
			die("Error critico: no se encontro el pm ".$key." que es necesario para esta aplicacion!\n");
		} else {
			print "Warning: No se encontro el pm ".$key." pero no es una dependencia critica. Continuando...\n";
		}
		$contadordeps++;
	}
}
open(CONFIG,"config.fhttp");
my %inconfig = ();
while(my $lineaconfig = <CONFIG>) {
	my @partes = ($lineaconfig =~ /^([^=]+)(.*)$/);
	$partes[0] =~ s/\s+//g; 
	$partes[1] =~ s/^=// if($partes[1] ne "");
	$partes[1] = "nil" if($partes[1] eq "");
	$inconfig{$partes[0]} = $partes[1];
}
close(CONFIG);
my @configsdesactivables = ("Gtk2","IO::Socket::SSL");
my @cheats = ("--proxy","--tunnel-http","--proxy-list","--crlf-req","--crlf-res","--verbose","--lang");
foreach $arg (@ARGV) {
	FLAG: foreach $valor (@configsdesactivables) {
		if($arg eq ("no-".$valor)) {
			print $valor." desactivado!\n";
			$inconfig{"no-".$valor} = 1;
			last FLAG;
		}
		elsif($arg eq $valor) {
			print $valor." activado!\n";
			$inconfig{"no-".$valor} = 0;
			last FLAG;
		} else {
			COMPRUEBA: foreach $cheat (@cheats) {
				if($arg =~ /^$cheat=(.+)/) {
					$mods{"Cheat-".$cheat} = $1;
					print "Cheat activado: ".$cheat." = ".$1."\n";
					if($cheat eq "--crlf-req" || $cheat eq "--crlf-res") {
						$mods{"Cheat-".$cheat} =~ s/\\r/\r/g;
						$mods{"Cheat-".$cheat} =~ s/\\n/\n/g;
					}
					if($cheat eq "--lang") {
						$mods{$cheat} = $mods{"Cheat-".$cheat};
						$inconfig{$cheat} = $mods{"Cheat-".$cheat};
					}
					last FLAG;
				}
			}
		}
	}
}
open(CONFIG,">config.fhttp");
while(($clave, $valor) = each(%inconfig)){
	print CONFIG $clave."=".$valor."\n";
	$clave =~ s/^no-//;
	if($clave eq "--lang") {
		$mods{$clave} = $valor;
	} else {
		$mods{$clave} = ($valor == 1 ? 0 : 1);
	}
}
close(CONFIG);
print (($contadordeps > 0) ? ("[x] Estas a ".$contadordeps." de tener la FHTTP funcionando al 100% :P\n") : ("Felicidades: FHTTP funcionando al 100%!\n"));

my $included = 1;
eval {
	&main::included();
};
if($@) {
$included = 0;
}

return 1 if($included == 1);
