#!/bin/bash

VERSION='2013/06/02 18:19:49 UTC'

downfiles()
{
TARGET=${1}
ADDURL1=${2}
ADDURL2=${3}
mkdir -p ${TARGET}
cd ${TARGET} || exit 1

TMPFILE1="`mktemp`"
TMPFILE2="`mktemp`"
while read -r codeline
do
line=${codeline#*code=}  # 最初のcode=より後ろを取り出す
line=${line%%/*}  # 最初の/より前の文字列を取り出す   
SCRAMBLECODE=$(eval echo ${line})
echo ${SCRAMBLECODE} && echo
URL=${BASEURL}${ADDURL1}/${SCRAMBLECODE}/${ADDURL2}

${WGET} -U "${UAG}" -O "${TMPFILE1}" ${URL}
if test $? -eq 0; then
grep \.${TARGETEXT} "${TMPFILE1}">"${TMPFILE2}"
while read -r line
do
echo "${line}"
line=${line#*title=}  # 最初のtitle=より後ろの文字列
line=${line#*\"}  # 最初の"より後ろの文字列を取り出す
title=${line%%\"*}  # 最初の"より前の文字列を取り出す   
echo "${title}"
line=${line#*hdate=}  # 最初のhdate=より後ろの文字列
line=${line#*\"}  # 最初の"より後ろの文字列を取り出す
hdate=${line%%\"*}  # 最初の"より前の文字列を取り出す   
echo "${hdate}"
line=${line#*file=}  # 最初のfile=より後ろの文字列
line=${line#*\"}  # 最初の"より後ろの文字列を取り出す
file=${line%%\"*}  # 最初の"より前の文字列を取り出す   
file=${file%*\.${TARGETEXT}}  # 指定拡張子の切取をおこなう
echo "${file}"
TARGETFILE="${title}_${hdate}.${TARGETEXT}"
TARGETM4A="${title}_${hdate}.m4a"
utf8title="`echo "${title}_${hdate}"`"
utf8Atitle="`echo "${title}"`"
cp932title="`echo "${utf8title}"|iconv -f UTF-8 -t CP932`"
cp932Atitle="`echo "${utf8Atitle}"|iconv -f UTF-8 -t CP932`"
if !(test -e "${TARGETM4A}") then
	currentdir="`pwd`"
	TMPFILE3="`mktemp "${currentdir}/tmp.XXXXXXXXXX"`"
	TMPFILE4="`mktemp "${currentdir}/tmp.XXXXXXXXXX"`"
	TMPMP4="${TMPFILE4}.mp4"
	mv "${TMPFILE4}" "${TMPMP4}"
	TMPFILE4="${TMPMP4}"
	TARGETFILE="${TMPFILE3}"
	${FLVSTREAMER} -o "${TARGETFILE}" -r "${FLVBASEURL}${SCRAMBLECODE}/${file}"
	test $? -ne 0 && test -e "${TARGETFILE}" && rm "${TARGETFILE}"
	test -e "${TARGETFILE}" && ! test -s "${TARGETFILE}" && rm "${TARGETFILE}"
	if test -e "${TARGETFILE}"; then
		ffmpeg -y -i "${TARGETFILE}" -acodec copy -f mp4 "${TMPFILE4}" </dev/null
		test $? -ne 0 && test -e "${TMPFILE4}" && rm "${TMPFILE4}"
		test -e "${TMPFILE4}" &&  mv "${TMPFILE4}" "${TARGETM4A}"
		rm "${TMPFILE3}"
	else
		rm "${TMPFILE4}"
	fi
	if test -e "${TARGETM4A}"; then
		chmod go+r "${TARGETM4A}"
		# Linux向き
#		id3v2 -g 101 -A "${utf8Atitle}" -t "${utf8title}" "${TARGETMP3}"
		# Windows, 携帯プレーヤー向き
#		id3v2 -g 101 -A "${cp932Atitle}" -t "${cp932title}" "${TARGETMP3}"
	fi
else
	echo "${TARGETM4A} already exists"
fi
done<"${TMPFILE2}"
fi
done<"${TMPFILE6}"
rm "${TMPFILE1}" "${TMPFILE2}"
cd ..
}

get_code()
{
TMPFILE5="`mktemp`"
#curl -o "${TMPFILE5}" ${CODEURL}
${WGET} -U "${UAG}" -O "${TMPFILE5}" ${CODEURL}
if test $? -eq 0; then
TMPFILE6="`mktemp`"
egrep "scramble [^\=]+\=" ${TMPFILE5}|sort -r>${TMPFILE6} 
#grep "scramble date=" ${TMPFILE5}>${TMPFILE6} 
fi
rm "${TMPFILE5}"
testline="$(cat ${TMPFILE6})"
if test -z "${testline}"; then
echo "Failed to retrieve scramble code"
rm "${TMPFILE6}" && exit 1
fi
}

exitproc(){ rm -rf "${TMPFILE1}" "${TMPFILE2}" "${TMPFILE3}"
 "${TMPFILE4}" "${TMPFILE5}" "${TMPFILE6}";}
trap '
exitproc
exit 1
' 2 15

WGET="wget -t 1 -T 15"
UAG="Mozilla/5.0 (Windows NT 5.1; rv:2.0) Gecko/20100101 Firefox/4.0"
FLVSTREAMER="flvstreamer -m 15"

# NHK語学講座用
BASEDIR="/home/disklessfun/Documents/NHK語学講座" # 必ず絶対パス
BASEURL="http://www.nhk.or.jp/gogaku/english/"
SCRAMBLECODE=
FLVBASEURL="rtmp://flv.nhk.or.jp/ondemand/mp4:flv/gogaku/streaming/mp4/"
TARGETEXT=mp4
mkdir -p "${BASEDIR}"
cd "${BASEDIR}" || exit 1

# NHK語学講座ダウンロード @ ウィキ - コード参照用xml
CODEURL="http://www47.atwiki.jp/jakago/pub/scramble.xml"
get_code

downfiles "01_基礎英語1"              "basic1"    "listdataflv.xml"
downfiles "02_基礎英語2"              "basic2"    "listdataflv.xml"
downfiles "03_基礎英語3"              "basic3"    "listdataflv.xml"
downfiles "04_英会話タイムトライアル" "timetrial" "listdataflv.xml"
downfiles "05_ラジオ英会話"           "kaiwa"     "listdataflv.xml"
downfiles "06_入門ビジネス英語"       "business1" "listdataflv.xml"
downfiles "07_実践ビジネス英語"       "business2" "listdataflv.xml"
rm ${TMPFILE6}
