#!/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;

my $VERSION = '6.4';

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

$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 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};

use GCOptions;
my $options = new GCOptionLoader($ENV{GCF_CONFIG_FILE});
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] [-w|--website] [-i|--import] [-e|--export] [-l|--lang]] | [FILE]]\n";
}

Getopt::Long::Configure ('bundling');
my ($update, $toBeUpdated, $noProxy, $help) = (0, {}, 0, 0);
(usage, exit 1) if !GetOptions("h|help" => \$help,
                               "u|update" => \$update,
                               "a|all" => \$toBeUpdated->{all},
                               "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{GCF_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);
}

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;
use GCMainWindow;

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

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

#Gtk2->set_locale;
#Gtk2->disable_setlocale;
Gtk2->init;
my $window = new GCFrame($options, $VERSION, \%searchJob);



Gtk2->main;

#if ($^O =~ /win32/i)
#{
#    my $newLang = $options->getFullLang;
#    my $currentFile = $ENV{GCF_BIN_DIR}.'/gcfilms.bat';
#    my $newFile = $currentFile.'.new';
#    open BAT, '< '.$currentFile or die 'Cannot save language preferences';
#    open NEW, '> '.$newFile or die 'Cannot save language preferences';
#    while (<BAT>)
#    {
#        s/LC_ALL=.*$/LC_ALL=$newLang/;
#        print NEW;
#    }
#    close BAT;
#    close NEW;
#    rename($newFile, $currentFile);
#}

0;
