#!/usr/bin/perl
# Filename:	signature module
# Author:	David Ljung Madison <DaveSource.com>
# See License:	http://MarginalHacks.com/License
# Description:	Creates a random signature
# Used by:	mail_editor, diffsig
use strict;

my $SIG_PATH="$ENV{HOME}/.elm/sigs";
my $SIG_QUOTES="$SIG_PATH/quotes";

my $FULL_NAME="Your Full Name";
my $PHONE="Your-Phone";

my @NAMES=(
	"PUT YOUR NAMES HERE",
	"PUT YOUR NAMES HERE",
	"PUT YOUR NAMES HERE",
	);

my @EMAIL=(
	"PUTYOUREMAILADDRESSESHERE",
	"PUTYOUREMAILADDRESSESHERE",
	"PUTYOUREMAILADDRESSESHERE",
	);

#########################
# Give them a chance to see the message
#########################
sub slow_death {
  foreach ( @_ ) { print "\n[$0]  $_\n\n"; }
  sleep 1;
  die("\n");
}

#########################
# Choose name to sign with
#########################
sub pick_name {
  $NAMES[int(rand(scalar(@NAMES)))];
}

#########################
# Top of sig
#########################
sub sig_top {
  my ($len) = @_;

  my $text;

  $text.="-"x$len."\n";
  # Center the chosen email address
  my $email=$EMAIL[int(rand(scalar(@EMAIL)))];
  my $pad_left=int(($len-length($email))/2)-length($FULL_NAME);
  my $pad_right=int(($len-length($email))/2+.5)-length($PHONE);
  $text.=$FULL_NAME." "x$pad_left.$email." "x$pad_right.$PHONE."\n";

  $text;
}

#########################
# xmcd lets us see what we are listening to
# See xmcd for more info
#########################
sub cd_info {
  my ($len) = @_;

  # Find the file
  my @cd_info=</tmp/.cdaudio/curr.*>;
  return "" if (!@cd_info);

  open(CD_INFO,$cd_info[0]) || return "";
  my ($disc,$track,$status);
  while(<CD_INFO>) {
    $disc=$1 if (/^Disc:\s+(.*)$/);
    $track=$1 if (/^Track:\s+(.*)$/);
    $status=$1 if (/^Status:\s+(.*)$/);
  }
  close(CD_INFO);

  return "" if ($status !~ /Playing/);

#  $len--;
  my $line=substr("Music: $track",0,$len);
  $len-=length($line)+2;
  $line=sprintf("$line %${len}.${len}s]","[$disc") if ($len>3);
  $line."\n";
}

#########################
# Which quote?
#########################
sub pick_quote {
  my ($str) = @_;

  my @quotes;
  my $text;
  my $len;	# Maximum line length

  #########################
  # Read in the quotes directory
  #########################
  if(!opendir(SIG_QUOTES,"$SIG_QUOTES")) {
    print STDERR "Couldn't open sig quotes directory: $SIG_QUOTES\n";
    sleep 1;
  }
  @quotes=grep(!/^\.{1,2}$/,readdir(SIG_QUOTES));
  # Ignore file.credit (those are for credits and whatnot)
  @quotes=grep(!/\.credit$/,@quotes);
  closedir(SIG_QUOTES);

  #########################
  # If we have an arg, then use that as a search
  # string for selecting quotes (unless it is just --------)
  #########################
  if ($str && $str !~ /^-*$/) {
    @quotes=grep(/$str/i,@quotes);
    if($#quotes == -1) {
      print "ERROR:  No sigfile names matched $str\n";
      exit -1;
    }
  }

  #########################
  # Pick one
  #########################
  srand(time ^ $$);	# Seed the random # generator
  my $sig_quote_num=int(rand(scalar(@quotes)));
  my $quote="$SIG_QUOTES/$quotes[$sig_quote_num]";

  if(!open(QUOTE,"$quote")) {
    print "Couldn't read sig quote:  $quote\n";
    sleep 1;
    return "";
  }
  while(<QUOTE>) {
    $text.=$_;
    $len=length($_) if (length($_)>$len)
  }
  close(QUOTE);

  ($text,$len-1);
}

#########################
# Get a full signature
#########################
sub get_sig {
  my ($str) = @_;
  my ($quote,$len)=pick_quote($str);
  sig_top($len).cd_info($len).$quote;
}

1;


