forked from yorkulibraries/branchhours
-
Notifications
You must be signed in to change notification settings - Fork 1
/
branchhourslink
executable file
·64 lines (54 loc) · 1.83 KB
/
branchhourslink
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
#!/usr/bin/ruby -w
# branchhourslink
#
# Make symlink so you can always include the same file in a web page
# and not have to figure out the date
#
# William Denton <[email protected]>
# http://github.com/wdenton/branchhours/
require 'optparse'
require 'time'
require 'yaml'
options = {}
options[:config] = 'branchhours.yml'
options[:date] = 'today'
options[:verbose] = false
OptionParser.new do |opts|
opts.banner = "Usage: branchhourslink [options]"
opts.on("-c", "--config configFile", "specify configuration file") do |configFile|
options[:config] = configFile
end
opts.on("-d", "--date date", "default day (today, tomorrow, YYYYMMDD)") do |date|
options[:date] = date
end
opts.on("-v", "--verbose", "be verbose") { options[:verbose] = true }
end.parse!
begin
config = YAML.load_file(options[:config])
rescue Exception => e
puts e
exit 1
end
if options[:date] =~ /today/i
yyyymmdd = Time.now.strftime("%Y%m%d")
elsif options[:date] =~ /tomorrow/i
yyyymmdd = (Time.now + 24*60*60).strftime("%Y%m%d")
else
yyyymmdd = options[:date]
end
defaultFilePath = config['outputDir'] + "/" + config['defaultFile']
targetFilePath = config['outputDir'] + "/" + yyyymmdd + ".html"
mdefaultFilePath = config['outputDir'] + "/m-" + config['defaultFile']
mtargetFilePath = config['outputDir'] + "/m-" + yyyymmdd + ".html"
STDERR.puts "#{defaultFilePath} -> #{targetFilePath}" if options[:verbose]
# Finally, make the symlink so we can always
begin
File.delete(defaultFilePath) if File.exist?(defaultFilePath)
File.symlink(targetFilePath, defaultFilePath)
File.delete(mdefaultFilePath) if File.exist?(mdefaultFilePath)
File.symlink(mtargetFilePath, mdefaultFilePath)
rescue Exception => e
STDERR.puts "Could not create symlink to today's hours: #{e}"
STDERR.puts "Hours information may be incorrect on your web site"
exit 1
end