#!/usr/bin/perl -w
#  $Id$  -*-cperl-*-
#=============================================================================
#
# AUTHOR
#   Jonas Liljegren   <jonas@paranormal.se>
#
# COPYRIGHT
#   Copyright (C) 2007-2008 Avisita AB.  All Rights Reserved.
#
#=============================================================================

=head1 DESCRIPTION

RDFguides daemon example server

=cut

our $CFG;
our $pf;

BEGIN
{
    our $VERSION  = sprintf("%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/);
    print "Starting ritguides $VERSION\n";

    use FindBin;
    my $configfile = $FindBin::Bin . "/site.conf";

    open PF_LOC, "<$FindBin::Bin/../.paraframe_location" or die $!;
    $pf = <PF_LOC>;
    chomp( $pf );
    push @INC, $pf.'/lib';


    $CFG = do $configfile
      or die "Failed to load config $configfile: $! ($@)\n";
    push @INC, $CFG->{'rb_root'}.'/lib';

    push @INC, $CFG->{'demo_root'}. "/lib";
}

use strict;
use locale;
use Scalar::Util qw( looks_like_number );

use Para::Frame;
use Para::Frame::DBIx;
use Para::Frame::Time qw();
use Para::Frame::Email;
use Para::Frame::Email::Address;
use Para::Frame::Utils qw( chmod_tree datadump debug );
use Para::Frame::Watchdog;
use Para::Frame::Site;

use RDF::Base;
use RDF::Base::Constants;
use RDF::Base::Utils;
use RDF::Base::Resource;
use RDF::Base::Arc;
use RDF::Base::Search;
use RDF::Base::Pred;
use RDF::Base::Rule;
use RDF::Base::Literal::Time;
use RDF::Base::Site;
use RDF::Base::L10N;

use RDF::Base::User::Meta;
use RDF::Base::Session;

use RDF::Base::Demo;

{
    # Do not daemonize if run with cmdline argument
    my $daemonize = @ARGV ? 0 : 1;

    unless( $CFG )
    {
	die "Configuration missing";
    }

#    debug "Loaded config:\n".datadump($CFG);

    RDF::Base::Demo->store_cfg( $CFG );

    Para::Frame::Site->add({
	'code'        => 'demo',
	'name'        => 'RDFBase Demo',
	'approot'     => $CFG->{'demo_root'},
	'webhome'     => $CFG->{site}{demo}{'webhome'}||'',
	'webhost'     => $CFG->{'webhost'},
	'is_compiled' => 0,
#	'loadpage'    => "/clean/loadpage.tt",
	'languages'       => ['sv'],
	'email'       => $CFG->{email},
	'css'         =>
	{
	 params =>
	 {
	  background => 'green',
	 },
	},
    });


    my $appback = [$CFG->{'rb_root'}];
    my $demo_root = $CFG->{'demo_root'};

    $CFG->{'debug'} ||= 0;
    if( looks_like_number($ARGV[0]) )
    {
	$CFG->{'debug'} = $ARGV[0];
    }

    my $cfg =
    {
     'paraframe'       => $pf,
     'paraframe_group' => 'staff',
     'rb_root'         => $CFG->{'rb_root'},

     'appback'         => $appback,
     'appbase'         => 'RDF::Base::Demo',
     'appfmly'         => 'RDF::Base',

     'user_class'      => 'RDF::Base::User::Meta',
     'session_class'   => 'RDF::Base::Session',
#     'tt_plugins'      => 'RDF::Guides::Template::Plugin',
#     'resource_class'  => 'RDF::Guides::Resource',
#     'search_collection_class' => 'RDF::Guides::Search',
#     'search_result_class'     => 'RDF::Guides::Search::Result',

     'l10n_class'      => 'RDF::Base::L10N',
     'site_class'      => 'RDF::Base::Site',

#     'bg_user_code'    => sub{ RDF::Base::Resource->get_by_id(1115) },
     'logfile'         => "$demo_root/logs/ritguides.log",
     'pidfile'         => "$demo_root/var/ritguides.pid",
     'dir_var'         => "$demo_root/var",

     'port'            => $CFG->{'port'},
     'debug'           => $CFG->{'debug'} || 0,
     'do_bgjob'        => $CFG->{'do_bgjob'},
     'umask'           => 0007,
     'languages'       => ['sv'],
     'time_stringify'  => 1,
     'site_autodetect' => 1,
     'site_auto'       => 0,

#     'ie7'             => $CFG->{'ie7'},

   };

    Para::Frame->configure( $cfg );
    RDF::Base::Demo->on_configure();

    # Check those...
    $RDF::Base::LOOKUP_CLASS_FOR{$cfg->{'user_class'}} = 1;


    # Configure database
    #
    $RDF::dbix = Para::Frame::DBIx ->
	new({
	    connect => $CFG->{'dbconnect'},
	    import_tt_params => 1,
	});


    # Attatch code to hooks
    #

    Para::Frame->add_hook('on_startup', sub
			  {
			      $RDF::dbix->connect;
			      RDF::Base::Demo->initialize_db;
			  });

    Para::Frame->add_hook('before_user_logout', sub
			  {
			      $_[0]->before_user_logout;
			  });

#    Para::Frame->add_hook('user_login', sub
#			  {
#			      $_[0]->after_user_login;
#			  });

    Para::Frame->add_hook('done', \&RDF::Base::Demo::on_done);


    RDF::Base->init();


    my $global_params =
    {
     now             => \&RDF::Base::Literal::Time::now,
     date            => \&RDF::Base::Literal::Time::date,
     favicon         => "pf/images/favicon.ico",
    };
    Para::Frame->add_global_tt_params( $global_params );


    # Assign extra handlers for Burner
    #
    my $burner_plain = Para::Frame::Burner->get_by_type('plain');
    $burner_plain->add_ext('htaccess');


    if( $ARGV[0] and not looks_like_number($ARGV[0]) )
    {
	Para::Frame->startup;
	print "Server stops";
	exit;
    }

    if( $daemonize )
    {
	Para::Frame->daemonize( 1 );
    }
    else
    {
	Para::Frame->watchdog_startup();
    }
}

#########################################################
