#!/usr/local/bin/perl
#
# aliasprepare - get a file ready for alias assignment
# Moves the alias to the beginning of each line.
# For lines without aliases, pretends the alias is "~~~~".
#
# Usage: aliasprepare config [files]
#

if ($#ARGV<0)
  {print STDERR "Usage: aliasprepare prod.cnf [files]\n";exit 1;}

#
# read the config file
$config = shift;
open(CONFIG,"<$config") || die "$config: $!\n";
while (<CONFIG>)
{
  if (/:alias:/)
  {
    split(':');
    $anum = $_[0];
    last;
  }
}

#
# process
while (<>)
{
  if (/\t$anum:/o)
  {
    s/(.*)\t$anum:([^\t\n]*)/$anum:$2\t$1/;
  }
  else
  {
    $_ = "$anum:~~~~\t" . $_;
  }
  print $_;
}
