#!/bin/bash

headOut()
{
animeIn=$(echo "$QUERY_STRING" | sed -n 's/^.*anime=\([^&]*\).*$/\1/p')
touch ../tmp/tmpShow ../tmp/animeShow
wget http://www.crunchyroll.com/"${animeIn}" -O ../tmp/tmpShow
sed 1,335d ../tmp/tmpShow > ../tmp/animeShow

IFS=$'\n'
bgs=($(ls -L ../backgrounds/))
numdirs=${#bgs[@]}
index=$(( (( RANDOM % $numdirs ) - 2) + 1 ))

if [ -f ./000.jpg ]; then
bg="<div id="bg"><img src="./000.jpg"/></div>";
else
bg="<div id="bg"><img src="\"../backgrounds/${bgs[index]}"\"/></div>";
fi
	echo '<!doctype html>
<html lang="en">
<head>
	<title>MiniRoll App</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link rel="stylesheet" href="../themes/bodyCss.css" type="text/css">
	<link rel="stylesheet" href="../themes/menuCss.css" type="text/css">
</head>
<body>
'${bg}'
<center>
<ul><a href="../index.html"><li>Home</li></a>
  <li>
    Layout
    <ul>
      <a href="../cgi-bin/listOnLeftTheme.cgi"><li>List On The Left</li></a>
      <a href="../cgi-bin/gridTheme.cgi"><li>Grid</li></a>
      <a href="../cgi-bin/centerList.cgi"><li>List Centered</li></a>
    </ul>
  </li>
  <li>About</li>
<a href="#" onclick="location.reload(true); return false;"><li>Refresh</li></a>
<a href="../cgi-bin/quit.cgi"><li>Quit</li></a>
</ul>
</center>
<br>
' > ../tmp/list.html
main
}

main()
{
rm ../tmp/titles ../tmp/imgList
sed -n 's/.*title="\([^"]*\).*/\1/p' ../tmp/animeShow > ../tmp/tmptitles
sed -n 's/.*src="\([^"]*\).*/\1/p' ../tmp/animeShow > ../tmp/tmpimgList
sed -n 's/.*data-thumbnailUrl="\([^"]*\).*/\1/p' ../tmp/animeShow >> ../tmp/tmpimgList
sed -n 's/.*media_id="\([^"]*\).*/\1/p' ../tmp/animeShow > ../tmp/mediaIds
d=$(cat ../tmp/mediaIds | wc -l) >> /dev/null ;
junkCount="1"
i="1"
    while [ $i -le $d ]; do
	      title=$(sed -n "${i}p" ../tmp/tmptitles);
        if [[ "${title}" == *"Episode"* ]]; then
             echo "${title}" >> ../tmp/titles
        else
	           junkCount=$[$junkCount++1];
        fi
	    i=$[$i++1];
done
i="1"
    while [ $i -le $d ]; do
	      image=$(sed -n "${i}p" ../tmp/tmpimgList);
        if [[ "${image}" == *".jpg"* && \
              "${image}" != *"_full.jpg"* && \
              "${image}" != *"_thumb.jpg"* ]]; then
             echo "${image}" >> ../tmp/imgList
        else
	           junkCount=$[$junkCount++1];
        fi
	    i=$[$i++1];
done
i="1"
    while [ $i -le $d ]; do
	    title=$(sed -n "${i}p" ../tmp/titles);
      imgLnk=$(sed -n "${i}p" ../tmp/imgList)
	    meiaID=$(sed -n "${i}p" ../tmp/mediaIds);
      vdoLnk="http://static.ak.crunchyroll.com/versioned_assets/StandardVideoPlayer.cc7e8515.swf?config_url=http%3A%2F%2Fwww.crunchyroll.com%2Fxml%2F%3Freq%3DRpcApiVideoPlayer_GetStandardConfig%26media_id%3D"$meiaID"video_format%3D106%26video_quality%3D61"
	    echo '
<div class="lnksNdirs">
   <a target="frame" href="'${vdoLnk}'" style="color:#00E8FF;">
   <img src="'${imgLnk}'" /> <br>
     '${title}'</a>
</div>' >> ../tmp/list.html ;
	    i=$[$i++1];
done
endOut
}

endOut()
{
    echo '
<iframe id="frame" name="frame" frameborder="0" class="iframer"></iframe>
<iframe id="invisFrame" name="invisFrame" frameborder="0"></iframe>
</body>
</html>' >> ../tmp/list.html
redirect
}
redirect()
{
echo "Content-type: text/html"
echo ""
echo "<html>"
echo  "<head>"
echo    "<title>Loading...</title>"
echo    "<meta http-equiv='refresh' content='1;URL=../tmp/list.html'>"
echo  "</head>"
echo  "<body style='background-color:#000000'>"
echo   "<center><font color='#FFFFFF'>You will be redirected in a few seconds...</font></center>"
echo  "<img style='width:30em;height:30em;margin-left:50%;margin-top:15%;' src='../icons/loading.gif'></img>"
echo  "</body>"
echo "</html>"
}
headOut