#!/usr/bin/perl

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

use strict;
use utf8;

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

use lib File::Spec->canonpath("$RealBin/../lib/gcfilms");

#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{GCF_CONFIG_HOME} = $ENV{XDG_CONFIG_HOME}.'/gcfilms';
$ENV{GCF_CONFIG_HOME} = $ENV{XDG_CONFIG_HOME} if ($^O =~ /win32/i);
mkdir $ENV{GCF_CONFIG_HOME};
$ENV{GCF_CONFIG_FILE} = $ENV{GCF_CONFIG_HOME}.'/gcfilms.conf';
$ENV{GCF_DATA_HOME} = $ENV{XDG_DATA_HOME}.'/gcfilms';
$ENV{GCF_DATA_HOME} = $ENV{XDG_DATA_HOME} if ($^O =~ /win32/i);
mkdir $ENV{GCF_DATA_HOME};

$ENV{GCF_BIN_DIR} = $RealBin;
($ENV{GCF_LIB_DIR} = $ENV{GCF_BIN_DIR}) =~ s/bin\/?$/lib\/gcfilms/;

($ENV{GCF_SHARE_DIR} = $ENV{GCF_BIN_DIR}) =~ s/bin\/?$/share\/gcfilms/;

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

#if ($ARGV[0] eq '--install-pictures')
#{
#    eval '
#        use GCInstaller;
#        use GCLang;
#        ';
#        my $langContainer = $GCLang::langs{$options->lang};
#        my $installer = new GCTextInstaller($langContainer, $ENV{GCF_SHARE_DIR});
#        $installer->install;
#    exit 0;
#}

$options->file($ARGV[0]) if $ARGV[0];

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

use Gtk2 "-init";
use GCMainWindow;

close RCOMMAND;
close WDATA;
my %searchJob = (
    pid => $pid,
    command => \*WCOMMAND,
    data => \*RDATA
);

if ($^O =~ /win32/i)
{
    close WCOMMAND;
    close RDATA;
}

my $window = new GCFrame($options, \%searchJob);

#Gtk2->set_locale;

Gtk2->main;

0;
