#!/usr/local/bin/perl # # MonthIndex # V1.4 pl 02 - 30 Jan 1999 # # Written by # Jouko Valta (jopi@x2ftp.oulu.fi) # # # List files updated after last month "NEW" list. # Create index of new files during current month. # # TODO: accept given month. # # Revision History # V1.2 pl 00 - 10 Jun 1996 # # V1.2 pl 01 - 26 Jun 1996 # Check and report file permissions # # V1.2 pl 02 - 18 Jul 1996 # Skip "rejected." files # # V1.2 pl 03 - 2 Oct 1996 # Read 00index for the 1st file on a new directory if not read already. # # V1.3 pl 00 - 12 Oct 1996 # Command line options; consistency check. # # V1.3 pl 01 - 7 Apr 1997 # Scan-rules against subdirectories. # # V1.4 pl 00 - 11 Jun 1997 # Testing options added. # # V1.4 pl 01 - 14 Aug 1997 # Move previous 00*.new index to $STATUSDIR automatically. # 'Strict find' (based on $Mday) option added. # # V1.4 pl 02 - 30 Jan 1999 # Index file consistency chacking improved. # # $Usage = "\ Options:\ -h Show help page and exit\ -c Check index file Consistency\ -f AutoCorrect\ -p Check file Permissions\ -s Use Strict Find based on month day number\ -t,-nw Test only. Don't update index file\ -w file Specify index file to write\ -nc Suppress Consistency checking\ -nf No AutoCorrect\ -np No Permission checking\n\n"; # ----------------------------------------------------------------------------- ## Determine some time constants @months = ( "January", "February", "March", "April", "May", "June", "July", "August", "September","October", "November", "December" ); @mons = ( "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ); # Get Date ($sec,$min,$hour, $Mday,$Mon,$Year, $wday,$yday,$isdst) = localtime; $YEAR = `/bin/date +%Y`; # Copyright year (in 4 digits) chop($YEAR); ## Default Filenames $RECENT = "00recent.txt"; $MASTER_INDEX = "00index.txt"; $STATDIR = "x2info"; $OLDINDEX = `/bin/ls -1tr $STATDIR/00*.new | tail -1`; chop ($OLDINDEX); # 00 + Abbreviated month name jan to dec + year + '.new' $TARGET = "00" . $mons[$Mon] . $Year . ".new"; $FINDSTRICT= "/bin/find \. -type f -mtime -$Mday -print | /bin/cut -c3-"; $FINDNEW = "/bin/find \. -newer $OLDINDEX -print | /bin/cut -c3-"; $SORT = "/bin/sort"; $FTPBIN = "/ftp-service/bin"; $INDEXUPDATE = "$FTPBIN/update_mainhtml"; # Updates the programming/ page. ## Defaults $TestMode = 0; # Test only. Don't write outfile. $Consistency = 1; $Permission = 1; $AutoCorrect = 0; # Fix file permissions. Unused at the moment. $StrictFind = 0; # Find $Mday old or newer files. $monthchange = 0; ## Command line options while ($_ = $ARGV[0], /^-/) { shift; last if /^--$/; if (/^-h/) { print $Usage; exit 2; } if (/^-t|^-nw/) { $TestMode = 1; next; } if (/^-c/) { $Consistency = 1; next; } if (/^-f/) { $AutoCorrect = 1; next; } if (/^-p/) { $Permission = 1; next; } if (/^-s/) { $StrictFind = 1; $FINDNEW = $FINDSTRICT; next; } if (/^-w/) { $TARGET = $ARGV[0]; next; } if (/^-nc/) { $Consistency = 0; next; } if (/^-nf/) { $AutoCorrect = 0; next; } if (/^-np/) { $Permission = 0; next; } if (/^-/) { die "\nUnsupported argument near $_\n Stopped"; } } print "\n"; # ----------------------------------------------------------------------------- # # Check if target file changed (i.e. entered new month). if (! -e $TARGET) { $old = `/bin/ls -1tr 00*.new | tail -1`; chop ($old); # Make sure $old is not the current month's index file. if (! grep (/$mons[$Mon]/, $old)) { printf STDERR "WARNING: Previous *new file '$old' is rather old.\n"; if ($old && ! -f "$STATDIR/$old") { if (! $TestMode) { printf STDERR "WARNING: Moving '$old' to '$STATDIR'.\n"; rename ($old, "$STATDIR/$old"); # Move previous *new file. # Update Index file. # "00jul97.new Material added during July 1997" open (I, ">>$STATDIR/00index.txt"); printf I "%s\tMaterial added during %s %d\n", $TARGET, $months[$Mon], $YEAR; close(I); $OLDINDEX = "$STATDIR/$old"; ++$monthchange; # Flag moving index files. } } # old } # new month } # new target file # ----------------------------------------------------------------------------- # # Create Month Index file # if ($TestMode) { $TARGET = "/dev/null"; } (-f $OLDINDEX) || die "Panic: File $OLDINDEX not found."; print "\nfound $OLDINDEX\n"; print "Writing to $TARGET\n\n"; # Get the directory descriptions ... open (M, "<$MASTER_INDEX"); print "Reading Directory list (25 sec)\n"; # (Actually, it's find + sort) while () { if (/^\s*$/) { next; } elsif (/^Index of all/) { next; } elsif (/\s*(\S+)\/(.*)/) { # Real directory declaration. $DIRSPEC{$1} = $2; # print "\t$1:$DIRSPEC{$1}\n"; # any TABs inclusive } } close (M); open (foo, "$FINDNEW | $SORT |") || die "Can't excute '$FINDNEW | $SORT'"; open (OF, ">$TARGET"); ## Title # Use 4-digit year. print OF "New files in x2ftp.oulu.fi:/pub/msdos/programming in $months[$Mon] $YEAR\n"; $newdir = 0; line: while () { if (/^\s*$/) { next line; } # Empty line if (/^00|\/00|00\_*index|index.html/) { next line; } # Index files if (/rejected\.\d+/) { next line; } # Info file (constantly edited) if (/contest95/) { next line; } # Nothing new elsif (/([A-Za-z0-9_\-\.]+)\/(\S+)/) { # File in any FTP directory $tmp = $1; $file = $2; # May contain subdir ... # Directory was not updated at the same time as the file (touch file). if ($tmp ne $dir) { $dir = $tmp; read_00index(); ++$newdir; } if ($newdir) { printf OF ("\n$dir/$DIRSPEC{$dir}\n"); $newdir = 0; } if ($FILESPEC{$file}) { printf OF ("$FILESPEC{$file}\n"); } else { printf "*** No description for '$dir/$file'. ***\n"; } next line; } # This rule only handles directories where no new files exist. elsif (/^(\S+)$/) { # FTP Directory $dir = $1; read_00index(); ++$newdir; next line; } } printf OF "\n"; close OF; chmod 0664, $TARGET; # system ("rm -f $RECENT"); # system ("ln -s $TARGET $RECENT"); # # Update the main HTML index pages if month index file was changed. # if ($monthchange) { system("$INDEXUPDATE"); } # ----------------------------------------------------------------------------- # Check file permissions if ($Permission) { print "\n\nChecking file permissions\n"; system ("find . ! -perm -0664"); system ("find . -perm -0002"); print "done.\n"; } # Permission # ----------------------------------------------------------------------------- # # Find matching file for each index file entry. # if ($Consistency) { print "\n\nChecking index file consistency ...\n"; $FileCnt = 0; $prevdir = ""; foreach $dir (sort {$a cmp $b} (keys %DIRSPEC)) { # printf "%s\t%s\n", $dir, $DIRSPEC{$dir}; # Try all ascii index file names open (M, "$dir/00index.txt") || open (M, "$dir/00_index.txt") || open (M, "$dir/00index") || open (M, "$dir/00INDEX") || printf STDERR "Cannot read index file for '$dir'\n"; while () { if (/\s*(\S+)(.*)/) { if (! -e "$dir/$1") { if ("$dir" ne "$prevdir") { printf "\n$dir:\n$_"; $prevdir = $dir; } else { printf "$_"; # Line has the LF. } ++$FileCnt; } } } # while close (M); } # for printf "\nFound %d stray index entries.", $FileCnt; } # Consistency print "\nChecking completed.\n"; # ----------------------------------------------------------------------------- sub read_00index { $FileCnt = 0; # Try all ascii index file names if (!open (M, "$dir/00index.txt") && !open (M, "$dir/00_index.txt") && !open (M, "$dir/00index") && !open (M, "$dir/00INDEX") ) { printf STDERR "Cannot read index file for '$dir'\n"; return; } while () { chop; if (/\s*(\S+)(.*)/) { $FILESPEC{$1} = $_; # Any TABs inclusive ++$FileCnt; } } close (M); print "Read file specs for '$dir'\t($FileCnt files)\n"; }