#!/usr/bin/env perl
  
use warnings;
use strict;

my $ipt = $ARGV[0];

open(IPT, '<', $ipt) or die "Couldn't open $ipt\n";
my @rules = ();
my $change = 0;
while (<IPT>){
  chomp;
  if (
      (m/(^:|.*\-[Aj]\s+)(CNI|NOMAD\-(?!ADMIN)|DOCKER).*/) or
      (m/.*-A\s+NOMAD\-ADMIN/ and not m/\-\-comment\s+"ansible/) or
      (m/.*\-o\s+docker0.*/)
     ){
    $change = 1;
    print "Removing line $_ from $ipt\n";
    next;
  }
  push @rules, $_;
}
close IPT;
if ($change){
  open(IPT, '>', $ipt) or die "Couldn't open $ipt\n";
  print IPT join("\n", @rules);
  close IPT;
}
