-
Notifications
You must be signed in to change notification settings - Fork 1
/
CalManage.pl
executable file
·281 lines (233 loc) · 7.45 KB
/
CalManage.pl
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#!/usr/bin/perl
## Tiny utility script to manage templates. Launch it without arguments to see how "clever" it is.
##
## (C) Vitaly Repin, 2013.
## License: GPLv3
use strict;
use File::Spec;
use File::Basename;
use File::Copy;
use Getopt::Long;
# hash array with configuration
my %cfg;
$cfg{"templdir"} = "templates";
$cfg{"caldir"} = "calendars";
# Logs message to stderr
# Input parameter: message to log
sub _log($) {
print STDERR shift . "\n";
}
# Replacement to the die subroutine. Calls _log subroutine
sub log_die($) {
my $str = shift;
_log $str;
die $str;
}
# Generates symlinks for the given calendar pointing to the given template files
# Arg1: template name
# Arg2: calendar name
sub gen_links($ $) {
my $templ = shift;
my $cal = shift;
my $fname;
_log "Generating symlinks for the calendar $cal from the template $templ";
my $tdirname = File::Spec->catdir( $cfg{"templdir"}, $templ );
my $cdirname = File::Spec->catdir( $cfg{"caldir"}, $cal );
opendir( DIR, $tdirname )
or log_die "can't opendir template dir $tdirname: $!";
# If no ".gitignore" exists, we'll create it and add all the symlinks there
my $gitignore = undef;
my $gitignorename = File::Spec->catfile( $cdirname, ".gitignore" );
if ( !-e $gitignorename ) {
open( $gitignore, '>', $gitignorename )
or log_die "can't open $gitignorename for writing!";
}
while ( defined( $fname = readdir(DIR) ) ) {
next if $fname =~ /^\.\.?$/; # skip . and ..
next
if ( $fname eq "License" )
or ( $fname eq "README" )
or ( $fname eq "GPL3.txt" ); # Skipping Licenses and Readme
# Skipping customization files.
next if ( $fname eq "customization.tex" ) or ( $fname eq "custom.mk" );
symlink(
File::Spec->catfile( "../../", $tdirname, $fname ),
File::Spec->catfile( $cdirname, $fname )
) or log_die "can't make symlink $fname : $!";
print "Added symlink to $fname\n";
if ( defined($gitignore) ) {
print "Adding the file $fname to .gitignore\n";
print $gitignore "$fname\n";
}
}
closedir(DIR);
if ( defined($gitignore) ) { close($gitignore); }
}
# Removes all the symlinks from the given directory
# Arg1: Directory to clean symlinks from
sub clean_links($) {
my $dirname = shift;
my $fname;
opendir( DIR, $dirname ) or log_die "can't opendir .: $!";
print "Cleaning the symbolic links from the directory $dirname\n";
while ( defined( $fname = readdir(DIR) ) ) {
next if $fname =~ /^\.\.?$/; # skip . and ..
my $ffname = File::Spec->catfile( $dirname, $fname );
if ( -l $ffname ) {
print "Unlinking symlink: '$ffname'\n";
unlink $ffname or log_die "Could not unlink $ffname: $!";
}
}
closedir(DIR);
}
# Returns the alphabetically sorted list of 1st layers-subdirs of the given directory.
# Arg1: Directory name
sub get_subdirs($) {
my $parent = shift;
my $fname;
my @dirlist = ();
opendir( DIR, $parent ) or log_die "can't opendir .: $!";
while ( defined( $fname = readdir(DIR) ) ) {
next if $fname =~ /^\.\.?$/; # skip . and ..
my $dirname = File::Spec->catdir( $parent, $fname );
if ( -d $dirname ) {
push( @dirlist, $fname );
}
}
# Sort the list alphabetically
return sort { $a cmp $b } @dirlist;
}
# Prints subdirs of the given directory (only of the 1st layer)
# Arg1: Direcory to show sibfolders for
sub show_subdirs($) {
my $parent = shift;
foreach ( get_subdirs($parent) ) { print "$_\n"; }
}
# Shows the templates defined
sub list_templs() {
print "Defined templates:\n\n";
show_subdirs( $cfg{"templdir"} );
}
# Shows the calendars defined
sub list_cals() {
print "Defined calendars:\n\n";
show_subdirs( $cfg{"caldir"} );
}
# Returns 1 if elements exists in the aaray. 0 otherwise
# Adapted from: http://docstore.mik.ua/orelly/perl/cookbook/ch04_13.htm
# Arg1: Element
# Arg2: Reference to an array
sub elem_exists($ $) {
my $item = shift;
my $arr = shift;
foreach (@$arr) {
if ( $item eq $_ ) {
return 1;
}
}
return 0;
}
# Checks is the template exists
# Arg1: Template name
sub check_template_exists ($) {
my $templ = shift;
my @templs = get_subdirs( $cfg{"templdir"} );
my $tst = elem_exists( $templ, \@templs );
if ( $tst != 1 ) {
print STDERR "Template $templ is not defined!\n";
list_templs();
die "Error processing the command";
}
}
# Synchronizes calendar with a template
# Arg1: Calendar name
# Arg2: Template name
sub sync_cal($ $) {
my $cal = shift;
my $templ = shift;
my @cals = get_subdirs( $cfg{"caldir"} );
# Check that name of the calendar and of the templates are correct
my $tst = elem_exists( $cal, \@cals );
if ( $tst != 1 ) {
print STDERR "Calendar $cal is not defined!\n";
list_cals();
return;
}
check_template_exists($templ);
# It's safe now to do synchronization
clean_links( File::Spec->catdir( $cfg{"caldir"}, $cal ) );
gen_links( $templ, $cal );
}
# Creates new calendar based on a template
# Arg1: Calendar name
# Arg2: Template name
sub new_cal($ $) {
my $cal = shift;
my $templ = shift;
# Making sure template exists
check_template_exists($templ);
# Making sure calendar does NOT exist
my @cals = get_subdirs( $cfg{"caldir"} );
my $tst = elem_exists( $cal, \@cals );
if ( $tst != 0 ) {
print STDERR "Calendar $cal is ALREADY defined! Remove it first!\n";
list_cals();
return;
}
# Now it's safe to create new calendar
my $caldir = File::Spec->catdir( $cfg{"caldir"}, $cal );
print "Creating new calendar directory: $caldir\n";
mkdir $caldir or log_die "can't mkdir $caldir : $!";
gen_links( $templ, $cal );
# Copying the "special" files (customization part of the template)
my @special_files = ( "customization.tex", "custom.mk" );
foreach (@special_files) {
my $dst = File::Spec->catfile( $caldir, $_ );
my $src = File::Spec->catfile( $cfg{"templdir"}, $templ, $_ );
if ( -e $src ) {
print "Copy file $src to $dst\n";
copy( $src, $dst ) or log_die "can't copy file: $!";
}
else {
print STDERR "WARNING: Template does not contain the file $_!\n";
}
}
}
# User's help
sub usage () {
print "Usage: $0 <command> [OPTIONS]\n";
print "<command> is one of:\n";
print " --list-templs\t List defined templates\n";
print " --list-cals\t List defined calendars\n";
print " --sync-cal <CAL_NAME> <TEMPL_NAME>\t Sync calendar <CAL_NAME> with its template <TEMPL_NAME>\n";
print " --new-cal <CAL_NAME> <TEMPL_NANE>\t Make a new calendar <CAL_NAME> based on the template <TEMPL_NAME>\n";
print " --help\t Get this text and exit\n";
}
sub usage_die () {
usage();
die;
}
## Main. Kind of :-)
my ( $help, @sync, @newcal, $list_templs, $list_cals );
GetOptions(
"list-templs" => \$list_templs,
"new-cal=s{2}" => \@newcal,
"list-cals" => \$list_cals,
"sync-cal=s{2}" => \@sync,
"help" => \$help
) or usage_die();
if ($list_templs) {
list_templs();
}
elsif ($list_cals) {
list_cals();
}
elsif (@sync) {
sync_cal( @sync[0], @sync[1] );
}
elsif (@newcal) {
new_cal( @newcal[0], @newcal[1] );
}
else {
usage();
}