#!/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::App::Install;

  # load modules
  use strict;

  use IT::Globals;

# ---------------------------------------------------------------------------------
# Function    : getCgiFolderUrl
# Description :
# Usage       : my $cgiFolderUrl = &IT::App::Install::getCgiFolderUrl();
# ---------------------------------------------------------------------------------

sub getCgiFolderUrl {

  # get cgidir url
  my $cgiDirUrl = $CGIURL;
  $cgiDirUrl   =~ s|[^\\\/]+$||;  # remove filename

  # add trailing slash
  if ($cgiDirUrl !~ m|/$|) { $cgiDirUrl .= "/"; }

  return $cgiDirUrl;
}

# ---------------------------------------------------------------------------------
# Function    : guessWebsiteRootPath
# Description : return the directory where a website is rooted (eg: C:/www/htdocs/)
# Usage       : my $DOCUMENT_ROOT = &IT::App::Install::guessWebsiteRootPath();
# ---------------------------------------------------------------------------------

sub guessWebsiteRootPath {
  my $websiteRootPath;

  # get document root
  if ($ENV{'DOCUMENT_ROOT'}) {
    $websiteRootPath = $ENV{'DOCUMENT_ROOT'};
  }
  else {
    my $scriptFilepath = $ENV{'SCRIPT_FILENAME'} || $ENV{'PATH_TRANSLATED'} || $0;   # eg: D:\websites\mysite.com\htdocs\cgi\script.cgi
    my $scriptWebpath  = $ENV{'SCRIPT_NAME'} || $ENV{'PATH_INFO'};                   # eg: /cgi/script.cgi

    foreach ($scriptFilepath, $scriptWebpath) { s|\\|/|gs; } # replace backslash with forward slash
    if ($scriptFilepath =~ /^(.*)\Q$scriptWebpath\E$/) {
      $websiteRootPath = $1;                        # eg: D:\websites\mysite.com\htdocs
    }
  }

  # add trailing slash
  if ($websiteRootPath !~ m|/$|) { $websiteRootPath .= "/"; }

  return $websiteRootPath;
}


# ---------------------------------------------------------------------------------
# Function    : guessWebFolderUrl
# Description : return the url of the programs web folder (not website document root!)
# Usage       : my $webFolderUrl = &IT::App::Install::guessWebFolderUrl();
# ---------------------------------------------------------------------------------

sub guessWebFolderUrl {

  # guess webdir url
  my $cgiDirUrl = &getCgiFolderUrl();
  my $webDirUrl = $cgiDirUrl;
  $webDirUrl =~ s|cgi-bin/?||gi;                  # remove cgi-bin/
  if ($webDirUrl !~ m|/$|) { $webDirUrl .= "/"; } # add trailing slash

  return $webDirUrl;
}

# ---------------------------------------------------------------------------------
# Function    : guessWebFolderDir
# Description :
# Usage       : my $webFolderDir = &IT::App::Install::guessWebFolderDir();
# ---------------------------------------------------------------------------------

sub guessWebFolderDir {
  my $webFolderDir = '';

  # get program dir name (eg: programName)
  my $programDirName = $CGIDIR;
  $programDirName   =~ s|[\\\/]+$||;    # remove trailing slash
  $programDirName   =~ s|^.+[\\\/]||;   # remove all but last dir name

  # get path from cgi-bin (eg: apps/new/programName)
  my $pathFromCgiBin;
  if ($CGIDIR =~ /cgi-bin/i) {
    $pathFromCgiBin = $CGIDIR;
    $pathFromCgiBin =~ s|/+$||;      # remove trailing slash
    $pathFromCgiBin =~ s|^.+/cgi-bin/||i;  # remove everything before (and including) the cgi-bin
  }

  # get cgi-bin parent dir (eg: /website/websiteName.com)
  my $cgiBinParentDir;
  if ($CGIDIR =~ /cgi-bin/i) {
    $cgiBinParentDir = $CGIDIR;
    $cgiBinParentDir =~ s|/cgi-bin/.+$||i;  # remove everything after (and including) the cgi-bin
  }


  ### create list of paths to check
  my %pathsToCheck;
  my @baseDirs             = ($CGIDIR, $cgiBinParentDir);
  my @parentReferences     = ('','../','../../','../../../');
  my @commonWebRootDirs    = ('','htdocs','httpdocs','httpd','www','web','html', 'webroot');  # blank webroot name is when cgi is installed under webroot and not an aliased dir
  my @relativeProgramPaths = ('',$pathFromCgiBin, $programDirName);

  foreach my $baseDir (@baseDirs) {
    foreach my $parentRef (@parentReferences) {
      foreach my $webRoot (@commonWebRootDirs) {
        foreach my $programPath (@relativeProgramPaths) {
          my $path = "$baseDir/$parentRef/$webRoot/$programPath";
          $pathsToCheck{$path} = 1;
        }
      }
    }
  }

  ### remove duplicates and clean paths
  my %cleanedPaths;
  foreach my $path (keys %pathsToCheck) {
    my $cleanPath = &IT::File::cleanPath($path);
    $cleanedPaths{$cleanPath} = 1;
  }
  %pathsToCheck = %cleanedPaths;


  ### Check paths
  foreach my $path (sort keys %pathsToCheck) {
    if (-d "$path/javascript/") { $webFolderDir = $path; }
  }

  # clean path
  if ($webFolderDir) {
    $webFolderDir = &IT::File::cleanPath($webFolderDir);
    $webFolderDir .= "/";                       # add trailing slash
  }

  return $webFolderDir
}

# ---------------------------------------------------------------------------------
# Function    : findSendmailPaths
# Description :
# Usage       : @sendmailPaths = &IT::App::Install::findSendmailPaths();
# ---------------------------------------------------------------------------------

sub findSendmailPaths {

  my @commonPaths = ('/usr/lib/sendmail',
                     '/usr/bin/sendmail',
                     '/usr/sbin/sendmail',
                     '/usr/local/bin/sendmail',
                     '/sbin/sendmail',
                     '/bin/sendmail',
                     '/etc/sendmail');


  my @sendmailPaths;
  foreach my $path (@commonPaths) {
    if (-e $path) { push @sendmailPaths, $path; }
  }

  return @sendmailPaths;
}


# ---------------------------------------------------------------------------------
# Function    : guessSmtpHostname
# Description :
# Usage       : my $smtpHostname = &IT::App::Install::guessSmtpHostname();
# ---------------------------------------------------------------------------------

sub guessSmtpHostname {

  # get current domain
  my $currentDomain           = $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'};
  my $currentDomainWithoutWWW = $currentDomain;
  $currentDomainWithoutWWW    =~ s/^\www\.//i;

  # get domains to try
  my @domainsToTry;
  unshift @domainsToTry, $currentDomain;
  unshift @domainsToTry, "mail.$currentDomainWithoutWWW";
  unshift @domainsToTry, "smtp.$currentDomainWithoutWWW";

  # check domains
  foreach my $domain (@domainsToTry) {
    my $isValid = gethostbyname($domain);
    if ($isValid) { return $domain; }
  }

  return;
}

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

