updated git and svn scripts
[xrzperl.git] / tagfs
blob23761918bedc79be65ea40e76fbe3fdf58c33cc7
1 #!/usr/bin/perl -w
2 ###APPNAME: tagfs
3 ###APPAUTHOR: duel
4 ###APPDATE: Thu Apr 29 17:38:29 2010
5 ###APPVER: 0.1
6 ###APPDESC: tagfs
7 ###APPUSAGE:
8 ###APPEXAMPLE: tagfs
9 ###APPOPTION: [-r (root_directory)] tag1,tag2,tag3,... files...
10 use strict;
12 #ENV variable MUST be defined somewhere,
13 #FOR perl to search modules from,
14 #OR nothing will work
15 use lib $ENV{XR_PERL_MODULE_DIR};
17 use MyPlace::Script::Usage qw/help_required help_even_empty/;
18 #exit 0 if(help_required($0,@ARGV));
19 exit 0 if(help_even_empty($0,@ARGV));
20 my $REALTAG='all';
21 my $root;
22 my $tags;
23 my @tags;
24 my @files;
25 my @SAVE_ARGV = @ARGV;
27 $root=shift;
28 if($root eq '-r') {
29 $root=shift;
30 $root = "$root/" unless($root =~ /\/$/);
32 else {
33 unshift @ARGV,$root;
34 $root="";
37 ($tags,@files)=@ARGV;
38 die(help_required($0,"-h")) unless(@files);
40 sub get_basename {
41 my $fullname = shift;
42 return unless($fullname);
43 $fullname =~ s/\/+$//;
44 $fullname =~ s/^.*\///;
45 return $fullname;
49 if($root and (! -d $root)) {
50 mkdir($root) or die("$!\n");
52 my $saved_in = "$root$REALTAG";
53 if(! -d $saved_in) {
54 mkdir($saved_in) or die("$!\n");
57 @tags = split(/\s*,\s*/,$tags);
58 die("Empty tag list!\n") unless(@tags);
60 my $log_files=$root . "tagfs.logs";
61 open FO,">>$log_files";
63 sub log_text {
64 print FO @_;
65 print STDERR @_;
69 &log_text('[' . scalar(localtime) . "] tagfs " . join(" ",@SAVE_ARGV) . "\n");
71 sub system_execute {
72 print STDERR "-- ",join(" ",@_),"\n";
73 return (system(@_) == 0);
74 open FI,"-|",@_ or return undef;
75 while(<FI>)
77 chomp;
78 next unless $_;
79 print FO "\t\t\t",$_,"\n";
80 print STDERR "\t\t\t",$_,"\n";
82 return 1;
84 sub tag_files {
85 foreach my $file (@_) {
86 my $basename = get_basename($file);
87 next if($basename eq '.' or $basename eq '..');
88 &log_text("\t[" . join(',',@tags) . '] ' . $file . "\n");
89 if(! -e "$saved_in/$basename") {
90 system_execute("mv","-v",$file,$saved_in);
92 foreach my $tag (@tags) {
93 if(! -d "$root$tag") {
94 mkdir "$root$tag" or die("$!\n");
96 system_execute("ln","-vfs","-t","$root$tag","../$REALTAG/$basename");
101 use File::Glob qw/bsd_glob/;
102 foreach (@files) {
103 if(-e $_) {
104 tag_files($_);
106 else {
107 tag_files(bsd_glob($_));
110 close FO;