#!/usr/local/bin/perl -w
use Getopt::Std;

sub find { shift }
my $dir = $0;
$dir =~ s#/\w+$#/#;
eval {require "$dir/utils.pm";};

my %opt;
getopts('rq', \%opt);

open (WORDS, find('terms.txt')) or die "Can't open terms.txt, aborting";

my (@words, %words);
while (<WORDS>) {
  chomp;
  s/\s+.*$//;
  push @words, $_;
  $words{$_} = $#words;
}

if (@ARGV and $ARGV[0] ne '-1') {
  foreach my $wordnum (@ARGV) {
    my $outword = $opt{r}? $words{$wordnum}: $words[$wordnum];
    if ($opt{q}) {
      print $outword, "\n";
    } else {
      printf "%5s %s\n", $wordnum, $outword;
    }
  }
} else {
  while (<>) {
    #printf "%5d %s\n", $_, $words[$_];
    print, next if /^\s*\#/;
    chomp;
    my $outword = $opt{r}? $words{$_}: $words[$_];
    if ($opt{q}) {
      print $outword, "\n";
    } else {
      printf "%s %s\n", $_, $outword;
    }
  }
}
