#!/usr/bin/perl -w use strict; use Conf2Hash; use Getopt::Std; my $o={}; getopts('ga:d:l:',$o); sub usage { print STDERR < /var/www/DAMS-Content/USERDIRS.TXT; EXAMPLE 2: to simply regenerate USERDIRS.TXT: # /var/www/DAMS-Content/generate_userdirs.pl -g > /var/www/DAMS-Content/USERDIRS.TXT; EXAMPLE 3: to delete the above entry from USERDIRS.TXT: # /var/www/DAMS-Content/generate_userdirs.pl -d userfiles/sfarrow/incoming -g > /var/www/DAMS-Content/USERDIRS.TXT; NOTES: this file expects to be /var/www/DAMS-Content/generate_userdirs.pl USAGE } (defined $$o{g} or defined $$o{a} or defined $$o{d} ) or (usage() and exit); # are there cli opts? if ($$o{a} and $$o{d}){ print STDERR "ERROR: can't tell me to add AND delete at the same time\n"; usage(); exit; } if ($$o{l} and !$$o{a}){ print STDERR "ERROR: can't just give me a label (-l $$o{l})\n"; usage(); exit; } #load conf my $C = { pwd=>'/var/www/DAMS-Content', HEADER=>'', BODY=>'', count=>1 }; conf2hash( file=>"$$C{pwd}/header.conf", hashref=>$C ); # get the labeled ones, users inside my $U= {}; conf2hash( file=>"$$C{pwd}/labeled.conf", hashref=>$U ); ################################################ # now labeled.conf changes may happen #{{{ if (defined $$o{a}){ $$o{a} or die ("what about content for what we are adding? $!"); -d "$$C{pwd}/$$o{a}" or (print STDERR "ERROR: [$$C{pwd}/$$o{a}] does NOT exist as a dir\n" and usage() and exit); unless (defined $$o{l}){ $$o{l} = label_from_dir($$o{a}); } $$U{$$o{a}}=$$o{l}; hash2conf(file=>"$$C{pwd}/labeled.conf", hashref=> $U); } if (defined $$o{d}){ $$o{d} or (print STDERR "ERROR: what about content for what we are deleting?\n $!\n" and usage() and exit); delete $$U{$$o{d}}; hash2conf(file=>"$$C{pwd}/labeled.conf", hashref=> $U); } #}}} # this is 0 only if cli was -g 0 defined $$o{g} or exit; # get the client dirs my $cdi ="$$C{pwd}/Clients"; my @d = sort (split(/\n/, `find $cdi -type d -name incoming -follow`)); # START BODY #####################################################3 $$C{BODY} = "[Users]\n"; for (keys %$U){ $$C{BODY}.= line($C,$_,$$U{$_}); } for (@d){ my $dir = $_; $dir=~s/^$$C{pwd}\///; $$C{BODY}.= line($C,$dir); } # START HEADER ####################### $$C{HEADER}="[PreferredServer]\n"; $$C{HEADER}.="Server=".$$C{Server}."\n"; $$C{HEADER}.="[RoutingID]\n"; $$C{HEADER}.="NextID=$$C{count}\n"; print $$C{HEADER}; print $$C{BODY}; exit; # usage : line($C,'userfiles/this/incoming','optlabel'); sub line { #{{{ my ($C,$dir,$label)=@_; $C or die($!); $dir or die($!); unless ($label){ $label = label_from_dir($dir); } $dir=~s/\//\\/g; my $line = "$label=$dir,$label,Dyer05,$$C{count},0\n"; $$C{count}++; return $line; } #}}} sub label_from_dir { my $dir = shift; my $label = $dir; $label=~s/\/incoming$//; $label=~s/^.+\/+//; return $label; }