<?php
{
    function PrintPage($host, $user, $pw, $db)
    {
        $db_id = mysql_connect($host, $user, $pw);
        if ($db_id==FALSE)
        {
            printf("mysql_connect returned the following error: %s\n", mysql_error());
            die("");
        }
        mysql_select_db($db);

        $query1="SELECT MAX(fStartDate) FROM MarsVersion";
        $dateresult=mysql_query($query1);
        $row = mysql_fetch_row($dateresult);
        $date=$row[0];
        if (mysql_errno()>0)
            printf("&nbsp; Error-Number: %s <br>", mysql_errno());
        if (mysql_error()!="")
            printf("&nbsp; Error-Message: %s <br>", mysql_error());
        if (mysql_info()!="")
            printf("&nbsp; Mysql-Info: %s <br>", mysql_info());
        mysql_free_result($dateresult);
        if (empty($date))
        {
            printf("Could not get date of latest Mars version from database.");
            return;
        }

        if (empty($_GET["insert"]))
        {
            $query0  = "SELECT fSequenceFirst, ";
            $query0 .= "if (fCallisto " . " < '" . $date . "', 'callisto to be resetted', ";
            $query0 .= "if (not isnull(fCallisto), 'callisto already processed with new release', ";
            $query0 .= "if (fStartTime > ADDDATE(Now(), INTERVAL -1 DAY) AND isnull(fFailedTime), 'callisto running',";
            $query0 .= "if (isnull(fFailedTime), 'callisto already resetted for new release', ";
            $query0 .= " 'callisto crashed or failed')))) as 'calstat', ";
            $query0 .= "if (fStar " . " < '" . $date . "', 'star to be resetted', ";
            $query0 .= "if (not isnull(fStar), 'star already processed with new release', ";
            $query0 .= "if (fStartTime > ADDDATE(Now(), INTERVAL -1 DAY) AND isnull(fFailedTime), 'star running',";
            $query0 .= "if (isnull(fFailedTime), 'star already resetted for new release', ";
            $query0 .= " 'star crashed or failed')))) as 'starstat' ";
            $query0 .= "FROM SequenceProcessStatus ";
        }
        else
        {
            $query0 = "UPDATE SequenceProcessStatus SET ";
            if ($_GET["step"]=="callisto")
                $query0 .="fCallisto=NULL, fFillCallisto=NULL, fStar=NULL, fFillStar=NULL, ";
            else
                if ($_GET["step"]=="star")
                    $query0 .="fStar=NULL, fFillStar=NULL, ";
            $query0 .="fStartTime=NULL, fFailedTime=NULL, fFailedCode=NULL, fFailedCodeAdd=NULL, fReturnCode=NULL ";

            if ($_GET["step"]=="callisto")
                $query0 .= " WHERE fCallisto " . " < '" . $date . "'";
            else
                if ($_GET["step"]=="star")
                    $query0 .= " WHERE fStar " . " < '" . $date . "'";
                else
                    $query0 .= " WHERE (NOT ISNULL(fFailedTime) OR fStartTime < ADDDATE(Now(), INTERVAL -1 DAY))";
        }
        if (ereg("^([ ]*[0-9]{5,8}[ ]*[,])*[ ]*[0-9]{5,8}[ ]*$",$_GET["Seqs"]))
        {
            if (strpos($query0, " WHERE ")==FALSE)
                $query0 .= " WHERE ";
            else
                $query0 .= " AND ";
            $query0 .=" fSequenceFirst IN (" . $_GET["Seqs"] . ")";
        }
        else
        {
            echo "You have to give the sequences, which you want to reset, in the correct format, i.e. sequence numbers separated by ','.<br>";
            return;
        }
        if (empty($_GET["step"]))
        {
            echo "Please choose what you want to reset: <br>";
            echo "crashed processes (i.e. running longer than 1 day or failed), callisto or star.<br>";
            return;
        }

        $result0=mysql_query($query0);
        if (mysql_errno()>0)
            printf("&nbsp; Errorno: %s <br>", mysql_errno());
        if (mysql_error()!="")
            printf("&nbsp; Errormsg: %s <br>", mysql_error());
        if (!empty($_GET["insert"]) && !empty($_GET["step"]))
        {
            printf("%s Sequences have been resetted. <br><br>\n", mysql_affected_rows());
            if (mysql_info()!="")
                printf("Mysql-Info: %s <br><br>\n", mysql_info());
        }
        else
        {
            if (mysql_affected_rows()==0)
            {
                echo "No sequence to reset.<br>";
                echo "The sequences you listed are already resetted.<br>";
                mysql_free_result($result0);
                return;
            }
            echo "The status of the sequences you entered: <br>\n";
            $star="";
            $callisto="";
            $crashed="";
            $calcolor='black';
            $starcolor='black';
            $style='normal';
            $col   = FALSE;
            $bgcolor='#C0C0C0';
            printf("<table border='0'>\n");
            printf("<tr bgcolor='%s'>\n<th style='padding-left:1ex;padding-right:1ex'>\n<u>Sequence</u>\n</th>\n<th>\n<u>Status Callisto</u>\n</th>\n<th>\n<u>Status Star</u>\n</th>\n</tr>\n", $bgcolor);
            while ($row = mysql_fetch_row($result0))
            {
                if ($row[2]=='star to be resetted' && $_GET["step"]=='star')
                {
                    $star[]=$row[0];
                    $starcolor='green';
                        $style='bold';
                }
                if ($row[1]=='callisto to be resetted' && $_GET["step"]=='callisto')
                {
                    $callisto[]=$row[0];
                    $calcolor='green';
                    $style='bold';
                }
                if (($row[1]=='callisto crashed or failed' || $row[2]=='star crashed or failed') && $_GET["step"]=='crashed')
                {
                    $crashed[]=$row[0];
                    if ($row[1]=='callisto crashed or failed')
                    {
                        $calcolor='green';
                        $style='bold';
                    }
                    else
                    {
                        $starcolor='green';
                        $style='bold';
                    }
                }
                if (!$col)
                    $bgcolor='#E0E0E0';
                else
                    $bgcolor='#D0D0D0';
                $col = !$col;
                printf("<tr bgcolor='%s'>\n<td align='center' style='font-weight:%s'>\n%s \n</td>\n", $bgcolor, $style, $row[0]);
                printf("<td style='padding-left:1ex;padding-right:1ex'>\n <font color='%s'>%s </font>\n</td>\n", $calcolor, $row[1]);
                printf("<td style='padding-left:1ex;padding-right:1ex'>\n <font color='%s'>%s </font>\n</td>\n</tr>\n", $starcolor, $row[2]);
                $calcolor='black';
                $starcolor='black';
                $style='normal';
            }
            printf("</table>\n");

            if ($_GET["step"]=="crashed")
                printf("<br>\n You chose to reset only the sequences which are crashed or failed.<br>\n");
            else
                printf("<br>\n Only the sequences which have not yet been processed or resetted, will be resetted.<br>\n");
            printf("To <b>reset</b> them, please check the insert checkbox and submit again.<br><br>\n");

            if (empty($star) && empty($callisto) && empty($crashed))
                echo "With your choice no sequences would be resetted.";
            else
                echo "With your choice the following sequences would be resetted: ";
            if (!empty($star))
                foreach($star as $num => $seq)
                    echo $seq . " ";
            if (!empty($callisto))
                foreach($callisto as $num => $seq)
                    echo $seq . " ";
            if (!empty($crashed))
                foreach($crashed as $num => $seq)
                    echo $seq . " ";
            mysql_free_result($result0);
            printf("<br>\n<br>\n<input type='button' value='Reset Sequences' onClick='self.location.href=\"%s&amp;insert=On\"'>&nbsp;&nbsp;&nbsp;\n", htmlspecialchars($_SERVER["REQUEST_URI"]));
        }
        mysql_close($db_id);

//        PrintSubmittedQuery($query0, "1", $db, "old");

    }

    include ("include.php");
    include ("menu.php");
    include ("db.php");
    include ("magicdefs.php");

    ini_set("display_errors", "On");
    ini_set("mysql.trace_mode", "On");

    echo (file_get_contents("index-header.html"));

    $ips=array("91.64.78.187", "132.187.47.10", "132.187.47.67");
    if (!in_array($_SERVER['REMOTE_ADDR'], $ips))
    {
        echo "You are not allowed to reset sequences.<br>";
        echo "If you want to reset your sequences in the automatic analysis, please contact Daniela Dorner.<br>";
        echo (file_get_contents("index-footer.html"));
        return;
    }

    $environment = sizeof($_GET);

    //init
    if (empty($_GET["Seqs"]))
        $_GET["Seqs"]="";
    if (empty($_GET["step"]))
        $_GET["step"]="";

    //print form
    printf("<center>\n");
    printf("<form action=\"resetseq.php\" METHOD=\"GET\">\n");

    printf("<input type='radio' name='step' value='crashed' %s> crashed processes &nbsp;", $_GET["step"]=="crashed" ? "checked" : "");
    printf("<input type='radio' name='step' value='callisto' %s> callisto &nbsp;", $_GET["step"]=="callisto" ? "checked" : "");
    printf("<input type='radio' name='step' value='star' %s> star<br>", $_GET["step"]=="star" ? "checked" : "");

    printf("<table>\n<tr>\n<td align='left'>\nSequences: \n</td>\n</td>\n</tr>\n<tr>\n<td valign='left'>\n");
    printf("<input name='Seqs' type='text' size='75' maxlength='300' value='%s'>\n</td>\n</tr>\n</table>\n<br>\n", $_GET["Seqs"]);

    ini_set("mysql.trace_mode", "Off");
//    ini_set("display_errors", "Off");

    printf("<input type='submit' value='Reset Sequences in DB'>&nbsp;&nbsp;&nbsp;\n");
    printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"resetseq.php\"'>&nbsp;&nbsp;&nbsp;\n");
//    if (!empty($_GET["step"]) && ereg("^([ ]*[0-9]{5,8}[ ]*[,])*[ ]*[0-9]{5,8}[ ]*$",$_GET["Seqs"]))
//        printf("<input type='checkbox' name='insert' value='On'>insert\n");
    printf("<br>\n");
    printf("</form>\n");
    printf("</center>\n");
    printf("</td>\n</tr>\n");
    printf("<tr class='Block'>\n<td>\n");

    if ($environment==0)
    {
        printf("No query submitted yet.<br>\n");
        printf("To reset sequences, please insert them in the following format: <i>12345,87654321</i><br>\n");
        printf("and choose whether you want to reset just crashed processes, callisto (this includes star) or just star.<br>\n");
        printf("You can enter up to 300 characters in the sequences field.<br>\n");
    }
    else
        PrintPage($host, $user, $pw, $db);

    echo (file_get_contents("index-footer.html"));

    ini_set("display_errors", "Off");
    ini_set("mysql.trace_mode", "Off");
}
?>
