#!/usr/local/bin/perl

### very preliminary version, just enough to get it working
### still missing: check proper version of tar, cpio, gzip
### needs cleaning                           WF Aug 6, 2001

if ( $ARGV[0] ) {
  print << 'EOF';
  Sorry, no help available, no options or arguments accepted (yet)
  Please call configure without arguments. If optional programs are
  not found, then you can choose between
  - giving the correct location of the program if installed
  - including the code without having the program
  - or skipping the code for the missing program.
EOF
  exit;
}
$|=1;
open IN, "lesspipe.sh.in";
open OUT, ">lesspipe.sh";
$in = 1;
check_file_vers();
while (<IN>) {
# line #ifdef prog1, prog2, ... encountered, store prog1, prog2, ... in @progs
  if ( /^#ifdef\s+(.*)/ ) {
    %rep = ();
    $_ = $1;
    chomp;
    @progs = split /,/;
# check if @progs existing and executable ($in == 1)
    $in = inpath ("Include code anyway", @progs);
# line #endif encountered, clear list of replacement strings %rep
  } elsif (/^#endif\s/ ) {
    %rep = ();
# unconditionally accept all statements after #endif
    $in = 1;
  } elsif ( $in ) {
# make replacements in lines if neccessary ($in == 1)
    for $p (keys %rep) {
      s/\b$p\b/$rep{$p}/ unless /^#/;
    }
# and write out the line
    print OUT;
  }
}
close OUT;
chmod 0755, "lesspipe.sh";

sub inpath {
  my $string = shift;
  for my $prog ( @_ ) {
    $rep{file} = $have{file} if $prog eq 'file';
    return 1 if $have{$prog} and $have{$prog} !~ /^n/i;
    return 0 if $have{$prog} and $have{$prog} =~ /^n/i;
    my $ok = 0;
    print "checking $prog...";
    for ( split /:/, $ENV{PATH} ) {
      next unless m|^/|; # consider only absolute PATH elements
      $have{$prog} = "$_/$prog", last if -x "$_/$prog";
    }
    if ( $have{$prog} ) {
      print "$have{$prog}\n";
    } else {
      print "not found\n";
      my $yesno = get_answer($string, $prog);
      $have{$prog} = $yesno;
      return 0 if $yesno !~ /^y/i;
    }
  }
  return 1;
}

sub get_answer {
  my ($string, $prog) = @_;
  my $yesno;
  while ( $yesno !~ /^[yn]/i ) {
    print "$string [y/N or <full_path_to_$prog>] ? ";
    $yesno = <STDIN>;
    chomp $yesno;
    $yesno = 'N' unless $yesno;
    if ( $yesno =~ m|^/| ) {
      if ( -x $yesno ) {
	$rep{$prog} = $yesno;
	$yesno = 'y';
      } else {
	print "Program $prog not found (or at least not executable)\n";
	$yesno = '';
      }
    }
  }
  return $yesno;
} 

sub check_file_vers {
  $recursion++;
# special treatment for file program
  inpath("Continue anyway", 'file');
  my $rc = system "$have{file} -L ./configure >/dev/null 2>&1";
  if ( $rc ) {
    print "  found system version of file, consider using GNU file\n";
    return if $recursion > 1;
    my $yesno = get_answer("Continue anyway", 'file');
    if ($rep{file} =~ m|^/|) {
      $have{file} = $rep{file};
      check_file_vers();
    }
    exit if $yesno =~ /^n/i;
  } else {
    my $vers = `$have{file} -v`;
    $vers = $1 if $vers =~ /(\d+\.\d+)/;
    if ( $vers < 3.27 ) {
      $have{file} .= ' -L';
      print "  found GNU file $vers, need at least 3.27 for full functionality\n";
      return if $recursion > 1;
      my $yesno = get_answer("Continue anyway", 'file');
      if ($rep{file} =~ m|^/|) {
        $have{file} = $rep{file};
        check_file_vers();
      }
      exit if $yesno =~ /^n/i;
    } else {
      $have{file} .= ' -L -s';
      print "  found GNU file$vers (ok)\n";
    }
  }
}
