#!/usr/bin/perl

# Copyright (c) 2006, Rob Bloom
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ROB BLOOM ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ROB BLOOM BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

$deffont = "roman";  # Default font

unless ($ENV{"font"}) {
  $font=$deffont;
} else {
  $font=$ENV{"font"};
}

use Curses;
initscr;
start_color;
noecho;
curs_set(0);
init_pair(1,COLOR_WHITE, COLOR_BLUE);
init_pair(2,COLOR_BLUE, COLOR_BLACK);
#attrs
$overlay = COLOR_PAIR(1) | A_BOLD;
$clock = COLOR_PAIR(2) | A_BOLD;

while (TRUE) {

  # First, let's get the basic strings we work with in.
  
  #Calling external programs...
  $date = `date +"%m-%d" | /usr/local/bin/figlet -w $COLS -f $font` . "\n";
  $time = `date +"%H:%M.%S" | figlet -w $COLS -f $font`;

  # Now, Centering date and time...
  # Determine the longest string (will be centered)
  # Then, all other lines can use the same X offset

  #First, split strings along newlines, so we can get the length.
  @date = split /\n/,$date;
  @time = split /\n/,$time;

  #Now, get length of each line, in both date and time.
  $datelen = 0; $timelen = 0;
  foreach (@date) {
    $datelen = length if (length > $datelen);
  }
  foreach (@time) {
    $timelen = length if (length > $timelen);
  }

  #Calculate number of spaces used for date/time
  $datespc = ($COLS / 2)-($datelen / 2);
  $timespc = ($COLS / 2)-($timelen / 2);

  #Now calculates number of spaces for vertically
  $vwidth = $#date + $#time + 1; # lines used (one space in middle)
  $vspc = ($LINES / 2)-($vwidth / 2);

  # Display!!!

  # Work with a clean slate.
  $step++;
  if ($step >= 60) {
    clear; # Once a minute, wipe any garbage off the clock.
	$step = 0;
  } else {
    erase;
  }

  #Show date/time, with spaces before each line, to center.
  attrset($clock);
  $i = 0;
  foreach (@date) {
	addstr($vspc + $i, $datespc,  $_);
    $i++;
  }
  $i++;
  foreach (@time) {
	addstr($vspc + $i, $timespc, $_);
	$i++;
  }

  #An overlay
  #Commented out because it depends on my code to update files with weather
  #status.
  #
  #attrset($overlay);

  #open INP, "/usr/local/share/currentweather/temperature";
  #$t = <INP>;
  #chomp $t;
  #close INP;
  #$t =~ s/F.*$/F/;
  #$t = " Temperature: $t ";
  #addstr(0,0, $t);

  #open INP, "/usr/local/share/currentweather/moonphase";
  #$m = <INP>;
  #close INP;
  #chomp $m;
  #$m = " Moon: $m% ";
  #$numspace = length($t) - length($m);
  #$spc = "";
  #foreach (1 .. $numspace) {
#	$spc .= " ";
#  }
#  addstr(1,0, $m . $spc);
  
  #Now, display.
  refresh;

  #Nappy time!
  sleep(1);
}
