-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkdir_heute
More file actions
executable file
·183 lines (156 loc) · 4.88 KB
/
mkdir_heute
File metadata and controls
executable file
·183 lines (156 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/perl -w
# vim: si sw=4 ts=4 et tw=78:
#
# mkdir_heute - choose or make a dir for 'heute' (today)
#
# This script scans a basedir (~/archiv) for directories named YYYY/MM/DD
# where YYYY, MM and DD are numbers corresponding to a year, month, day of
# month. You may choose a directory from the list or a new directory which
# will be created named after the current day.
#
# The script returns the choosen directory on STDOUT and may be used in a
# shell alias like this:
#
# alias cdheute='cd `mkdir_heute`'
#
# so that you may say 'cdheute' on the command line and your working directory
# will be changed to the choosen directory.
#
use strict;
$|++;
my $basedir = "$ENV{HOME}/archiv";
my $listLines = 20;
my ($day,$month,$year) = (localtime)[3,4,5];
$day = sprintf("%02d",$day);
$month = sprintf("%02d",$month+1);
$year += 1900;
my $daydir = "$year/$month/$day";
my $dirprefix = "$basedir/$daydir";
$listLines -= 2; # reserve one line for '+' and 'q'
print chooseDir($basedir,$daydir);
sub makeDir {
my $path = shift;
my $dir = '';
$path =~ s{^(.*[^/])$}{$1/}; # provide a sentinel
while ($path =~ s{^([^/]*)/}{}) {
if ($1) {
$dir .= $1;
(-d $dir) || mkdir($dir,0777) || return 0;
$dir .= '/';
}
else {
$dir = '/' unless ($dir);
}
}
return 1;
} # makeDir()
sub chooseDir {
my ($basedir,$daydir) = @_;
my ($base,$prefix,$relbase);
my @dirs = findDirs( $basedir, $listLines );
my @projects = findProjects( $basedir, @dirs);
# let the user choose a directory
my $suffix = '';
while ( 1 ) {
my $project_text = '';
for (my $i = 1; $i <= $#dirs; $i++) {
my $project = $projects[$i] || '';
printf STDERR "%-7s: %-12s: %s\n", $i, $dirs[$i], $project;
}
print STDERR "+(plus): neues Verzeichnis\n";
print STDERR "q : aktuelles Verzeichnis\n";
$suffix = <STDIN>;
chomp $suffix;
if ($suffix =~ /^(\.|q(uit)?)$/i) {
return '.';
}
elsif ($suffix =~ /^\+(.*)$/) {
if ($1) {
$project_text = $1;
$project_text =~ s/^\s+//;
$project_text =~ s/\s+$//;
}
$suffix = q();
if (-d $dirprefix) {
$suffix = 'a';
while (-d qq($dirprefix$suffix)) {
$suffix++;
}
}
if (makeDir($dirprefix . $suffix)) {
if ($project_text
and open(my $PROJ,'>',qq($dirprefix$suffix/.project))) {
print $PROJ qq($project_text\n);
}
return $dirprefix . $suffix;
}
else {
return '.';
}
}
elsif ( $suffix =~ /^\d+$/ ) {
return qq($basedir/$dirs[$suffix]) if ( $dirs[$suffix] );
}
}
} # chooseDir()
sub findDirs {
my ($basedir,$listLines) = @_;
my @dirs = ();
my $dirNum = 1;
# find the last $list_lines daily directories under $BASEDIR
if ( my @years = getDirList( $basedir, qr(^\d{4}$) ) ) {
YEAR:
for my $year (reverse sort @years) {
my @months = getDirList( qq($basedir/$year)
, qr(^\d{2}$)
);
next YEAR unless @months;
MONTH:
for my $month (reverse sort @months) {
my @days = getDirList( qq($basedir/$year/$month)
, qr(^\d{2})
);
next MONTH unless @days;
# iterate over all days of this month backward
DAY:
for my $day (reverse sort @days) {
my $path = qq($year/$month/$day);
$dirs[$dirNum] = $path;
$dirNum++;
# no need to search for more directories
last YEAR if ($dirNum > $listLines);
}
}
}
}
@dirs;
} # findDirs()
sub findProjects {
my ($basedir,@dirs) = @_;
my @projects = ();
my $dirNum = 0;
for my $path ( @dirs ) {
if ($path and -f qq($basedir/$path/.project)
and open my $PROJECT, '<', qq($basedir/$path/.project)) {
my $project = <$PROJECT>;
close $PROJECT;
chomp $project;
$projects[$dirNum] = $project;
}
$dirNum++;
}
return @projects;
} # findProjects()
sub getDirList {
my ($basedir,$regex) = @_;
my @dirList = ();
if (opendir my $BASEDIR, $basedir) {
while (defined (my $dir = readdir($BASEDIR))) {
if ($dir =~ m/$regex/) {
push @dirList, $dir;
}
}
closedir $BASEDIR;
}
return @dirList;
} # getDirHash()