#!/usr/bin/perl
# --------------------------------------------------
#  Association v0.1 by JC
#   -- Usage: association <anExtention>
#      Returns the command line corresponding
#        to the extention in the file 'associations.txt'
#   -- Exemple: association ps
#      Returns "gv"
#      That means: 'gv' has to be run to view a <ps> file.
# --------------------------------------------------

if($#ARGV == -1)
{ $extension = "txt";
} else 
{ $extension = $ARGV[0];
}


open(ASSOCIATIONS, "/usr/local/bin/associations.txt");
  while($line = <ASSOCIATIONS>)
  { chop($line);
    @chars = split(//,$line);
    $_ = $chars[0];

    ## comment line
    if(tr/\#/\#/ == 1)
    { next;
    }
    ## empty line
    if($#chars == -1)
    { next;
    }

    @extensionAndCommand = split(/\./,$line);

    # txt.???
    $_ = $extensionAndCommand[0];
    if(m/$extension/i)
    {
      # txt..text
      @commandChars = split(//,$extensionAndCommand[1]);
      if($#commandChars == -1)
      { $extension = $extensionAndCommand[2];
        next;
      }

      # txt.emacs -cr green
      print($extensionAndCommand[1]);
      close(ASSOCIATIONS);
      exit 0;
    }
  }
close(ASSOCIATIONS);
exit -1;
