#! /usr/bin/perl
#
# This file is part of qVamps.
#
# qVamps 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.
#
# qVamps 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 qVamps; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


use strict;
use warnings;
use Config;

BEGIN
{
  my $qvamps_home = $ENV {"QVAMPS"};
  my $archname    = $Config {"archname"};
  my $load_prefix = $1 if ($0 =~ m|(.*)/bin/qvamps$|);
  my $par_prgname = $ENV {"PAR_PROGNAME"};

  if (defined ($par_prgname) && defined ($PAR::LibCache {$par_prgname}))
  {
    my $dummy    = $PAR::LibCache;
    my $par_temp = $ENV {"PAR_TEMP"};

    print STDERR "qVamps: loading modules from binary distribution archive\n";

    # don't understand, why this is necessary
    push @INC, "$par_temp/inc/lib/Qt";
  }
  elsif (-e "./QVamps.pm")
  {
    print STDERR "qVamps: loading modules from current working directory\n";
    unshift @INC, ".";
  }
  elsif ($qvamps_home && -e $qvamps_home)
  {
    print STDERR "qVamps: loading modules from $qvamps_home/share/lib\n";
    unshift @INC, "$qvamps_home/share/lib";
    unshift @INC, "$qvamps_home/lib/$archname";
  }
  elsif ($load_prefix && -e "$load_prefix/share/qvamps")
  {
    print STDERR "qVamps: loading modules from " .
                 "$load_prefix/share/qvamps/lib\n";
    unshift @INC, "$load_prefix/share/qvamps/lib";
    unshift @INC, "$load_prefix/lib/qvamps/$archname";
  }
}

use Qt;
#use Qt::debug qw (gc);
use QVamps qw (read_setting replace);
use MainWindow;
use DesignerMimeSourceFactory_qvamps;


my  $app        = Qt::Application (\@ARGV);
my  $translator = Qt::Translator;
my  $prefix     = read_setting ("/Installation/prefix");
our $lang       = read_setting ("/General/language");

unless ($lang)
{
  $lang = $ENV {"LANG"};
  $lang =~ s/^(..).+/$1/ if ($lang);
}

if ($lang)
{
  my $loaded = $translator -> load ("./$lang", undef, '_@.');

  unless ($loaded)
  {
    my $msg_file = read_setting ("/Installation/messages_file");
    $msg_file    = replace ($msg_file, { "p" => $prefix, "l" => $lang });
    $loaded      = $translator -> load ($msg_file, undef, '_@.');
  }

  $app -> installTranslator ($translator) if ($loaded);
}

my $win  = MainWindow;
my $icon = Qt::Pixmap::fromMimeSource ("icon.png");

$win  -> setIcon ($icon);

$app -> setMainWidget ($win);
$win -> show ();
exit ($app -> exec ());
