<?php
    // Finds the name of the APOD (Astronomy Picture Of the Day) from 
    // the URL, downsizes it to a width of 100 pixels and proportional
    // height, then uploads it to polaris.umuc.edu/~sdsilva/ where it
    // is accessible from any webpage.

    require_once ("createThumb.php");

    $thumbWidth=100;    // determines the width of the thumb
    $dateToday = date("ymd");
    $url = "http://antwrp.gsfc.nasa.gov/apod/ap".$dateToday.".html";
    $lines=file($url);
    $date = date("ym");
    $imgUrl = "http://antwrp.gsfc.nasa.gov/image/";


    // Reads the html document to find the line that contains "href="image$date/"
    // Then gets the image name $newTmb
    foreach($lines as $line)
    {
        $line=rtrim($line);
        //xif(strpos(strtolower($line),"href=\"image/$date/")){
        if(strpos(strtolower($line),"img src=\"image/")){
            $strLength = strlen($line);
            $startPos  = strpos(strtolower($line),"href=\"image/$date/")+21;
            $newTmb = substr($line,$startPos,$strLength-$startPos-1);
            $date   = substr($line,$startPos-5,4);
            print $date;
            print "\n";
            print $newTmb;
            print "\n";
            print strlen($line);
            print "\n";
            $imgUrl = $imgUrl.$date."/";
            print $imgUrl;
            print "\n";
            break;
        }
    }



    $s_img = $imgUrl.$newTmb;           // source image
    print $s_img."\n";
    $d_img = "apod.jpg";                // destination image
    // converts the image into a thumb of width = $thumbWidth and proportional height
    $thmb = new createThumb($s_img,$d_img,$thumbWidth);
    $thmb->image2Thumb();

    // uploads it to sdsilva@polaris.umuc.edu:www/
    `rsync -avz --stats --rsh=ssh  $d_img sdsilva@polaris.umuc.edu:www/`;


?>