# -*-perl-*-
# Keitairc::Config
# $Id: Config.pm,v 1.2 2008/01/09 18:45:53 morimoto Exp $
# $Source: /cvsroot/keitairc/lib/Keitairc/Config.pm,v $
#
# Copyright (c) 2008 Jun Morimoto <morimoto@mrmt.net>
# This program is covered by the GNU General Public License 2

package Keitairc::Config;
use strict;
use AppConfig qw(:argcount);
use Cwd;
use vars qw(@ISA);
@ISA = qw(AppConfig);

################################################################
sub new{
	my ($class, $version, @argv) = @_;
	my $me = $class->SUPER::new(
		{
			CASE => 1,
			GLOBAL => {
				ARGCOUNT => ARGCOUNT_ONE,
			}
		},
		qw(irc_nick irc_username irc_desc
		   irc_server irc_port irc_password
		   au_subscriber_id au_pcsv
		   docomo_foma_icc
		   use_cookie cookie_ttl session_ttl
		   web_port web_title web_lines web_root
		   web_username web_password web_host
		   show_newmsgonly ping_delay reconnect_delay
		   template_dir version daemonize pid_dir plugin_dir)
		);

	# set default values
	$me->version($version);
	$me->irc_desc('keitairc');
	$me->irc_port(6667);
	$me->ping_delay(30);
	$me->reconnect_delay(10);
	$me->cookie_ttl(86400 * 3);  # 3 days
	$me->session_ttl(60 * 30);  # 30 min
	$me->pid_dir('/var/run');
	$me->plugin_dir(getcwd() . '/lib/plugins:/usr/share/keitairc/lib/plugins');
	$me->template_dir(getcwd() . '/lib/templates:/usr/share/keitairc/lib/templates');

	if(defined $argv[0]){
		unless(-e $argv[0]){
			::log("Can't read $argv[0]");
			exit 1;
		}
		$me->file($argv[0]);
		shift(@argv);
	}else{
		$me->file('/etc/keitairc');
		$me->file($ENV{HOME} . '/.keitairc');
	}

	$me->args(\@argv);

	if(defined $me->web_username()){
		::log("web_username has obsoleted from keitairc 2.0");
	}

	if(defined $me->use_cookie()){
		::log("use_cookie has obsoleted from keitairc 2.0");
	}

	if(defined $me->web_root()){
		::log("web_root has obsoleted from keitairc 2.0");
	}

	if(defined $me->au_pcsv()){
		::log("au_pcsv has obsoleted from keitairc 2.0");
	}

	unless(length $me->irc_nick()){
		::log_die("irc_nick does not specified");
	}

	unless(length $me->irc_username()){
		::log_die("irc_username does not specified");
	}

	unless(length $me->irc_server()){
		::log_die("irc_server does not specified");
	}

	unless(length $me->web_port()){
		::log_die("web_port does not specified");
	}

	unless(length $me->web_host()){
		::log_die("web_host does not specified");
	}

	unless(length $me->web_password()){
		::log_die("web_password does not specified");
	}

	$me;
}

################################################################
sub file{
	my $me = shift;
	my $file = shift;
	if(-e $file){
		$me->SUPER::file($file);
		::log("Loaded configuration file: $file");
		return;
	}
	::log("$file does not exist");
}

1;
