#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Pointing Device configurator : Manage your mouse etc
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Ravi Pratap <ravi@che.iitm.ac.in>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library 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 Library General Public License for more details.
#
# You should have received a copy of the GNU Library 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.

# Best viewed with 100 columns of width.

# Configuration files affected:
#
# /etc/X11/XF86Config

# Running programs affected:
#
# /etc/X11/X


BEGIN {
  $SCRIPTSDIR = "/usr/share/setup-tool-backends/scripts";
  if ($SCRIPTSDIR =~ /^@scriptsdir[@]/)
  {
      $SCRIPTSDIR = ".";
      $DOTIN = ".in";
  }
  
  require "$SCRIPTSDIR/general.pl$DOTIN";
  require "$SCRIPTSDIR/platform.pl$DOTIN";
  require "$SCRIPTSDIR/util.pl$DOTIN";	
  require "$SCRIPTSDIR/file.pl$DOTIN";	
  require "$SCRIPTSDIR/xml.pl$DOTIN";	
  require "$SCRIPTSDIR/parse.pl$DOTIN";
#require "$SCRIPTSDIR/mouse.pl$DOTIN";
}


# -- Tool information --- #

$name = "mouse";
$version = "1.4.2";
@platforms = ("redhat-5.2", "redhat-6.0", "redhat-6.1", "redhat-6.2", 
              "redhat-7.0", "redhat-7.1", "redhat-7.2", "turbolinux-7.0");

$description =<<"end_of_desc;";
       Configures Pointing Tool Device
end_of_desc;

@conf_files = ("/etc/X11/XF86Config");

# Helper functions

sub gst_mouse_conf_get
{
    my %hash;
    my $config_file;
    local *CONFIG_FILE;

    $config_file = &gst_file_open_read_from_names (@conf_files);
    if (not $config_file) { return; }
    *CONFIG_FILE = $config_file;

    while (<CONFIG_FILE>) {
        chomp;

        # We're only interested in the pointer section
        
        next if (/^\#/ || /^$/);
        last if (/Pointer/);
    }
    
    while (<CONFIG_FILE>) {
        my ($a, $b);
        
        chomp;
        next if (/^\#/ || /^$/);
        last if (/EndSection/);

        ($a, $b) = split (" ", $_, 2);

        if ($b) { $hash{"$a"} = $b; }
        else    { $hash{"$a"} = "on"; }
    }
    
    return \%hash;

}


sub xml_print ()
{
    my $h = $_[0];
    my @tags = ('Protocol', 'Device', 'BaudRate', 'SampleRate', 'Emulate3Buttons', 'Emulate3Timeout', 'ChordMiddle');
    my $i;

    &gst_xml_print_begin ();
    
    while ($i = shift @tags) {
        if (exists $$h{$i} && $$h{$i} eq "on") { gst_xml_print_line ("<$i/>\n"); } 
        elsif (exists $$h{$i}) { gst_xml_print_line ("<$i>$$h{$i}</$i>\n"); }
    }
    
    &gst_xml_print_end ();
}




# The main stuff 

# --- Write new configuration to file --- #

sub set
{
    print "Not implemented yet! \n";
    
    &gst_report_end ();
    
}


# --- Get configuration from file --- #

sub get 
{
    my $hash;
    
    $hash = &gst_mouse_conf_get ();
    
    &xml_print ($hash);
    &gst_report_end ();

}


# --- Filter config: XML in, XML out --- #

sub filter
{
    print "Not implemented yet! \n";
    
    &gst_report_end ();
}

# --- Main --- #

# get, set and filter are special cases that don't need more parameters than a ref to their function.
# Read general.pl.in:gst_run_directive to know about the format of this hash.

$directives = {
  "get"    => [ \&get,    [], "" ],
  "set"    => [ \&set,    [], "" ],
  "filter" => [ \&filter, [], "" ]
    };

$tool = &gst_init ($name, $version, $description, $directives, @ARGV);
&gst_platform_ensure_supported ($tool, @platforms);
&gst_run ($tool);
