/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            pracrod.cc
 *
 *  Wed Aug 22 12:15:59 CEST 2007
 *  Copyright 2007 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Pracro.
 *
 *  Pracro 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.
 *
 *  Pracro 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 Pracro; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
// For ETC
#include <config.h>

#include "daemon.h"

// For setuid and setgid
#include <sys/types.h>
#include <unistd.h>

// For waitpid
#include <sys/wait.h>

// For signal
#include <signal.h>

// For errno and strerror
#include <errno.h>

#include <stdio.h>

#include <stdlib.h>
#include <string.h>

// For getpwent and getgrent
#include <sys/types.h>
#include <grp.h>
#include <pwd.h>

// For getopt_long and friends
#include <getopt.h>

#include "configurationparser.h"
#include "configuration.h"

#include "server.h"

#include "tcpsocket.h"

#include "debug.h"

static const char version_str[] =
"Pracro server v" VERSION "\n"
;

static const char copyright_str[] =
"Copyright (C) 2006-2009 Bent Bisballe Nyeng - Aasimon.org.\n"
"This is free software.  You may redistribute copies of it under the terms of\n"
"the GNU General Public License <http://www.gnu.org/licenses/gpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Written by Bent Bisballe Nyeng (deva@aasimon.org)\n"
;

static const char usage_str[] =
"Usage: %s [options]\n"
"Options:\n"
"  -c, --config file   Read configfile from 'file'\n"
"  -f, --foreground    Run in foreground mode (non-background mode)\n"
"  -u, --user user     Run as 'user' (overrides the configfile)\n"
"  -g, --group group   Run as 'group' (overrides the configfile)\n"
"  -x, --xml-basedir d Use 'd' as basedir for finding template- and macro-files (default "XML").\n"
"  -v, --version       Print version information and exit.\n"
"  -h, --help          Print this message and exit.\n"
"  -D, --debug ddd     Enable debug messages on 'ddd'; see documentation for details\n"
"  -d  --database db   Use db as the database backend. Can be one of pgsql or testdb (default pgsql).\n"
;

ConfigurationParser *configparser = NULL;

bool pracro_is_running = true;

void ctrl_c(int)
{
  //  printf("Ctrl+c\n");
  pracro_is_running = false;
}

void childwait(int)
{
  //  printf("childwait\n");

  pid_t pid;
  while((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
    //    printf("\tOne down!\n");
  }
}

void reload(int)
{
  int port;
  printf("Reload!\n");
  port = configparser->lookup("port");
  configparser->reload();

  { // Force wake the server process for reinitialization.
    TCPSocket socket;
    socket.connect("localhost", port);
  }
}

class PracroDaemon : public Daemon {
private:
  int daemon_main();
};

int PracroDaemon::daemon_main()
{
  // Activate the server main loop.
  server();

  return 0;
}

int main(int argc, char *argv[])
{
  int c;
  char *configfile = NULL;
  char *user = NULL;
  char *group = NULL;
  bool foreground = false;
  char *xml_basedir = NULL;
  char *debugstr = NULL;
  std::string database;

  pracro_debug_init();

  int option_index = 0;
  while(1) {
    //    int this_option_optind = optind ? optind : 1;
    static struct option long_options[] = {
      {"foreground", no_argument, 0, 'f'},
      {"config", required_argument, 0, 'c'},
      {"user", required_argument, 0, 'u'},
      {"group", required_argument, 0, 'g'},
      {"help", no_argument, 0, 'h'},
      {"version", no_argument, 0, 'v'},
      {"xml-basedir", required_argument, 0, 'x'},
      {"debug", required_argument, 0, 'D'},
      {"database", required_argument, 0, 'd'},
      {0, 0, 0, 0}
    };
    
    c = getopt_long (argc, argv, "D:hvfc:u:g:x:d:", long_options, &option_index);
    
    if (c == -1)
      break;

    switch(c) {
    case 'd':
      database = optarg;
      break;

    case 'c':
      configfile = strdup(optarg);
      break;

    case 'f':
      foreground = true;
      break;

    case 'u':
      user = strdup(optarg);
      break;

    case 'g':
      group = strdup(optarg);
      break;

    case 'x':
      xml_basedir = strdup(optarg);
      break;

    case 'D':
      debugstr = strdup(optarg);
      break;

    case '?':
    case 'h':
      printf(version_str);
      printf(usage_str, argv[0]);
      return 0;

    case 'v':
      printf(version_str);
      printf(copyright_str);
      return 0;

    default:
      break;
    }
  }

  if(debugstr) {
    pracro_debug_parse(debugstr);
  }

  // Load config
  if(configfile) configparser = new ConfigurationParser(configfile);
  else configparser = new ConfigurationParser(ETC"/pracrod.conf");

  if(database != "") {
    Conf::database_backend = database;
  }

  if(!user) {
    user = strdup(Conf::server_user.c_str());
  }


  if(!group) {
    group = strdup(Conf::server_group.c_str());
  }

  if(xml_basedir) {
    Conf::xml_basedir = xml_basedir;
  }

  signal(SIGHUP, reload);
  signal(SIGCLD, childwait);
  if(foreground) signal (SIGINT, ctrl_c);

  PracroDaemon daemon;
  daemon.run(user, group, !foreground);

  // Clean up
  if(configfile) free(configfile);
  if(user) free(user);
  if(group) free(group);

  return 0;
}