#! /usr/bin/perl

# this is only a test program

use Getopt::Std;

@templatedirs= split / /, qq#$ENV{'HOME'}/.fai-chboot/ /etc/fai-chboot/#;

# parse the normal command line option
getopts('Bd:ehnvlLiIp:f:Frk:Sot:',\%opt);

sub readtemplate {

  my @newargs;
  my $filename = $opt{'t'};

  for $dir (@templatedirs) {
    print "look into $dir\n";
    next unless (-f "$dir/${filename}.tmpl");
    print "reading template ${filename}.tmpl\n";
    open my $fh, "$dir/${filename}.tmpl" or die "$@\n";
    while (<$fh>) {
      next if /^#/;
      chomp;
      push @newargs, $_;
    }

    @ARGV = @newargs;
    getopts('Bd:ehnvlLiIp:f:Frk:So',\%gopt);
    return;
  }

warn "Template ${filename}.tmpl not found.\n";
}

# read a template if -t was given
readtemplate() if $opt{'t'};

foreach (keys %opt) {
  # overwrite the template values with the command line value
  # options given in the template, cant be disabled by the command line
  $gopt{$_}= $opt{$_};
}


# print resulting options
foreach (keys %gopt) {
  print "Option $_ was set to $gopt{$_}\n";
}

print "I'm finished";
