#!/usr/bin/env perl
# Copyright (C) 2004 by İsmail Dönmez
# Licensed under GPL v2 or later at your option

$PORT = shift;
$SERVER = shift;
$TARGET = shift;
$OPTION = shift;

@stations = `dcop KWeatherService WeatherService listStations`;

if( ! @stations ) {
  exec ("dcop $PORT Konversation error \"KWeather is not installed or not running!\" ");
} 

if( $OPTION && $stations[$OPTION-1] ) {
  @stations = $stations[$OPTION-1];
}

foreach $station (@stations) {

  $city = `dcop KWeatherService WeatherService stationName $station`;
  $temperature = `dcop KWeatherService WeatherService temperature $station`;
  $pressure = `dcop KWeatherService WeatherService pressure $station`; 
  $wind = `dcop KWeatherService WeatherService wind $station`;
  $detail = `dcop KWeatherService WeatherService weather $station`;
  $detail2 = `dcop KWeatherService WeatherService cover $station`;

  chomp $detail;
  $details = join ", ", split(/\n/,$detail);
  
  chomp $detail2;
  $details2 = join ", ", split(/\n/,$detail2);

  
  $pressure =~ s/\"/\\\"/g; # Escape double quote

  chomp $city;
  chomp $temperature;
  chomp $pressure;
  chomp $wind;

  if ( $details2 =~ /(\S)+/ ) {
      if( $details =~ /(\S)+/ ) {
	  $details = $details.", ".$details2;
      }
      else {
	  $details = $details2;
      }
  }
      


  if( $details =~ /(\S)+/ ) {
  $MESSAGE = "Current Weather for %B$city%B : %B$details%B, Temperature: %B$temperature%B, Pressure: %B$pressure%B, Wind: %B$wind%B";
  }
  else {
  $MESSAGE = "Current Weather for %B$city%B : Temperature: %B$temperature%B, Pressure: %B$pressure%B, Wind: %B$wind%B";
  }
  
  system ("dcop $PORT Konversation say $SERVER \"$TARGET\" \"$MESSAGE\" ");
  
}
