#!/usr/local/bin/perl -w #Copyright to John Pickering, Athens, Georgia - 20010906 # HEADER VALUES my $program = "add_xml"; my $version = "1.01"; my $basedir = "/home/web/dl/nh/id"; my $guide=""; if ($ARGV[1]) { print qq|\a\n\nSorry -- too many arguments.\nCommand: ${program} guide_name\nCommand: ${program} guide_name\n\n|; exit 1; } elsif ($ARGV[0]) { $guide = $ARGV[0]; &main; } else { print qq|\n\n\a${program} -- adds new xml in ${basedir}/tmp/20q/xml/\$guide.xml to permanent ${basedir}/20q/\$guide.xml, |; print qq|where \$guide is argument.\nCommand: ${program} guide_name\n\n|; exit 1; } ##################################################################################### ##################################################################################### sub main { #Calculates time variables my $now = localtime(time); my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime(time); my $when_gmt = sprintf "%d%02d%02d%02d%02d%02d", ${year} + 1900, ${mon} + 1, ${mday}, ${hour}, ${min}, ${sec}; #print "when_gmt = ${when_gmt}\n"; #Set process id # $pid = $$; #Sets present working directory # my $pwd = "$ENV{PWD}"; # #print qq|\tpwd = ${pwd}\n|; # my $path = $pwd; $path =~ s/(.+)\/(.+)/$1/; # my $taxon = $2; # #print qq|\tpath=${path}\ttaxon=${taxon}\n|; #Makes OLD directory unless ( -e "${basedir}/20q/OLD" ) { mkdir "${basedir}/20q/OLD", 0777; chmod 0775, "${basedir}/20q/OLD"; } unless ( -d "${basedir}/20q/OLD" && -x "${basedir}/20q/OLD" ) { print "\n\n\aWARNING: Check permissions. Couldn't write to directory=OLD\n\n"; exit 1; } #Checks if new xml code exists unless ( -f "${basedir}/tmp/20q/xml/${guide}" ) { print qq|\a\n\n\tWARNING: No new xml code in ${basedir}/id/tmp/20q/xml for guide=${guide}. Did nothing.\n\n|; exit 1; } #Checks if $guide.xml exists if ( -e "${basedir}/20q/${guide}.xml" ) { #Checks whether writable unless ( -w "${basedir}/20q/${guide}.xml" ) { print qq|\a\n\n\tWARNING: ${basedir}/20q/${guide}.xml not writable. Check permissions. Did nothing.\n\n|; exit 1; } } #Backup 20q/$guide.xml $infile = "${basedir}/20q/${guide}.xml"; if ( -f $infile && -r $infile ) { $outfile = "${basedir}/20q/OLD/${guide}.${when_gmt}"; print "\tSource: ${infile}\tTarget: ${outfile}\n"; system( `cp -p $infile $outfile` ) || print "WARNING: failed to copy file=${infile} to ${outfile}\n"; chmod (0664, "${outfile}"); } else { print "\n\tWARNING: failed to copy ${basedir}/20q/${guide}.xml because it is not a plain, readable file. Check permissions.\n\n"; exit 1; } #Backup tmp/20q/xml/$guide $infile = "${basedir}/tmp/20q/xml/${guide}"; if ( -f $infile && -r $infile ) { $outfile = "${basedir}/20q/OLD/${guide}.${when_gmt}\+"; print "\tSource: ${infile}\tTarget: ${outfile}\n"; system( `cp -p $infile $outfile` ) || print "WARNING: failed to copy file=${infile} to ${outfile}\n"; chmod (0664, "${outfile}"); } else { print "\n\tWARNING: failed to copy ${basedir}/tmp/xml/20q/${guide} because it is not a plain, readable file. Check permissions.\n\n"; exit 1; } #Appends new xml to $guide.xml open (GUIDE, ">>${basedir}/20q/${guide}.xml"); open (NEW, "${basedir}/tmp/20q/xml/${guide}"); while () { print GUIDE qq|$_|; #print qq|$_|; } close NEW; close GUIDE; chmod (0664, "${basedir}/20q/${guide}.xml"); unlink("${basedir}/tmp/20q/xml/${guide}"); print qq|\tAdded: ${basedir}/tmp/20q/xml/${guide}\tto ${basedir}/tmp/20q/xml/${guide}\n|; } ##################################################################################### #####################################################################################