#!/usr/bin/perl
eval "exec /usr/bin/perl -S $0 $*"
    if $running_under_some_shell;
    # this emulates #! processing on NIH machines.
    # (remove #! line above if indigestible)


# checkalias.pl, an elm-style checkalias script for showing the address
# of mutt aliases.  It will look in your .muttrc or a specified file for
# alias statements, and also recursively follow any source statements.
#
# Usage: checkalias.pl alias [alias ...]
#
# Author: Mikko Hänninen <Mikko.Hanninen@iki.fi>, 25 Oct 1997

# Changed to also parse source-directives to include other files.
# - Mikko Hänninen, 4 Dec 1998
# Changed to correctly parse paths of the format ~/...
# - Mikko Hänninen, 13 Dec 1998
# Added the -a and -s options, handling of continuation lines, \# escapes,
# new help function, "not found" printing
# - Mikko Hänninen, 26 May 2000


require "getopts.pl";

&Getopts('ahf:sD') || dohelp(1);

$listall = $opt_a;
$simple = $opt_s;
$debug = $opt_D;

if ($opt_f) { $aliasfile = $opt_f; }
else { $aliasfile = $ENV{HOME} . "/.muttrc"; }
push @files, $aliasfile;
@checkaliases = @ARGV;

if ($debug) {
  print "\$listall = $listall\n";
  print "\$simple = $simple\n";
  print "\$aliasfile = $aliasfile\n";
  print "\@checkaliases = ", join(", ", @checkaliases), "\n";
}

if ($opt_h || (!@checkaliases && !$listall) ) {
  dohelp(0);
}

foreach $alias (@checkaliases) {
  $notfound{$alias} = 1;
}


for $aliasfile (@files) {

  # make sure to only read each file once
  $checked{$aliasfile} = 1;
  if ($debug) { print STDERR "Reading file $aliasfile\n"; }

  $basepath = $aliasfile;
  $basepath =~ s#[^/]+$##;

  open(ALIASES, "<$aliasfile") || die("Couldn't open $aliasfile for reading: $!");
  while (<ALIASES>) {
    chomp;

    if (/\\$/) {
      # line continued on next line
      s/\\$//;
      $prevline .= $_;
      next;
    }

    if ($prevline) {
      # previous line was continued on this line
      if ($prevline =~ /\S$/ && $_ =~ /^\S/)
        { $prevline .= " "; }  # add space between the joined lines to have some whitespace there
      $_ = $prevline . $_;
    }

    if (/^\s*source\s+(\S+)/) {
      $filename = $1;
      if ($filename =~ m#^\s*/#) {
        $newfile = $1;
      }
      elsif ($filename =~ m#\s*~(/.+)#) {
        $newfile = $ENV{HOME} . $1;
      }
      else {
        $newfile = $basepath . $filename;
      }
      push (@files, $newfile) unless ($checked{$newfile});
      print "Adding file $newfile\n" if $debug;
      next;
    }
    if (/^\s*alias\s+(\S+)\s+([^<]+)\s+(<.+>)\s*$/i) {
      $alias = $1;
      $name = $2;
      $address = $3;
      print "Found alias: $alias $name $address\n" if $debug;

      $name =~ s/^["']//;
      $name =~ s/["']$//;
      $name =~ s/\\#/#/;

      $printalias = 0;
      if ($listall) {
        $printalias = 1;
      }
      else {
        foreach (@checkaliases) {
          if ($_ eq $alias) {
            $printalias = 1;
          }
        }
      }

      delete $notfound{$alias} if ($notfound{$alias});  # delete from "not found" list

      if ($printalias) {
        if ($simple)
          { print "$alias \"$name\" $address\n"; }
        else
          { print "$alias is \"$name\", mail sent to $address\n"; }
      }
    }
  }
  close(ALIASES);
}

unless ($simple) {
  foreach $alias (sort keys %notfound) {
    print "$alias: not found\n";
  }
}


sub dohelp {
  my ($ec) = shift || 0;
  my ($progname) = $0;
  $progname =~ s#^.*/##;
  print <<EOF
Usage: $progname [-a] [-h] [-f mailalias file] [-s] aliases...
-a       list all aliases
-h       this help
-f file  specify mail alias file, defaults to .muttrc
-s       simplified output: aliasname "Full Name" <address>,
         no reporting of aliases which were not found
EOF
;

  exit $ec;
}

