#!/usr/bin/perl

###################################################
#
#  Copyright 2005-2006 Tian
#
#  This file is part of GCstar.
#
#  GCstar 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.
#
#  GCstar 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 GCstar; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###################################################

use strict;
use utf8;

my $initTime;
if ($ENV{GCS_PROFILING} eq '1')
{
    eval 'use Time::HiRes';
    eval '$initTime = [Time::HiRes::gettimeofday()]';
}

my $VERSION = '0.3';

my $MULTI_PROCESS = 1;

use Getopt::Long;
use File::Path;
use File::Spec;
use FindBin qw($RealBin);
use POSIX qw(locale_h);

$ENV{GCS_BIN_DIR} = $RealBin;
($ENV{GCS_LIB_DIR} = $ENV{GCS_BIN_DIR}) =~ s/bin\/?$/lib\/gcstar/;
($ENV{GCS_SHARE_DIR} = $ENV{GCS_BIN_DIR}) =~ s/bin\/?$/share\/gcstar/;
use lib File::Spec->canonpath("$RealBin/../lib/gcstar");

#XDG stuff
my $home = $ENV{'HOME'};
$home =~ s/\\/\//g if ($^O =~ /win32/i);

$ENV{XDG_CONFIG_HOME} = $RealBin.'/../config' if ($^O =~ /win32/i);
$ENV{XDG_CONFIG_HOME} = $home.'/.config' if ! exists $ENV{XDG_CONFIG_HOME};
mkpath $ENV{XDG_CONFIG_HOME};

$ENV{XDG_DATA_HOME} = $RealBin.'/../data' if ($^O =~ /win32/i);
$ENV{XDG_DATA_HOME} = $home.'/.local/share' if ! exists $ENV{XDG_DATA_HOME};
mkpath $ENV{XDG_DATA_HOME};

$ENV{GCS_CONFIG_HOME} = $ENV{XDG_CONFIG_HOME}.'/gcstar';
$ENV{GCS_CONFIG_HOME} = $ENV{XDG_CONFIG_HOME} if ($^O =~ /win32/i);
mkdir $ENV{GCS_CONFIG_HOME};
$ENV{GCS_CONFIG_FILE} = $ENV{GCS_CONFIG_HOME}.'/GCstar.conf';
$ENV{GCS_DATA_HOME} = $ENV{XDG_DATA_HOME}.'/gcstar';
$ENV{GCS_DATA_HOME} = $ENV{XDG_DATA_HOME} if ($^O =~ /win32/i);
mkdir $ENV{GCS_DATA_HOME};


use GCOptions;
my $options = new GCOptionLoader($ENV{GCS_CONFIG_FILE}, 1);
               
my $lang = $options->getFullLang;
$ENV{LANG} = $lang;
$ENV{LANGUAGE} = $lang;
$ENV{LC_ALL} = $lang;
$ENV{LC_CTYPE} = $lang;
setlocale(LC_ALL, $lang);

sub usage
{
    print "Usage: $0 [[-u|--update [-a|--all] [-c|--collection] [-w|--website] [-i|--import] [-e|--export] [-l|--lang]] | [FILE]]\n";
}

Getopt::Long::Configure ('bundling');
my ($help, $update, $toBeUpdated, $noProxy) = (0, 0, {}, 0);
(usage, exit 1) if !GetOptions("h|help" => \$help,
                               "u|update" => \$update,
                               "a|all" => \$toBeUpdated->{all},
                               "c|collection" => \$toBeUpdated->{models},
                               "w|website" => \$toBeUpdated->{plugins},
                               "i|import" => \$toBeUpdated->{import},
                               "e|export" => \$toBeUpdated->{export},
                               "l|lang" => \$toBeUpdated->{lang},
                               "n|noproxy" => \$noProxy);

my $atLeastOne = 0;
foreach (keys %$toBeUpdated)
{
    $atLeastOne = 1 if $toBeUpdated->{$_};
}
(usage, exit 1) if $help || ($atLeastOne && !$update);

if ($update)
{
    eval '
        use GCUpdater;
        use GCLang;
        ';
    my $langContainer = $GCLang::langs{$options->lang};
    $toBeUpdated->{all} = 1 if !$atLeastOne;
    my $updater = new GCTextUpdater($langContainer,
                                    $ENV{GCS_LIB_DIR},
                                    $toBeUpdated,
                                    $noProxy,
                                    $VERSION);
    $updater->update;
    exit 0;
}
if ($ARGV[0])
{
    # We have to make it absolute if needed
    my $file = $ARGV[0];
    $file = File::Spec->rel2abs($file)
        if (!File::Spec->file_name_is_absolute($file));
    $options->file($file);
}

my $pid;
if ($MULTI_PROCESS)
{
    pipe(RCOMMAND, WCOMMAND);
    pipe(RDATA, WDATA);
    if ($^O !~ /win32/i)
    {
        if (!($pid = fork))
        {
            use GCPlugins;
            close WCOMMAND;
            close RDATA;
            my $searchJob = new GCPluginJob(\*RCOMMAND, \*WDATA);
        
            $searchJob->run;
        }
    }
}

use Gtk2;
use GCMainWindow;

my %searchJob = {};

if ($MULTI_PROCESS)
{
    close RCOMMAND;
    close WDATA;
    %searchJob = (
        pid => $pid,
        command => \*WCOMMAND,
        data => \*RDATA
    );
    
    if ($^O =~ /win32/i)
    {
        close WCOMMAND;
        close RDATA;
    }
}
Gtk2->init;
my $window = new GCFrame($options, $VERSION, \%searchJob);

#Gtk2->set_locale;
Gtk2->main;

if ($ENV{GCS_PROFILING} eq '1')
{
    my $elapsed;
    eval '$elapsed = Time::HiRes::tv_interval($initTime)';
    print "Elapsed : $elapsed\n";
}

0;
