/*
** Copyright (c) 2002 D. Richard Hipp
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
** General Public License for more details.
** 
** You should have received a copy of the GNU General Public
** License along with this library; if not, write to the
** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
** Boston, MA  02111-1307, USA.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
**
** Code to generate the index page
*/
#include "config.h"
#include "index.h"

/*
** Return TRUE if the given string contains at least one non-space
** character
*/
static int not_blank(const char *z){
  while( isspace(*z) ){ z++; }
  return *z!=0;
}

/*
** WEBPAGE: /
** WEBPAGE: /index
** WEBPAGE: /mainmenu
*/
void index_page(void){
  int cnt = 0;
  login_check_credentials();
  common_standard_menu("index", 0);

  common_add_help_item("CvstracDocumentation");

  /* If the user has wiki read permission and a wiki page named HomePage
  ** exists and is not empty and is locked (meaning that only an 
  ** administrator could have created it), then use that page as the
  ** main menu rather than the built-in main menu.
  **
  ** The built-in main menu is always reachable using the /mainmenu URL
  ** instead of "/index" or "/".
  */
  if( g.okRdWiki && g.zPath[0]!='m' ){
    char *zBody = db_short_query(
        "SELECT text FROM wiki WHERE name='HomePage' AND locked");
    if( zBody && not_blank(zBody) ){
      common_add_nav_item("mainmenu", "Main Menu");
      common_header("Home Page");
      /* menu_sidebar(); */
      output_wiki(zBody, "", "HomePage");
      common_footer();
      return;
    }
  }

  /* Render the built-in main-menu page.
  */
  common_header("Main Index");
  cgi_printf("<table cellpadding=\"10\">\n"
         "\n");
  if( g.zPath[0]=='m' ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"index\"><b>Home Page</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "View the Wiki-based homepage for this project.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okNewTkt ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"tktnew\"><b>Ticket</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Create a new Ticket with a defect report or enhancement request.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okCheckout ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"%h\"><b>Browse</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Browse the %s repository tree.\n"
           "</td>\n"
           "</tr>\n"
           "\n",default_browse_url(),g.scm.zName);
    cnt++;
  }   
  if( g.okRead ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"reportlist\"><b>Reports</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "View summary reports of Tickets.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okRdWiki || g.okRead || g.okCheckout ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"timeline\"><b>Timeline</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "View a chronology of Check-Ins and Ticket changes.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okRdWiki ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"wiki\"><b>Wiki</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "View the Wiki documentation pages.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okRead || g.okCheckout || g.okRdWiki ){
    const char *az[5];
    int n=0;
    if( g.okRead ) az[n++] = "Tickets";
    if( g.okCheckout ) az[n++] = "Check-ins";
    if( g.okRdWiki ) az[n++] = "Wiki pages";
    if( g.okCheckout ) az[n++] = "Filenames";
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"search\"><b>Search</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n");
    if( n==4 ){
      cgi_printf("Search for keywords in %s, %s, %s, and/or %s\n",az[0],az[1],az[2],az[3]);
    }else if( n==3 ){
      cgi_printf("Search for keywords in %s, %s, and/or %s\n",az[0],az[1],az[2]);
    }else if( n==2 ){
      cgi_printf("Search for keywords in %s and/or %s\n",az[0],az[1]);
    }else{
      cgi_printf("Search for keywords in %s\n",az[0]);
    }
    cgi_printf("</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okCheckin ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"msnew\"><b>Milestones</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Create new project milestones.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okWrite && !g.isAnon ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"userlist\"><b>User</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Create, edit, and delete users.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okAdmin ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"setup\"><b>Setup</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Setup global system parameters.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.okRdWiki ){
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"wiki?p=CvstracDocumentation\"><b>Documentation</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Read the online manual.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
    cnt++;
  }
  if( g.isAnon ){
    if( cnt==0 ){
      login_needed();
      return;
    }
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"login\"><b>Login</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Log in.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
  }else{
    cgi_printf("<tr>\n"
           "<td valign=\"top\">\n"
           "<a href=\"logout\"><b>Logout</b></a>\n"
           "</td>\n"
           "<td valign=\"top\">\n"
           "Log off or change password.\n"
           "</td>\n"
           "</tr>\n"
           "\n");
  }
  cgi_printf("</table>\n"
         "\n");
  common_footer();
}


syntax highlighted by Code2HTML, v. 0.9.1