#!/usr/bin/perl -w

# Author: Emidio Planamente
#
# Email : eplanamente@gmx.ch
#
# History of change
#  Version  Date        Description
#  1.0      xxxx-xx-xx  Initial creation



use strict;
use File::Basename;

# add library emi::Utility
use lib "/usr/lib/perl/";
use emi::Utility;


# script informations
my $BASE=basename($0);
my $VERSION = "Vx.y";
my $AUTHOR  = "Emidio Planamente";
my $EMAIL   = "eplanamente\@gmx.ch";


# global variables
my $SEP = "-" x 80 . "\n";


# global scripts
$CMD_XXX = "/bin/xxx";

# name of the possible configuration file
my $CONFIG = "/etc/..:";


# configuration file (default values)
my $XXX = "1";




#*********************************************************************************
#*********************************************************************************
#*********************************************************************************

sub main()
{
  print "$SEP";
  print_version();

  # get switch
  use Getopt::Std;
  my %switch = ();  
  getopts("ab:c:dhv", \%switch);
  if($switch{d})
  {
    print "Debug: following switches are set:\n"; 
    Utility::print_hash(%switch);  
  }  

  my $status = start(%switch);

  print "$SEP";
  return $status;
}

sub print_version()
{
  print "$BASE $VERSION\n";
  print "Written by $AUTHOR ($EMAIL)\n";
}

sub print_usage()
{
  print "\n";
  print "  $BASE [options]\n";
  print "\n";
  print "  Short description.\n";
  print "\n";
  print "  $BASE -a - make aaa\n";
  print "  $BASE -b - make bbb\n";
  print "\n";
  print "  Detailled description.\n";
  print "\n";
  print "\n";
  print "  OPTIONS\n";
  print "\n";
  print "    -x: short description\n";
  print "        Bla bla bla.\n";
  print "\n";
  print "    -d: debug mode\n";
  print "        Print a lot of useful debug informations.\n";
  print "\n";
  print "    -h: help mode\n";
  print "        Print this help.\n";
  print "        This switch can't be used with others switches.\n";
  print "\n";
  print "    -v: version\n";
  print "        Print the version of this script.\n";
  print "        This switch can't be used with others switches.\n";
  print "\n";
  print "\n";
  print "  AUTHOR\n";
  print "\n";
  print "    $AUTHOR ($EMAIL)\n";
  print "\n";
  return 1;
}

sub start()
{
  my %switch = @_;

  if($switch{h})
  {
    # help mode
    
    return print_usage();
  }
  elsif($switch{v})
  {
    # version mode
    
    # no code is required because the version is always showed
    return 1;
  }
  elsif($switch{x} and !$switch{y})
  {
    # xxx mode

    if($switch{x} and @ARGV == 2)
    {
      return sub_1($switch{s}, $ARGV[0], $ARGV[1], $switch{d});
    }
    elsif(!$switch{s} and @ARGV == 2)
    {
      return sub2("695m", $ARGV[0], $ARGV[1], $switch{d});
    }      
  }
  elsif($switch{z} and !$switch{a})
  {
    # yyy mode
    
    return restore($ARGV[0], $ARGV[1], $switch{d});
  }
  
  die "ERROR: wrong arguments. Type $BASE -h for help\n";    
  return 0; 
}

sub read_config(@) 
{
  my ($config1, $config2) = @_;

  # read XXX configuration
  if(-f $config1)
  {
    eval `cat $config1`;
    return "config1";    
  }
    
  # read YYY configuration 
  if(-f $config2)
  {
    eval `cat $config2`;
    return "config2";    
  }
  
  return "missing";
}

#*********************************************************************************
#*********************************************************************************
#*********************************************************************************


sub sub_1()
{
  my ($aaa, $bbb, $ccc, $debug) = @_;

  print "Debug: XXX MODE: \$aaa=$aaa, \$bbb=$bbb, \$ccc=$ccc\n" if $debug;

  return 1;
}

exit !main();

