Merge branch 'master' into topic/upload_fieldbook_images
[sgn.git] / bin / iotw.pl
blob6af67f842518779ef42a8af79c2976c9863331ad
1 #!/usr/bin/perl -w
2 # This script will parse the text files in this directory to determinr/set
3 # the new image of the week.
4 # * It will create/update the simlink img/feature to the current image
5 # * It will put the description for the image into img/feature/desc.txt
6 # It should be run weekly from cron
9 use strict;
10 use SGN::Context;
11 use Image::Size;
13 my $c = SGN::Context->new;
15 BEGIN {
16 $ENV{'PROJECT_NAME'} = "SGN";
19 # debug status (greater for modified operation, more verbose output)
20 my $debug = 0;
22 # file setup
23 my $cfg = $c->config;
24 my $webpath = $cfg->{'static_datasets_url'}."/images/iotw/";
25 my $fullpath = $cfg->{'static_datasets_path'}."/images/iotw/";
26 my $file = $fullpath . "iotw.txt";
27 my $temp = "$file.tmp";
28 my $bak = "$file.orig";
30 $debug and die "The index file $file will be updated.\n";
32 # get today's date
33 my $year = (localtime)[5]+1900;
34 my $month = sprintf("%02d", (localtime)[4]+1 % 100);
35 my $day = sprintf("%02d", (localtime)[3]+1 % 100);
36 my $now = "$year$month$day";
38 # read file of images
39 my $image = ""; # set image line to NULL for now...
40 my $previous = ""; # placeholder for the previous line in a file
41 open(IMAGES, "< $file") or die "Can't open $file file: $!\n";
42 open(TEMP, "> $temp") or die "Can't open $temp file: $!\n";
44 # parse bookkeeping line
45 my $tracking = <IMAGES>;
46 my ($curr, $date) = split(/::/, $tracking);
47 chomp ($curr, $date);
49 $curr++; # increment the index of the picture to display
50 print "Image index updated to $curr, date updated from $date to $now\n";
51 print TEMP "${curr}::${now}\n";
52 while (<IMAGES>) { # go through the file
53 if ($_) { # to make sure we don't get a blank line
54 $previous = $_;
56 if ($. == $curr) { # while we're doing this, set the image
57 $image = $_;
59 print TEMP $_;
62 # close files
63 close(TEMP) or die "Can't close $temp file: $!\n";
64 close(IMAGES) or die "Can't close $file file: $!\n";
66 rename($file, $bak) or die "Can't rename $file to $bak: $!\n";
67 rename($temp, $file) or die "Can't rename $temp to $file: $!\n";
69 # chmod the file so new images can be added without an error
70 my $chmod = `chmod 777 $file`;
71 if (!$chmod) {
72 print $chmod;
75 # we've run out of images, just use the last one in the file for now
76 if (!$image) {
77 print "We've run out of images (Week #", $curr-1, ")- using the most recent one available...\n";
78 $image = $previous;
81 my $text;
82 ($image, $text) = split(/::/, $image);
83 chop($text = $text);
85 my $desired_x = 200;
86 my ($size_x, $size_y) = imgsize($fullpath.$image);
87 my $scaled_y = sprintf( '%0.0f', $size_y * $desired_x / $size_x );
88 my $outdesc = $fullpath . "desc.txt";
89 open DESC, ">$outdesc" or die "Couldn't open $outdesc: $!\n";
90 print DESC <<"EOF";
91 <img width="$desired_x" height="$scaled_y" hspace="0" vspace="0" border="0" src="$webpath$image" alt="IOTW, week $curr">
92 <br />
93 $text
95 EOF
96 close(DESC);