#!/usr/bin/perl
# -*- Mode: perl -*-
#template by ~tconnors/bin/newplscript
#Tue Feb  1 15:37:52 EST 2005

# $Revision: 1.4 $ $Date: 2024/01/04 11:52:13 $
# $Id: removedup,v 1.4 2024/01/04 11:52:13 tconnors Exp $
# $Header: /home/tconnors/cvsroot/bin/removedup,v 1.4 2024/01/04 11:52:13 tconnors Exp $
# $RCSfile: removedup,v $

# This program removes duplicate words from stdin, without needing to
# pipe to sort and uniq

# Keeps first entry; if you want to keep last entry, then you need to
# pipe to tac before and after this

use strict;
use warnings;
#use mycommon;

my (@SAVEARGV)=@ARGV;

sub usageerror(;$) {
  my ($error)=(@_);
  if (defined($error)) {
    print STDERR "Usage error: $error\n";
    print STDERR "Usage was: $0 @SAVEARGV\n";
  }
  usage(1);
}

sub usage(;$) {
  my ($exit)=(@_);
  print STDERR "Usage: $0 [--printdups] < in > out\n";
  $exit=defined($exit) ? $exit : 0;
  exit $exit;
}

my $printdups;

sub parseinput() {
  $printdups=0;
  while (defined($_= shift (@ARGV))) {
    /^--printdups$/ && do { $printdups=1 ; 1; } ||
    /^--help$/ && usage() ||
    /^-/ && usageerror("Unrecognised switch: $_") ||
    usageerror("Unrecognised option: $_") ||
    last;
  }
  defined($_) && unshift @ARGV, $_;
}

#########################

parseinput();
select STDOUT ; $| = 1;

my %lines;

while (<>) {
  if (!defined $lines{$_}) {
    $lines{$_}=1;
    if (!$printdups) {
      print $_;
    }
  } elsif ($printdups) {
    print $_;
  }
}
