#!/bin/sh
# Plesk Server Administrator net-installer
# 
# Author: Scott R. Shinn <scott@plesk.com>
#
# TODO
# Detect psa STD install
# Test for psa RPM version
# Install BU utils
# Install CU utils
# Conversion from STD to RPM support

# ChangeLog
# 1.0.6 - backported the PSA 6.0 installer routines into this one
# 1.0.5 - updated from rc to rug
# 1.0.4 - updated rcd to use wildcards
# 1.0.3 - updated to rcd-1.4.2-0.ximian.3.2.i386.rpm
# 1.0.2 - updated to rcd-1.2.1-1.ximian.3.i386.rpm

INSTALLER=1.0.6

clear
echo "This is a network based PSA installer version $INSTALLER. It will attempt to" 
echo "retrieve dependency files through Ximian Red Carpet, and"
echo "ftp.plesk.com. If you have these files stored on your"
echo "local server, you can overriding these settings with"
echo "these commands "
echo "export PLESK_SERVER=<your plesk distro server> "
echo "export PLESK_PATH=<path to files on your PSA server> "
echo ""
echo "Please report bugs to scott@atomicrocketturtle.com"
echo "or www.atomicrocketturtle.com"
echo "Hit any key to continue, or ctrl-C to abort"

read foo < /dev/tty

MANIFEST_SERVER=www.atomicrocketturtle.com
MANIFEST_PATH=/psa
PSA_VERSION=5.0.5
PSA_MINOR_VERSION=build030207.16
LOGFILE=../psa-install.log
REDHAT_GPG_FINGERPRINT="219180CDDB42A60E"
XIMIAN_SERVER=www.atomicrocketturtle.com
XIMIAN_PATH=/psa/redcarpet/


#version check
if [ -f /etc/redhat-release ]; then
  RELEASE_NUM=`cat /etc/redhat-release | sed -e 's/^.* release //' | sed -e 's/ \(.*\)$//'`
  RELEASE_MAJOR=`echo $RELEASE_NUM | sed -e 's/\..*$//'`
  RELEASE_MINOR=`echo $RELEASE_NUM | awk -F. '{print $2}'`
else 
  echo This install only supports RedHat systems at this time
  rm psa-install.sh
  exit 0
fi


if [ $RELEASE_NUM = 7.2 ]; then
  echo "You are running Redhat $RELEASE_NUM"
  XIMIAN_VER=redhat-72-i386
  SUB_XIMIAN_VER=redhat-72-i386
  RCD_VERSION=rcd-1.4.2-0.ximian.3.2.i386.rpm
  RUG_VERSION=rug-1.4.3-0.ximian.3.1.i386.rpm
elif [ $RELEASE_NUM = 7.3 ]; then
  echo "You are running Redhat $RELEASE_NUM"
  XIMIAN_VER=redhat-73-i386
  SUB_XIMIAN_VER=redhat-73-i386
  RCD_VERSION=rcd-1.4.2-0.ximian.4.2.i386.rpm
  RUG_VERSION=rug-1.4.3-0.ximian.4.1.i386.rpm
else
  echo "This net-installer currently only supports Redhat version 7.2 and 7.3"
  exit 0
fi


if [ "$PLESK_SERVER" = "" ]; then
  # IP for ftp.plesk.com
  PLESK_SERVER=209.225.2.67
fi

if [ "$PLESK_PATH" = "" ]; then
  PLESK_PATH=/redhat.rpm/psa5.0_distros/
fi

## gpg check
#if [ -x /usr/bin/gpg ]; then
#  KEY=KEY`gpg --list-keys -q --with-colons |grep security@redhat.com | awk -F: '{print $5}'`
#  
#  
#  if [ $KEY = KEY$REDHAT_GPG_FINGERPRINT ]; then
#    echo "Testing with GPG"
#    RPMCHECK="rpm -K"
#  else
#    echo "Testing with MD5"
#    RPMCHECK="rpm -K --nogpg"
#  fi
#else
#  echo "Testing with MD5"
#  RPMCHECK="rpm -K --nogpg"
#fi
  

if [ -d tmp-psa-install ]; then
  echo "tmp dir already exists, previous installation attempt?"
  echo "Hit any key to continue or ctl-c to abort"
  if [ -f tmp-psa-install/install ] ; then
    rm tmp-psa-install/install
  fi
  read foo < /dev/tty
else 
  echo Creating tmp dir
  mkdir tmp-psa-install
fi
cd tmp-psa-install

if [ "$GET" = "" ]; then 
  if [ -f `which wget` ]; then
    GET=wget
    echo Using wget to retrieve files
  elif [ -f `which ncftpget` ]; then
    GET=ncftpget
    echo Using ncftpget to retrieve files
  else
    echo Couldnt find wget or ncftpget, check your paths and/or ensure that they are installed
    exit 0
  fi
fi

# Install Ximian rcd and rug
                                                                                                                                         
echo -n "Checking for Ximian red-carpet: "
                                                                                                                                         
RCD_PID=`pidof rcd`
                                                                                                                                         
if [ "$RCD_PID" != "" ]; then
  echo " PID $RCD_PID"
else
  if [ -x /usr/sbin/rcd ] ; then
    echo -n rcd
    echo "Starting rcd..."
    #/usr/sbin/rcd --late-background
    /etc/init.d/rcd restart
  else
    echo "rcd not detected"
    rpm -Uvh http://$XIMIAN_SERVER/$XIMIAN_PATH/$XIMIAN_VER/$RCD_VERSION
    if [ -x /usr/sbin/rcd ] ; then
      echo "Starting rcd..."
     # /usr/sbin/rcd --late-background
      /etc/init.d/rcd restart
    else
      echo "ERROR: rcd could not be started, possibly the download failed"
      exit 1
    fi
  fi
fi

echo -n "Checking for rc/rug: "
if [ -x /usr/bin/rug ] ; then
  echo -n "rug detected"
  RC=rug
elif [ -x /usr/bin/rc ] ; then
  echo -n "rc detected"
  RC=rc
else
  echo "rug not detected"
  rpm -Uvh http://$XIMIAN_SERVER/$XIMIAN_PATH/$XIMIAN_VER/$RUG_VERSION
  if [ -x /usr/bin/rug ] ; then
    echo
  else
    echo "ERROR: rug could not be started, possibly the download failed"
    exit 1
  fi
fi

# test to see if rcd is running, and rug can talk to it
echo
echo -n "Syncing... (this could take a minute)"
  sleep  60
echo OK
                                                                                                                                         
echo -n "Checking to see if rcd is running (if this fails, this script will attempt to retry): "
RCD_RUNNING=`$RC ping |grep Carpet`
                                                                                                                                         
if [ "$RCD_RUNNING" != "" ]; then
  echo "$RCD_RUNNING"
else
  echo "Could not connect, trying a mirror "
  # Setting rcd mirror to UUNET
  # need to figure out how to make this loop until it connects
  echo "Setting Ximian Mirror to UUNET..."
  FAIL=n
  $RC mirrors 18 || FAIL=y
  if [ $FAIL = y ]; then
    echo "Could not connect to the rcd daemon..."
    /etc/init.d/rcd restart
    echo "Syncing....(this could take a few minutes)"
    sleep 60
                                                                                                                                         
    FAIL=n
    $RC mirrors 18 || FAIL=y
    if [ $FAIL = y ]; then
      echo "ERROR: Could not communicate with the Red Carpet daemon. Check firewall rules, and if the process is running, then try: rug ping   to see if the daemon is operating. Sometimes rcd takes a while to start up"
      exit 1
    fi
  fi
fi
                                                                                                                                         
# add a test to see if rcd already subscribed
$RC subscribe $SUB_XIMIAN_VER



echo -n "Getting $RELEASE_NUM file manifest "
$GET http://$MANIFEST_SERVER/$MANIFEST_PATH/$RELEASE_NUM-manifest >>$LOGFILE 2>&1
if [ -f $RELEASE_NUM-manifest ] ; then
  echo OK
else
  echo FAILED 
  exit 1
fi


for i in `cat $RELEASE_NUM-manifest`; do
  PACKAGE=`echo $i | awk -F@@ '{print $1}'`
  VERSION=`echo $i | awk -F@@ '{print $2}'`
  echo -n "Testing for $PACKAGE"

  MANIFEST=`rpm --qf %{NAME} -q $PACKAGE | awk '{print $1}' `

  if [ $PACKAGE = $MANIFEST ]; then
    echo " OK"
  else
    echo "not detected"
    echo $PACKAGE >> install
  fi

done
sleep 5

if [ -f install ] ; then
  $RC -y in --read-from-file install || exit 1
fi


INSTALLED_VERSION=`rpm --qf %{VERSION} -q psa | awk '{print $1}'`

if [ $INSTALLED_VERSION = "package" ]; then
  echo "PSA is not installed"
else
  echo "PSA version $PSA_VERSION is installed"
  if [ $INSTALLED_VERSION = $PSA_VERSION ]; then
    echo "PSA $PSA_VERSION is already installed, are you sure you wish to continue?"
    echo "Hit any key to continue, or ctrl-C to abort"
    read foo < /dev/tty
  fi

fi
# Get PSA files
echo -n "Retrieving psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz"
if [ -f psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz ]; then
  echo "OK (local)"
else
  $GET ftp://$PLESK_SERVER/$PLESK_PATH/psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz 
  if [ -f psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz ]; then
    echo OK
  else
    echo "$GET ftp://$PLESK_SERVER/$PLESK_PATH/psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz"
    echo "FAILED"
    exit 0
  fi
fi

echo -n "Installing PSA RPMS "
if [ -f psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz ]; then
  FAIL=n
  tar zxvf psa-$PSA_VERSION-rh$RELEASE_NUM.$PSA_MINOR_VERSION.i586.rpm.tar.gz
  cd rpm_RedHat$RELEASE_NUM/base
  rpm -Uvh *rpm || FAIL=y
  if [ $FAIL = y ]; then
    echo "Base PSA install FAILED"
    exit 0
  fi
  cd ../opt/fp
  rpm -Uvh *rpm || FAIL=y
  if [ $FAIL = y ]; then
    echo "Frontpage extention install FAILED"
    exit 0
  fi
  cd ../perl
  rpm -Uvh *rpm || FAIL=y
  if [ $FAIL = y ]; then
    echo "Perl extention install FAILED";
    exit 0
  fi
  echo OK
else
  echo FAILED
  exit 0
fi

$RC update -y


# cleanup
echo ""
echo ""
echo ""
echo Cleaning up
cd ../../../..
rm psa-install.sh
rm -rf tmp-psa-install
