#! /usr/bin/perl # # Public holidays data importer for PVlink # Copyright (c) 2003 by Marcin Woli\'nski # http://www.mimuw.edu.pl/~wolinski/pv/ # # # This simple script allows public holidays data published # at http://www.pcsync.de/download/e_holiday.asp to be imported into # PVlink's sync folder. For the detailed procedure see # http://www.mimuw.edu.pl/~wolinski/pv/ # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # use strict; use vars qw($syncfolder @countries $mode $area $areafolder %holidays $fnum); # Processing of command-line arguments: $area='reminders'; while ($_ = shift) { if (/^-/) { if (/^-s$/) { $area='scheduler'; } elsif (/^-r$/) { $area='reminders'; } else { print "$0: Unsupported option $_.\n"; exit 1; } next; } unless ($syncfolder) { $syncfolder=$_; } else { push @countries, $_; } } # Not enough arguments: unless ($syncfolder && @countries) { print <; die "No holidays data for $country" unless @fnames; die "Multiple matching filenames for $country" if @fnames > 1; my $sdfname = $fnames[0]; die "No holidays data for $country (no file $sdfname)" unless -r $sdfname; open SDF, $sdfname; while () { die "File format error in $sdfname" # note DOS line endings: unless (/^([0-9]{8});;+\"?([^;\(]+) +\((.+)\)\"?\r\n/); my ($date,$holiday,$ccode) = ($1,$2,$3); # print "$holiday IN $ccode ON $date\n"; # This structure is indexed by date and then by holiday name: push @{$holidays{$date}->{$holiday}}, $ccode; } } # Ready for import print "\nWill import public holidays for ". join(', ',@countries). " to the $area area\n(data located at $syncfolder).\n\n"; if ($area eq 'reminders') { $mode = '201000'; } else { $mode = '200000'; } # Now have to determine the folder for items to be imported (specified # in conf/categories): open CONF, "$syncfolder/conf/categories"; while () { # grep the code of the appropriate mode: if (/$mode ([^ ]+) /) { $areafolder = "$syncfolder/$1"; last; } } die "Folder for $mode unknown" unless $areafolder; unless ( -d $areafolder ) { print "Creating folder $areafolder\n\n"; system "mkdir -p $areafolder"; } # This sub finds a safe name for an item: $fnum = 1; sub gennewname { my $fname; $fnum++ while -e ($fname="$areafolder/holid$fnum"); return $fname; } # Import # (That is creation of files in $areafolder). foreach my $date (sort keys %holidays) { my $datef = $date; $datef =~ s|(....)(..)(..)|$1/$2/$3|; # 2003/01/01 open SYNC, ">".gennewname(); print SYNC "900000 900000 $datef\n"; print SYNC "000000 000000 ".(join ", ", map { "$_ (".join(', ',@{$holidays{$date}->{$_}}).")" } sort keys %{$holidays{$date}})."\n"; close SYNC; print "$datef: '"; print join ", ", (map { "$_ (".join(', ',@{$holidays{$date}->{$_}}).")" } sort keys %{$holidays{$date}}); print "'\n"; } print "\nDone.\nNow please synchronize your PV with the folder $syncfolder.\n\n";