updated git and svn scripts
[xrzperl.git] / zim-del-directory
blob02893cd309aa9820e846f7258d28794059fa68a3
1 #!/usr/bin/perl -w
2 ###APPNAME: zim-del-directory
3 ###APPAUTHOR: duel
4 ###APPDATE: 2008-05-11 14:27:04
5 ###APPVER: 0.1
6 ###APPDESC: delete zim directory entry
7 ###APPUSAGE: [--test] (directory) [level]
8 #[entry Name] [entry Description] [destination]
9 ###APPEXAMPLE: zim-del-directory Test .test abc.txt
10 ###APPOPTION: --test:No writing will be made
11 use strict;
13 #ENV variable MUST be defined somewhere,
14 #FOR perl to search modules from,
15 #OR nothing will work
16 use Cwd;
17 use lib $ENV{XR_PERL_MODULE_DIR};
19 use MyPlace::Script::Usage qw/help_required help_even_empty/;
20 exit 0 if(help_even_empty($0,@ARGV));
22 my $test=0;
24 if($ARGV[0] eq "--test") {
25 $test=1;
26 shift;
28 my ($root_d,$level) = @ARGV;
29 $level=32768 unless($level);
31 #my ($edir,$ename,$edesc,$dist) = @ARGV;
32 #my $eloc;
33 sub tee($) {
34 my $str=shift;
35 print STDERR "\t$str";
36 return $str;
39 #unless($ename) {
40 # $ename = $edir;
41 # $ename =~ s/\/$//g;
42 # $ename =~ s/^.*\///g;
45 #unless($eloc) {
46 # $eloc = $ename;
47 ## $eloc =~ s/[ ,\+ ~×]/_/g;
48 # $eloc = ".$eloc";
51 #unless($dist) {
52 # $dist=$ENV{PWD};
53 # $dist =~ s/\/?$/.txt/;
56 sub error_chdir($) {
57 my $dir=shift;
58 warn "Can't change to $dir!\n";
61 sub do_unlink {
62 if($test) {
63 print STDERR "[Test]unlink $_...\n" foreach(@_);
65 else {
66 print STDERR "Unlink $_...\n" foreach(@_);
67 unlink @_ or warn("$!\n");
71 sub deleteDirectory($) {
72 $level--;
73 my $edir=shift;
74 my $ename = $edir;
75 $ename =~ s/\/$//g;
76 $ename =~ s/^.*\///g;
77 my @dirs;
78 my $olddir=cwd();
79 do_unlink "$edir.txt" if(-f "$edir.txt");
80 chdir($edir) or return(error_chdir($edir));
81 do_unlink "$edir.txt" if(-f "$edir.txt");
82 foreach my $file (glob("*")) {
83 next if($file eq ".");
84 next if($file eq "..");
85 if(-d $file && $level>=0) {
86 push @dirs,$file;
89 foreach(@dirs) {
90 do_unlink "$_.txt" if(-f "$_.txt");
92 foreach(@dirs) {
93 &deleteDirectory($_);
95 chdir($olddir) or return(error_chdir($olddir));
98 &deleteDirectory($root_d);