+close($index);
+rename 'html/index.new', 'html/index.html';
+
+debug 'auth_header' => $auth_header;
+
+
+my $department_category_author;
+foreach my $department ( sort keys %$auth_department ) {
+ foreach my $authid ( sort @{ $auth_department->{$department} } ) {
+ my @categories = keys %{ $authors->{$authid}->{aut} };
+ push @categories, keys %{ $authors->{$authid}->{__sec} };
+ foreach my $category ( sort @categories ) {
+ push @{ $department_category_author->{$department}->{$category} }, $authid;
+ push @{ $department_category_author->{'AAA_ukupno'}->{$category} }, $authid if $department_in_sum->{$department};
+ if ( my $group = $department_in_group->{ $department } ) {
+ push @{ $department_category_author->{$group}->{$category} }, $authid;
+ } else {
+ $skip->{'department_not_in_group'}->{ $department }++;
+ }
+ }
+ }
+}
+
+debug 'department_category_author' => $department_category_author;
+
+
+sub department_html {
+ my ( $fh, $department, $type, $label ) = @_;
+
+ print $fh qq|<a name="$type"><h2>$label</h2></a>\n|;
+
+ foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
+
+ my @authids = @{ $department_category_author->{$department}->{$category} };
+ next unless @authids;
+
+ my @biblionumber = unique_biblionumber map { @{ $authors->{$_}->{$type}->{$category} } } grep { exists $authors->{$_}->{$type}->{$category} } @authids;
+
+ next unless @biblionumber;
+
+ my $label = $category_label->{$category} || 'Bez kategorije';
+ print $fh qq|<a name="$type-$category"><h3>$label</h3></a>\n<ol>\n|;
+
+ print $fh li_biblio( $_ ) foreach @biblionumber;
+
+ print $fh qq|</ol>|;
+ }
+
+}
+
+
+mkdir 'html/departments' unless -d 'html/departments';
+
+open(my $dep_fh, '>:encoding(utf-8)', 'html/departments/index.new');
+print $dep_fh html_title('Odsjeci Filozofskog fakulteta u Zagrebu'), qq|<ul>\n|;
+foreach my $department ( sort keys %$department_category_author ) {
+ my $dep = $department || 'Nema odsjeka';
+ my $dep_file = unac_string('utf-8',$dep);
+ print $dep_fh qq|<li><a href="$dep_file.html">$dep</a></li>\n|;
+ open(my $fh, '>:encoding(utf-8)', "html/departments/$dep_file.new");
+
+ print $fh html_title($department . ' bibliografija');
+ print $fh qq|<h1>$department bibliografija</h1>\n|;
+
+ my @authids;
+ foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
+ push @authids, @{ $department_category_author->{$department}->{$category} };
+ }
+ html_year_selection $fh => @authids;
+
+ my $i = 0;
+ while ( $i < $#toc_type_label ) {
+ my $type = $toc_type_label[$i++] || die "type";
+ my $label = $toc_type_label[$i++] || die "label";
+ department_html( $fh, $department, $type, $label );
+ }
+
+ print $fh html_end;
+ close($fh);
+ rename "html/departments/$dep_file.new", "html/departments/$dep_file.html";
+}
+print $dep_fh qq|</ul>\n|, html_end;
+close($dep_fh);
+rename 'html/departments/index.new', 'html/departments/index.html';
+
+my $azvo_stat_biblio;
+
+foreach my $department ( sort keys %$department_category_author ) {
+ foreach my $category ( sort keys %{ $department_category_author->{$department} } ) {
+ foreach my $authid ( @{ $department_category_author->{$department}->{$category} } ) {
+ my $group = $auth_group->{$authid};
+ if ( ! $group ) {
+ push @{ $skip->{no_auth_group} }, $authid;
+ next;
+ }
+ foreach my $type ( keys %{ $authors->{$authid} } ) {
+ next unless exists $authors->{$authid}->{$type}->{$category};
+ push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{$group} }, @{ $authors->{$authid}->{$type}->{$category} };
+ push @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{''} }, @{ $authors->{$authid}->{$type}->{$category} };
+ }
+ }
+ foreach my $type ( keys %{ $azvo_stat_biblio->{ $department }->{ $category } } ) {
+ foreach my $group ( keys %{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type } } ) {
+ my @biblios = unique_biblionumber @{ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } };
+ $azvo_stat_biblio->{ $department }->{ $category }->{ $type }->{ $group } = [ @biblios ];
+ }
+ }
+ }
+}
+
+debug 'azvo_stat_biblio' => $azvo_stat_biblio;
+
+my @report_lines;
+my @report_labels;
+
+my $label;
+my $sub_labels;
+open(my $report, '<:encoding(utf-8)', 'AZVO.txt');
+while( <$report> ) {
+ chomp;
+ if ( /^([^\t]+)\t+(.+)/ ) {
+ $label = $1;
+ push @report_labels, $label;
+ my $type = [ map { m/\s+/ ? [ split(/\s+/,$_) ] : [ $_, 'aut' ] } split (/\s*\+\s*/, $2) ];
+ push @report_lines, [ $label, @$type ];
+ } elsif ( /^\t+([^\t]+):\t+(\d+)(\w*)\t*(.*)$/ ) {
+ push @{ $sub_labels->{$label} }, [ $1, $2, $3, $4 ];
+ my $sub_label = $1;
+ pop (@report_labels) if ( $report_labels[ $#report_labels ] =~ m/^$label$/ ); # remove partial name
+ push @report_labels, $label . $sub_label;
+ } else {
+ die "ERROR: [$_]\n";
+ }
+}
+
+debug 'report_lines', \@report_lines;
+debug 'sub_labels', $sub_labels;
+debug 'report_labels', \@report_labels;
+
+my @departments = ( sort { lc($a) cmp lc($b) } keys %$azvo_stat_biblio );