#! /usr/bin/perl use strict ; use warnings ; use YAML ; my $DEF_YAML_FILE = 'data/attic.yaml' ; my $DEF_DOCS_DIR = 'docs' ; my $DEF_FLAGS_DIR = "$DEF_DOCS_DIR/flags" ; my $BANNER_FILE = 'banners' ; my $prog = substr $0, rindex ( $0, '/' ) + 1 ; my $Usage = < 2 ; $opt{v} ||= $opt{d} ; my $TAG = $opt{f} ? 'DID' : 'WOULD' ; my $mods = 0 ; my $YAML_FILE = shift || $DEF_YAML_FILE ; my $FLAGS_DIR = shift || $DEF_FLAGS_DIR ; if ( $opt{v} ) { print "using yaml file '$YAML_FILE'\n" ; print "using flags dir '$FLAGS_DIR'\n" ; } Error "no yaml file ($YAML_FILE)" unless -f $YAML_FILE ; for my $dir ( $DEF_DOCS_DIR, $FLAGS_DIR ) { -d $dir or mkdir $dir, 0755 or Error "can't mkdir $dir ($!)" ; } sub mk_tag { my $hash = shift ; my $res = $hash -> {stem} ; unless ( $res ) { $res = lc $hash -> {name} ; $res =~ s![/\s]!-!g ; } $res ; } sub get_yaml { my @res = YAML::LoadFile $YAML_FILE ; [ @res ] ; } sub get_prjs { get_yaml ; } sub banners { my $hash = shift ; my $date = $hash -> {retired} ; ( split ' ', $date ) [ 1 ] >= 2018 ; } sub touch { my $file = shift ; my $res = 1 ; if ( open FILE, '>', $file ) { close FILE ; } else { $res = 0 ; } $res ; } sub mk_flags { my $prjs = shift ; my $res = {} ; for my $hash ( sort { $a -> {project} cmp $b -> {project} } @$prjs ) { next if exists $hash -> {subproject} ; next if exists $hash -> {revived} ; my $tag = mk_tag $hash ; my $fdir = sprintf "%s/%s.apache.org", $FLAGS_DIR, $tag ; $res -> { $fdir } ++ ; unless ( -d $fdir ) { printf "$TAG mkdir $fdir\n" unless $opt{q} ; $mods ++ ; if ( $opt{f} ) { mkdir $fdir, 0755 or Error "can't mkdir $fdir ($!)" ; } } if ( banners $hash ) { my $file = "$fdir/$BANNER_FILE" ; $res -> { $file } ++ ; unless ( -f $file ) { printf "$TAG touch $file\n" unless $opt{q} ; $mods ++ ; if ( $opt{f} ) { touch $file or Error "can't touch $file ($!)" ; } } } } $res ; } sub get_have { opendir FDIR, $FLAGS_DIR or Error "can't opendir $FLAGS_DIR ($!)" ; my @fdir = map "$FLAGS_DIR/$_", grep ! /^\./, readdir FDIR ; closedir FDIR ; my $res = {} ; for my $fdir ( @fdir ) { my $file = "$fdir/$BANNER_FILE" ; $res -> { $fdir } ++ ; $res -> { $file } ++ if -f $file ; } $res ; } sub cleanup { my $want = shift ; my $have = get_have ; for my $item ( reverse sort keys %$have ) { if ( $want -> { $item } ) { print "we do want $item\n" if $opt{d} ; } else { print "$TAG remove $item\n" ; $mods ++ ; if ( $opt{f} ) { if ( -d $item ) { rmdir $item or Error "can't rmdir $item ($!)" ; } else { unlink "$item" or Error "can't unlink $item ($!)" ; } } } } } cleanup mk_flags get_prjs ; printf "$prog : no change\n" unless $opt{q} or $mods ;