/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2006  by Diether Knof and Borg Enders
 *
 *   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 can find this license in the file 'gpl.txt'.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 *   MA  02111-1307  USA
 *
 *  Contact:
 *    Diether Knof dknof@gmx.de
 *    Borg Enders  borg@borgsoft.de
 *
 *********************************************************************/

#include "help.h"

#include "ui.h"
#include "../misc/translator.h"
#include "../utils/windows.h"
#include "../utils/string.h"

/**
 ** show the help page of the given location
 **
 ** @param     location	location of the page to show
 **
 ** @return    -
 **
 ** @author    Diether Knof
 **
 ** @version   0.6.3
 **
 **/
void
Help::show(string const& location)
{
#ifndef WINDOWS
  ::system((::setting(Setting::BROWSER) + " " + location + " &").c_str());
#else
  ::system((::setting(Setting::BROWSER) + " " + location).c_str());
#endif

  return ;
} // void Help::show(string const location)

/**
 ** -> result
 **
 ** @param     page   help page
 **                   default: index
 **
 ** @return    the location with the manual help in page 'page'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **
 ** @bug       using the function 'realpath'
 **/
string
Help::manual_location(string const& page)
{
  // Search through a list of local directories, if that does not help,
  // return the internet address of the manual.

  list<string> directories;

  if (getenv("FREEDOKO_MANUAL_DIRECTORY"))
    directories.push_back(getenv("FREEDOKO_MANUAL_DIRECTORY"));
#ifdef WINDOWS
  {
    string dir = DK::Utils::Windows::Reg_read(HKEY_LOCAL_MACHINE,
					      "Software\\FreeDoko",
					      "Help Directory");
    if (!dir.empty())
      directories.push_back(dir + "/" + ::translator.token());
  }
#endif
  directories.push_back(::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());
  directories.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());
  directories.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());
  directories.push_back("./" + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting_default(Setting::MANUAL_DIRECTORY) + "/"
		      + ::translator.token());

  directories.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting(Setting::LANGUAGE));
  directories.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting(Setting::LANGUAGE));
  directories.push_back("./" + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting(Setting::LANGUAGE));
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting(Setting::LANGUAGE));
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting_default(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting(Setting::LANGUAGE));

  directories.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting_default(Setting::LANGUAGE));
  directories.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting_default(Setting::LANGUAGE));
  directories.push_back("./" + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting_default(Setting::LANGUAGE));
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting_default(Setting::LANGUAGE));
  directories.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
		      + ::setting_default(Setting::MANUAL_DIRECTORY) + "/"
		      + ::setting_default(Setting::LANGUAGE));

  list<string>::iterator d;
  for (d = directories.begin(); d != directories.end(); d++) {
    ifstream istr((*d + "/" + page).c_str());
    if (istr.good()) {
#ifdef WINDOWS
      if (   (d->size() < 2)
	  || ((*d)[1] != ':')) {
	// no absolute path
	char cwd[MAX_PATH];
	if (getcwd(cwd, MAX_PATH) != NULL) {
	  return ("file://" + string(cwd) + "/" + *d + "/" + page);
	  break;
	}
      } // if (no absolute path)

#else
      char path[PATH_MAX];
      //if (realpath(d->c_str(), path) != NULL)
      // Bug: the manual page from 'realpath' says not to use the function
      if (realpath(d->c_str(), path) != NULL) {
	return ("file://" + string(path) + "/" + page);
	break;
      }
#endif
    }
  } // for (d \in directories)

  return ("http://free-doko.sourceforge.net/doc/manual/"
	  + ::setting(Setting::LANGUAGE) + "/" + page);
} // string Help::manual_location(string page)

/**
 ** -> result
 **
 ** @param     page   help page
 **
 ** @return    the location with the operation help in page 'page'
 **
 ** @author    Diether Knof
 **
 ** @version   0.6.3
 **/
string
Help::operation_location(string const& page)
{
  return manual_location("operation/" + page);
} // string Help::operation_location(string page)

/**
 ** -> result
 **
 ** @param     type   rule type to show
 **
 ** @return    the location of the help for the rule 'type'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Rule::TypeBool const type)
{
  string name = ::name(type);
  return operation_location("rules_config.html"
			    + DK::Utils::String::replace_all(name,
							     " ", "_"));
} // string Help::location(Rule::TypeBool type)

/**
 ** -> result
 **
 ** @param     type   rule type to show
 **
 ** @return    the location of the help for the rule 'type'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Rule::TypeUnsigned const type)
{
  string name = ::name(type);
  return operation_location("rules_config.html"
			    + DK::Utils::String::replace_all(name, " ", "_"));
} // string Help::location(Rule::TypeUnsigned type)

/**
 ** -> result
 **
 ** @param     type   setting type to show
 **
 ** @return    the location of the help for the setting 'type'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Setting::TypeBool const type)
{
  string name = ::name(type);
  return operation_location("settings.html"
			    + DK::Utils::String::replace_all(name, " ", "_"));
} // string Help::location(Setting::TypeBool type)

/**
 ** -> result
 **
 ** @param     type   setting type to show
 **
 ** @return    the location of the help for the setting 'type'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Setting::TypeUnsigned const type)
{
  string name = ::name(type);
  return operation_location("settings.html"
			    + DK::Utils::String::replace_all(name, " ", "_"));
} // string Help::location(Setting::TypeUnsigned type)

/**
 ** -> result
 **
 ** @param     type   setting type to show
 **
 ** @return    the location of the help for the setting 'type'
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Setting::TypeString const type)
{
  string name = ::name(type);
  return operation_location("settings.html"
			    + DK::Utils::String::replace_all(name, " ", "_"));
} // string Help::location(Setting::TypeString type)

/**
 ** -> result
 **
 ** @param     type   setting type to show
 **
 ** @return    the location of the cards order help
 **
 ** @author    Diether Knof
 **
 ** @version   0.7.3
 **/
string
Help::location(Setting::TypeCardsOrder const type)
{
  string name = ::name(type);
  return operation_location("settings.html"
			    + DK::Utils::String::replace_all(name, " ", "_"));
} // string Help::location(Setting::TypeCardsOrder type)
