/**********************************************************************
 *
 *   FreeDoko a Doppelkopf-Game
 *    
 *   Copyright (C) 2001-2004  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"

namespace Help {

  /**
   ** -> result
   **
   ** @param	-
   **
   ** @return	the directory with the manual
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @bug	using the function 'realpath'
   **
   **/
  string
    Help::manual_directory()
    {
	vector<string> directory;

	directory.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::translator.token());
	directory.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::translator.token());
	directory.push_back("./" + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::translator.token());
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::translator.token());
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting_default(Setting::HELP_DIRECTORY) + "/"
			    + ::translator.token());

	directory.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back("./" + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting_default(Setting::HELP_DIRECTORY) + "/"
			    + ::setting(Setting::LANGUAGE));

	directory.push_back(::setting(Setting::PRIVATE_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting_default(Setting::LANGUAGE));
	directory.push_back(::setting(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting_default(Setting::LANGUAGE));
	directory.push_back("./" + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting_default(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting(Setting::HELP_DIRECTORY) + "/"
			    + ::setting_default(Setting::LANGUAGE));
	directory.push_back(::setting_default(Setting::PUBLIC_DATA_DIRECTORY) + "/"
			    + ::setting_default(Setting::HELP_DIRECTORY) + "/"
			    + ::setting_default(Setting::LANGUAGE));

	vector<string>::iterator d;
	for (d = directory.begin(); d != directory.end(); d++) {
	  ifstream istr((*d + "/index.html").c_str());
	  if (istr.good()) {
#ifdef WINDOWS
	    char cwd[MAX_PATH];
	    if (getcwd(cwd, MAX_PATH) != NULL) {
	      return ("file://" + string(cwd) + "/" + *d + "/");
	      break;
	    }
#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) + "/");
	      break;
	    }
#endif
	  }
	} // for (d \in directory)

	return ("http://free-doko.sourceforge.net/"
			+ ::setting(Setting::HELP_DIRECTORY) + "/"
			+ ::setting(Setting::LANGUAGE) + "/");
    } // string Help::manual_directory()

  /**
   ** -> result
   **
   ** @param	-
   **
   ** @return	the directory with the operation help
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   **/
  string
    Help::operation_directory()
    {
      return (manual_directory() + "operation/");
    } // string Help::operation_directory()

  /**
   ** 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)

  /**
   ** show the manual help page of the given location
   **
   ** @param	page	manual page to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   **/
  void
    Help::show_manual(string const page)
    {
      show(manual_directory() + page + ".html");

      return ;
    } // void Help::show_manual(string const page)

  /**
   ** show the operation help page of the given location
   **
   ** @param	page	manual page to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   **/
  void
    Help::show_operation(string const page)
    {
      show(operation_directory() + page + ".html");

      return ;
    } // void Help::show_operation(string const page)

  /**
   ** shows the help for 'game_status'
   **
   ** @param	game_status	game status to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(GameStatus const game_status)
    {
      return ;
    } // void Help::show(GameStatus const game_status)

  /**
   ** shows the help for the rule 'type'
   **
   ** @param	type	rule type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Rule::TypeBool const type)
    {
      return ;
    } // void Help::show(Rule::TypeBool const type)

  /**
   ** shows the help for the rule 'type'
   **
   ** @param	type	rule type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Rule::TypeUnsigned const type)
    {
      return ;
    } // void Help::show(Rule::TypeUnsigned const type)

  /**
   ** shows the help for the setting 'type'
   **
   ** @param	type	setting type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Setting::TypeBool const type)
    {
      return ;
    } // void Help::show(Setting::TypeBool const type)

  /**
   ** shows the help for the setting 'type'
   **
   ** @param	type	setting type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Setting::TypeUnsigned const type)
    {
      return ;
    } // void Help::show(Setting::TypeUnsigned const type)

  /**
   ** shows the help for the setting 'type'
   **
   ** @param	type	setting type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Setting::TypeString const type)
    {
      return ;
    } // void Help::show(Setting::TypeString const type)

  /**
   ** shows the help for the setting 'type'
   **
   ** @param	type	setting type to show
   **
   ** @return	-
   **
   ** @author	Diether Knof
   **
   ** @version	0.6.3
   **
   ** @todo	all
   **
   **/
  void
    Help::show(Setting::TypeCardsOrder const type)
    {
      return ;
    } // void Help::show(Setting::TypeCardsOrder const type)

} // namespace Help
