#!/usr/bin/perl
# ----------------------------------------------------------------------------
# This software is Copyright (C) 2007, All Rights Reserved
# ----------------------------------------------------------------------------
# This program is protected by local and international copyright laws. Any
# use of this program is subject to the the terms of the license agreement
# included as part of this distribution archive. Any other uses are strictly
# prohibited without the written permission of the Vendor and all
# other rights are reserved.
# -----------------------------------------------------------------------------
# Warning : Modifying this program violates your license agreement!
# -----------------------------------------------------------------------------

package IT::ArtMan::CustomMenus::Publish::ExternalUrls;

# load modules
use strict;

use IT::Globals;

# ------------------------------------------------------------------------------
# Function    : request
# Description :
# Usage       : &IT::ArtMan::CustomMenus::Publish::ExternalUrls::request($rule, $publishUpdatedOnly, $stepRef, $subStepRef);
# ------------------------------------------------------------------------------

sub request {
  my $rule               = shift || die "no rule specified!\n";
  my $publishUpdatedOnly = shift;
  my $stepRef            = shift || die "no currentStepRef defined!\n";
  my $subStepRef         = shift || die "no currentSubStepRef defined!\n";
  my $publishAll         = !$publishUpdatedOnly;

  # does rule need publishing?
  my $ruleNeedsPublish   = $publishAll || $rule->{'alwaysPublish'};

  # display status message
  if ($ruleNeedsPublish)   { &IT::MultiStepOperations::addStatusMessage("Publishing: $rule->{'name'} ($rule->{'type'})\n"); }
  else                     { &IT::MultiStepOperations::addStatusMessage("No updates for: $rule->{'name'} ($rule->{'type'})\n"); }

  # publish page
  if ($ruleNeedsPublish) {

    ### Request url
    $rule->{'externalUrl'} =~ m|^(?:https?://)?([^/]+)(/.*)?$|i || die "Couldn't parse external url '$rule->{'externalUrl'}'!\n";
    my $hostname = $1;
    my $url      = $2 || "/";
    my $response = &IT::Common::getPage($hostname, $url);
    if ($response =~ m|^HTTP/\d+\.\d+ (.*)$|m) {
      my $serverStatus = $1;
      &IT::MultiStepOperations::addStatusMessage(" - server responded: $serverStatus\n");
    }
    else {
      &IT::MultiStepOperations::addStatusMessage(" - cound't parse server response\n");
    }
  }

  #
  my $isStepComplete = 1;
  return $isStepComplete;
}

# ----------------------------------------------------------------------------
# end of file
1;

