#!/usr/bin/perl
#usage recwrap.pl ch length(sec) [bitrate(5)] [TID] [NO] [PID] [stationid] [digitalflag] [digitalband] [digitalch] 
#
# Anime recording system foltia
# http://www.dcc-jpl.com/soft/foltia/
#
#
#レコーディングラッパ
#atから呼び出され、tvrecordingを呼び出し録画
#そのあとMPEG4トラコンを呼び出す
#
# DCC-JPL Japan/foltia project
#

use LWP::UserAgent;
use HTTP::Request::Common;
use Time::Local;
use Jcode;

$path = $0;
$path =~ s/recwrap.pl$//i;
if ($pwd  ne "./"){
push( @INC, "$path");
}

require "foltialib.pl";
#引き数がアルか?
$recch = $ARGV[0] ;
if ($recch eq "" ){
	#引き数なしで実行されたら、終了
	print "usage recwrap.pl  ch length(sec) [bitrate(5)] [TID] [NO] [PID]\n";
	exit;
}

$recch = $ARGV[0] ;
$reclength = $ARGV[1] ;
$bitrate  = $ARGV[2] ;
$tid  = $ARGV[3] ;
$countno  = $ARGV[4] ;
$pid = $ARGV[5] ;
$stationid = $ARGV[6] ;
$usedigital = $ARGV[7] ;
$digitalstationband = $ARGV[8] ;
$digitalch= $ARGV[9] ;
$tunerch = $ARGV[10] ;

system("$toolpath/perl/mklocalizeddir.pl $tid");

if ( $digitalch == 0 )
{
	&writelog("recwrap INVALID digital channel 0");
	exit;
}

$post_chs = 0;
$post_recch = 0;
$post_tunerch = 0;

if ( $digitalstationband == 0 ) { #Terra
	$post_chs = 0;
	$post_recch = $digitalch-13;
	$post_tunerch = $tunerch;
} elsif ( $digitalstationband == 1 ) { #BS
	$post_chs = 3;
	$post_recch = $digitalch;
	$post_tunerch = $tunerch;
	
if ( 0 ) {
	   if ( $digitalch == 101 ) { $post_recch = 9; } #NHK-BS1
	elsif ( $digitalch == 102 ) { $post_recch = 9; } #NHK-BS2
	elsif ( $digitalch == 103 ) { $post_recch = 10; }#NHK-BShi
	elsif ( $digitalch == 141 ) { $post_recch = 7; } #BS-NTV
	elsif ( $digitalch == 151 ) { $post_recch = 0; } #BS-Asahi
	elsif ( $digitalch == 161 ) { $post_recch = 1; } #BS-i
	elsif ( $digitalch == 171 ) { $post_recch = 3; } #BS-Japan
	elsif ( $digitalch == 181 ) { $post_recch = 8; } #BS-Fuji
	elsif ( $digitalch == 191 ) { $post_recch = 2; } #WOWOW
	elsif ( $digitalch == 192 ) { $post_recch = 2; } #WOWOW-2
	elsif ( $digitalch == 193 ) { $post_recch = 2; } #WOWOW-3
	elsif ( $digitalch == 200 ) { $post_recch = 5; } #Star Channel
	elsif ( $digitalch == 211 ) { $post_recch = 4; } #BS11
	elsif ( $digitalch == 222 ) { $post_recch = 6; } #TwellV
	else
	{
		&writelog("recwrap INVALID BS CHANNEL $digitalch");
		exit;
	}
	
} #end if(0)

} elsif ( $digitalstationband == 2 ) { #CS
	$post_chs = 4;
	$post_recch = $digitalch;
	$post_tunerch = $tunerch;
} elsif ( $digitalstationband == 100 ) { #AM radio (radiko)
	$post_chs = 100;
	$post_recch = $digitalch;
}

&writelog("recwrap RECSTART DIGITAL $digitalstationband $digitalch $reclength $stationid 0 $outputfilename $tid $countno PT1remote chs: $post_chs recch: $post_recch tunerch: $tunerch");

my $url = 'http://192.168.0.5:16000/test.phpcgi';
my %postdata = ( 'chs' => $post_chs, 'recch' => $post_recch, 'length' => $reclength, 'tid' => $tid, 'no' => $countno, 'pid' => $pid, 'staid'=> $stationid, 'digitalch'=> $digitalch, 'tunerch'=> $post_tunerch );
my $request = POST( $url, \%postdata );
my $ua = LWP::UserAgent->new;
$ua->request( $request );

