#!/usr/local/bin/perl
# socketœ̃y[W𔲂XNvg
# 8000

# gethtml
$get_url = "http://nyama.sytes.net:8000/";
# check镶
$check = "Server is currently up and";
# ev[gIɕۑt@C
$logfile = "./radiolog.txt";
# XVԊu 5Ȃ500
$updatetime_temp = 100;

# imgfBNg Ō / Kv
$noquery_imgurl = "http://laser.s7.xrea.com/";

# ڑ̃Ot@C
$uniqueip_log = "./radio_uniqueiplog.txt";

# ]t@C͒wȂ̂Œ
# [JfBNg
$ftp_local_dir = "./";

#--------------------------------------------------------------------

$ENV{TZ} = 'JST-9';
$times = time;
($sec,$min,$hour,$mday,$month,$year,$wday,undef,undef) = localtime($times);
$month++;
$year = 1900 + $year;
$actime = sprintf("%d%02d%02d%02d%02d%02d", $year, $month, $mday, $hour, $min, $sec);

open(IN,"<$logfile");
($onaircheck,$lastupdatetime) = split(/<>/, <IN>);
close(IN);

if($actime > $lastupdatetime + $updatetime_temp){
	$onairflag = &httpget;
	&wlog($onairflag,$actime);
	&printout($onairflag);
}else{
	&printout($onaircheck);
}
exit;

#--------------------------------------------------------------------

# Ԃ
sub printout{
	local($onair_c) = @_;
	if($onair_c){
		$reprint = "radio_onair3.gif";
	}else{
		$reprint = "radio_off4.gif";
	}
	# ڑ
	open(IN,"<$uniqueip_log");
		($radioip_print) = split(/<>/, <IN>);
	close(IN);
	if($radioip_print eq "--"){
		$uniqueip_print = "";
	}else{
		$uniqueip_print = "<font size=\"-1\">݂̐ڑ : $radioip_print (60bȓ)</font>";
	}
	@PAIRS = split(/&/,$ENV{'QUERY_STRING'});
	foreach(@PAIRS){
		($key, $value) = split(/=/, $_);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$QUERY{$key} = $value;
	}
	$QUERY{'imagedir'} = $noquery_imgurl if(!($QUERY{'imagedir'} =~ /http\:\/\//));
	# o
	if($QUERY{'mode'} eq "redirect"){
		print "Location: $QUERY{'imagedir'}$reprint\n\n";
        }else{
		print "Content-type: text/html\n\n";
		print "<a href=\"http://nyama.sytes.net:8000/listen.pls\"><img src=\"$QUERY{'imagedir'}$reprint\" alt=\"60bƂɍXV\" border=\"0\"></a> $uniqueip_print\n";
        }
}

# logɏ
sub wlog{
	local($onair,$lasttime) = @_;
	open(OUT,"+< $logfile");
	flock(OUT,2);
	truncate(OUT,0);
	seek(OUT,0,0);
	print OUT "$onair<>$lasttime<>\n";
	close(OUT);
}

# socketget
# ߂2000(www.amezo2000.com)̃XNvg
sub httpget{
	$get_url =~ /http:\/\/([^:\/]*)(:([0-9]+))?(\/.*)?/;
	$host = $1;
	$port = ($3 ne "") ? $3 : 80;
	$path = ($4 ne "") ? $4 : "/";
	$addr = (gethostbyname($host))[4];
	$name = pack("S n a4 x8", 2, $port, $addr);
	socket(S, 2, 1, 0);
	connect(S, $name);
	binmode(S);
	select(S); $| = 1; select(stdout);
	print S "GET $path HTTP/1.0\r\n"
		. "User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98)\r\n"
		. "Host: $host\r\n"
		. "Referer: $HEAD{'url'}\r\n\r\n";
	while(<S>){
		if ($_ eq "\n" || $_ eq "\r\n" || $_ eq "\r") { last; }
	}
	while(<S>){
		$data2 .= $_;
	}
	close(S);
	if($data2 =~ /$check/){
                if($data2 =~ /of 100 listeners \(([0-9]*) unique\)/){
			open(OUT,"+< $uniqueip_log");
			flock(OUT,2);
			truncate(OUT,0);
			seek(OUT,0,0);
			print OUT "$1<>\n";
			close(OUT);
                }else{
			open(OUT,"+< $uniqueip_log");
			flock(OUT,2);
			truncate(OUT,0);
			seek(OUT,0,0);
			print OUT "--<>\n";
			close(OUT);
                }
		return 1;
        }else{
		open(OUT,"+< $uniqueip_log");
		flock(OUT,2);
		truncate(OUT,0);
		seek(OUT,0,0);
		print OUT "--<>\n";
		close(OUT);
		return 0;
	}
}

