#!/usr/bin/perl -wT

#----------------------------------------------------------------------
#
# copyright (C) Decaux Nicolas Aka 'GawiNDX'
# contributor: Nicolas Decaux <decauxnico@gmail.com>
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#----------------------------------------------------------------------

package esmith;

use strict;
use CGI ':all';
use CGI::Carp qw(fatalsToBrowser);
use FileHandle;

use esmith::cgi;
use esmith::config;
use esmith::util;
use esmith::db;

sub showInitial ($$);
sub performAndShowResult ($);

BEGIN
{
    # Clear PATH and related environment variables so that calls to
    # external programs do not cause results to be tainted. See
    # "perlsec" manual page for details.

    #path hack see /sbin/e-smith/yum line 12 & 17
    $ENV {'PATH'} = '/bin:/usr/bin';
#    $ENV {'SHELL'} = '/bin/bash';
#    delete $ENV {'ENV'};
}

esmith::util::setRealToEffective ();

my %conf;
tie %conf, 'esmith::config';

#------------------------------------------------------------
# examine state parameter and display the appropriate form
#------------------------------------------------------------

my $q = new CGI;

open TMPFILE, "<", "/tmp/tmp_moz_loc";
my @array_locale = <TMPFILE>;
close TMPFILE;

if (! grep (/^state$/, $q->param))
{
    showInitial ($q, '');
}

elsif ($q->param ('state') eq "perform")
{
    performAndShowResult ($q);
}

else
{
    esmith::cgi::genStateError ($q, \%conf);
}

exit (0);

#------------------------------------------------------------
# subroutine to display initial form
#------------------------------------------------------------

sub showInitial ($$)
{
    my ($q, $msg) = @_;

    my %yesnoLabels = ('yes' => 'Yes',
                       'no'  => 'No');

    if ($msg eq '')
    {
        esmith::cgi::genHeaderNonCacheable
            ($q, \%conf, "$array_locale[0]");
    }
    else
    {
        esmith::cgi::genHeaderNonCacheable
            ($q, \%conf, "$array_locale[0]");

        print $q->p ($msg);
        print $q->hr;
    }

    print $q->start_multipart_form (
            -method => 'POST',
            -action => $q->url (-absolute => 1));

	print $q->p ("$array_locale[1]");
    print $q->table ({border => 0, cellspacing => 0, cellpadding => 4},

    esmith::cgi::genWidgetRow ($q, "$array_locale[2]",
                               $q->filefield (-name    => 'updateFile',
                                              -default => "mozus_addons.xpi",
                                              -size  => 32)));

    print $q->p (esmith::cgi::genButtonRow ($q,
                               $q->submit (-name => 'action', -value => "$array_locale[3]")));

    print $q->hidden (-name => 'state', -override => 1, -default => 'perform');

    print $q->endform;
    esmith::cgi::genFooter ($q);
}

sub performAndShowResult ($)
{
    my ($q) = @_;

    my $updateFile    = $q->param ('updateFile');

    if (-e "/tmp/mozus_addon.xpi")
    {
      unlink ("/tmp/mozus_addon.xpi");
    }

    if ("$updateFile" ne "")
    {
      unlink ("/tmp/mozus_addon.xpi");
      open (WR,">/tmp/mozus_addon.xpi") or die ("Error while opening temporally file.\n");
      while (<$updateFile>)
      {
         print WR;
      }
      close WR;
    }
	
    sleep 1;

    esmith::cgi::genHeaderNonCacheable
        ($q, \%conf, "$array_locale[4]");
    print $q->p ("$array_locale[5]");

    print '</table>';
	print $q->p ("<a class='button-like' href='Mozus_addons'>$array_locale[6]</a>");

    print $q->hr;


    esmith::cgi::genFooter ($q);
	
    return;
}
