#!/usr/bin/perl

use strict;
use warnings;

use List::MoreUtils qw(uniq);

my %files;
my $current;
while (my $line = <>) {
  chomp $line;
  if ($line =~ /^diff.*b\/(.*)/) {
    $current = $1;
    if (!-e $current) {
      $current =~ s/^hostapd\//src\/ap\//;
      $current =~ s/^src\/ap\/driver/src\/drivers\/driver/;
      $current =~ s/^src\/ap\/ap\.h/wpa_supplicant\/ap\.h/;
      $current =~ s/^src\/ap\/config/src\/ap\/ap_config/;
      $current =~ s/^src\/ap\/eapol_sm/src\/eapol_auth\/eapol_auth_sm/;
      $current =~ s/^src\/ap\/hostap_common\.h/src\/drivers\/driver_hostap\.h/;
      $current =~ s/^src\/ap\/hostapd_defs\.h/src\/utils\/common\.h/;
      $current =~ s/^src\/ap\/mlme/src\/ap\/ap_mlme/;
      $current =~ s/^src\/ap\/peerkey\.c/src\/ap\/peerkey_auth\.c/;
      $current =~ s/^src\/ap\/pmksa_cache/src\/ap\/pmksa_cache_auth/;
      $current =~ s/^src\/ap\/preauth/src\/ap\/preauth_auth/;
      $current =~ s/^src\/ap\/wpa\.h/src\/ap\/wpa_auth\.h/;
      $current =~ s/^src\/ap\/wpa\.c/src\/ap\/wpa_auth\.c/;
      $current =~ s/^src\/ap\/wpa_ft\.c/src\/ap\/wpa_auth_ft\.c/;
      $current =~ s/^src\/ap\/radiotap/src\/utils\/radiotap/;
      $current =~ s/^src\/ap\/sta_flags\.h/src\/ap\/sta_info\.h/;
      $current =~ s/^src\/ap\/wme/src\/ap\/wmm/;
      $current =~ s/^src\/common\/nl80211_copy\.h/src\/drivers\/nl80211_copy\.h/;
      $current =~ s/^src\/crypto\/aes.c/src\/crypto\/aes-internal.c/;
      $current =~ s/^src\/crypto\/aes_wrap.c/src\/crypto\/aes-wrap.c/;
      $current =~ s/^src\/crypto\/des.c/src\/crypto\/des-internal.c/;
      $current =~ s/^src\/crypto\/md4.c/src\/crypto\/md4-internal.c/;
      $current =~ s/^src\/crypto\/rc4.h/src\/crypto\/crypto.h/;
      $current =~ s/^src\/drivers\/driver_i\.h/wpa_supplicant\/driver_i\.h/;
      $current =~ s/^src\/eap_peer\/eap_aka_prime\.c/src\/eap_peer\/eap_aka\.c/;
      $current =~ s/^src\/eap_peer\/eap_tlv\.c/src\/eap_peer\/eap_peap\.c/;
      $current =~ s/^src\/eap_peer\/eap_tlv\.h/src\/eap_common\/eap_tlv_common\.h/;
      $current =~ s/^src\/eap_server\/eap\.c/src\/eap_server\/eap_server\.c/;
      $current =~ s/^src\/eap_server\/eap_aka\.c/src\/eap_server\/eap_server_aka.c/;
      $current =~ s/^src\/eap_server\/eap_aka_prime\.c/src\/eap_server\/eap_server_aka\.c/;
      $current =~ s/^src\/eap_server\/eap_fast\.c/src\/eap_server\/eap_server_fast\.c/;
      $current =~ s/^src\/eap_server\/eap_gpsk\.c/src\/eap_server\/eap_server_gpsk\.c/;
      $current =~ s/^src\/eap_server\/eap_gtc\.c/src\/eap_server\/eap_server_gtc\.c/;
      $current =~ s/^src\/eap_server\/eap_identity\.c/src\/eap_server\/eap_server_identity\.c/;
      $current =~ s/^src\/eap_server\/eap_ikev2\.c/src\/eap_server\/eap_server_ikev2\.c/;
      $current =~ s/^src\/eap_server\/eap_md5\.c/src\/eap_server\/eap_server_md5\.c/;
      $current =~ s/^src\/eap_server\/eap_methods\.c/src\/eap_server\/eap_server_methods\.c/;
      $current =~ s/^src\/eap_server\/eap_mschapv2\.c/src\/eap_server\/eap_server_mschapv2\.c/;
      $current =~ s/^src\/eap_server\/eap_pax\.c/src\/eap_server\/eap_server_pax\.c/;
      $current =~ s/^src\/eap_server\/eap_peap\.c/src\/eap_server\/eap_server_peap\.c/;
      $current =~ s/^src\/eap_server\/eap_psk\.c/src\/eap_server\/eap_server_psk\.c/;
      $current =~ s/^src\/eap_server\/eap_sake\.c/src\/eap_server\/eap_server_sake\.c/;
      $current =~ s/^src\/eap_server\/eap_sim\.c/src\/eap_server\/eap_server_sim\.c/;
      $current =~ s/^src\/eap_server\/eap_tls\.c/src\/eap_server\/eap_server_tls\.c/;
      $current =~ s/^src\/eap_server\/eap_tls_common\.c/src\/eap_server\/eap_server_tls_common\.c/;
      $current =~ s/^src\/eap_server\/eap_tlv\.c/src\/eap_server\/eap_server_peap\.c/;
      $current =~ s/^src\/eap_server\/eap_tnc\.c/src\/eap_server\/eap_server_tnc\.c/;
      $current =~ s/^src\/eap_server\/eap_ttls\.c/src\/eap_server\/eap_server_ttls\.c/;
      $current =~ s/^src\/eap_server\/eap_vendor_test\.c/src\/eap_server\/eap_server_vendor_test\.c/;
      $current =~ s/^src\/eap_server\/eap_wsc\.c/src\/eap_server\/eap_server_wsc\.c/;
      $current =~ s/^src\/drivers\/radiotap/src\/utils\/radiotap/;
      $current =~ s/^src\/drivers\/scan_helpers\.c/wpa_supplicant\/scan\.c/;
      $current =~ s/^src\/hlr_auc_gw\/hlr_auc_gw\.c/hostapd\/hlr_auc_gw\.c/;
      $current =~ s/^src\/hlr_auc_gw\/milenage/src\/crypto\/milenage/;
      $current =~ s/^src\/tls\/asn1_test\.c/tests\/test-asn1\.c/;
      $current =~ s/^wpa_supplicant\/ctrl_iface_/wpa_supplicant\//;
      $current =~ s/^wpa_supplicant\/dbus\/ctrl_iface_/wpa_supplicant\/dbus\//;
      $current =~ s/^wpa_supplicant\/dbus\./wpa_supplicant\/dbus\/dbus\./;
      $current =~ s/^wpa_supplicant\/dbus_/wpa_supplicant\/dbus\/dbus_/;
      $current =~ s/^wpa_supplicant\/dbus\/dbus\./wpa_supplicant\/dbus\/dbus_old\./;
      $current =~ s/^wpa_supplicant\/dbus\/dbus_handlers/wpa_supplicant\/dbus\/dbus_old_handlers/;
      $current =~ s/^wpa_supplicant\/mlme/src\/ap\/ap_mlme/;
      $current =~ s/^wpa_supplicant\/tests\/test_/tests\/test-/;
    }
  }
  else {
    my @copyrights = ();
    if (exists $files{$current}) {
      @copyrights = @{$files{$current}};
    }
    push @copyrights, $line;
    $files{$current} = \@copyrights;
  }
}

foreach my $file (sort keys %files) {
  if ($file =~ /^(CONTRIBUTIONS|COPYING|README|eap_example|doc|mac80211_hwsim|radius_example|test|wlantest|www)/) {
    next;
  }
  print "\nFile: $file";
  if (!-e $file) {
    print " (file does not exist)";
  }
  print "\n";

  my %owners;
  foreach my $copyright (@{$files{$file}}) {
    if ($copyright =~ /([0-9]{4})(-([0-9]{4}))?,?\s*(.*)/) {
      my $year_start = $1;
      my $year_end = $3;
      my $owner = $4;

      my @years = ();
      if (exists $owners{$owner}) {
        @years = @{$owners{$owner}};
      }
      if (defined $year_end) {
        for (my $year = $year_start; $year <= $year_end; $year++) {
          push @years, $year;
        }
      }
      else {
        push @years, $year_start;
      }

      my @unique_years = uniq @years;
      $owners{$owner} = \@unique_years;
    }
  }

  foreach my $owner (sort keys %owners) {
    print "Copyright: ";
    my $previous;
    my $ranging;
    foreach my $year (@{$owners{$owner}}) {
      if (!defined $previous) {
        print $year;
        $previous = $year;
        next;
      }
      if ($year == $previous + 1) {
        if (!$ranging) {
          print "-";
          $ranging = 1;
        }
        $previous = $year;
        next;
      }
      if ($ranging) {
        print $previous;
        $ranging = 0;
      }
      print ",$year";
      $previous = $year;
    }

    if ($ranging) {
      print $previous;
    }
    print ", $owner\n";
  }
}
