#!/bin/sh
export PATH=$PATH:/home/user/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/\usr/local/sbin:/usr/local/bin
export LD_LIBRARY_PATH=/home/user/lib
export PERL5LIB="/home/user/lib/perl5/lib/perl5:/home/user/lib/perl5/li\b/perl5/perl5/i386-freebsd-64int"

# args check
if [ $# -ne 8 ]; then
	echo "usage : $0 OUTFILE_SUFFIX  REC_TIME(s)  "\
	     "STREAM(V/A)  UPLOAD(0/1) COMMENT  DL_PASS  RM_PASS  BIWEEK(0/1)"
	exit 1;
fi

SUFFIX=AG-$1
REC_TIME=$2
STREAM=$3
UPLOAD_F=$4
COMMENT=$5
DL_PASS=$6
RM_PASS=$7
BIWEEK_F=$8

ftpaccount_pass='xxxxxx:yyyy;
ftpurl="ftp://ftp.4shared.com";
working_dir=/home/user/REC
check_biweek_dir=/home/user/100

now=`date '+%Y-%m-%d-%H%M'`;
ftpdir=Public/Radio/`date '+%Y-%m'`;

check_date=`date -v -1w '+%Y-%m-%d-%H%M'`;
check_file=$check_date-$SUFFIX

if ( [ $BIWEEK_F -eq 9 ] ); then
	today=`date -v -6H '+%d'`;
	weekly=`date -v -6H '+%u'`;

        checkmonday=$(expr $today - $weekly  + 1);

        if ( [ $checkmonday -le 0 ] ); then
                exit 0;
        fi
        if ( [ $checkmonday -ge 8 ] && [ $checkmonday -le 14 ] ); then
                exit 0;
        fi
        if ( [ $checkmonday -ge 22 ] && [ $checkmonday -le 28 ] ); then
                exit 0;
        fi
fi

if ( [ $BIWEEK_F -eq 1 ] && [ -f $check_biweek_dir/$check_file ] ); then 
	rm -f $check_biweek_dir/$check_file;
	exit 0;
fi

cd $working_dir

outfile=$now-$SUFFIX;

[ $BIWEEK_F -eq 1 ] && touch "$check_biweek_dir/$outfile";



#
# rtmpdump
#
RETRYCOUNT=0
while :
do
	rtmpdump -q -vr "rtmp://fms1.uniqueradio.jp/" \
		 -a "?rtmp://fms-base1.mitene.ad.jp/agqr/" \
		 -f "WIN 11,3,300,257" \
		 -W "http://www.uniqueradio.jp/agplayerf/LIVEPlayer.swf" \
		 -p "http://www.uniqueradio.jp/agplayerf/newplayerf2-sp.php" \
	         -C B:0 -y "aandg1" \
                 --stop ${REC_TIME} \
		 -o $outfile

  if [ $? -ne 1 -o `wc -c $outfile | awk '{print $1}'` -ge 10240 ]; then
    break
  elif [ ${RETRYCOUNT} -ge 5 ]; then
    echo "failed rtmpdump"
    exit 1
  else
    RETRYCOUNT=`expr ${RETRYCOUNT} + 1`
  fi
done
####


####
if ( [ $STREAM = 'V' -o $STREAM = 'v' ] ); then
	mv $outfile $outfile.flv
	outfile=$outfile.flv
else
	ffmpeg -i $outfile -vn -acodec copy $outfile.m4a >/dev/null 2>/dev/null
	rm -f $outfile
	outfile=$outfile.m4a
fi

gpg --options /home/user/.gnupg/opt.txt $outfile

DB=/home/user/.gnupg/Sessionkeys.db
key=`gpg -o /dev/null --batch --show-session-key $outfile.gpg 2>&1|
	perl -ne 'print $1 if (/gpg: session key:\s+.(\w+:\w+)/)'`
sqlite3 $DB "insert into sKey values('$outfile.gpg', '$key');"  

Update-crk.sh $working_dir/$outfile.gpg



#curl -s \
#        -T "$upfile" \
#        -u "$ftpaccount_pass" \
#        --retry 20 \
#        --retry-delay 10 \
#        --connect-timeout 300 \
#	--retry-delay 10 \
#        --ftp-create-dirs \
#        $ftpurl/$ftpdir/ > /dev/null

curl -s \
        -T "$outfile.gpg" \
        -u "$ftpaccount_pass" \
        --retry 20 \
        --retry-delay 10 \
        --connect-timeout 300 \
        --ftp-create-dirs \
        $ftpurl/$ftpdir/  >/dev/null


curl -s \
        -T "$outfile.gpg" \
        -u xxxxx:yyyyy \
        --retry 2 \
       --retry-delay 2 \
        --connect-timeout 30 \
	--ftp-create-dirs \
        ftp://agqr.tk/public_html/Radio/`date '+%Y-%m'`/ >/dev/null


curl -s \
        -T "$outfile.gpg" \
        -u 'xxxxx:yyyyy' \
        --retry 10 \
        --retry-delay 10 \
        --connect-timeout 120 \
	--ftp-create-dirs \
        ftp://anispa.tk/public_html/Radio/`date '+%Y-%m'`/ >/dev/null


TW.pl "[$outfile.gpg] is uploaded." >/dev/null

rm -f $outfile.gpg

exit 0;
