#!/bin/sh

# checking OS
case `uname` in
    FreeBSD)
        # checking installed packages
        ########## pcre ###############
        echo -n "Checking for PCRE: "
        pkg_info -Ex "^pcre\-" 2>&1 >/dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: portinstall pcre"
            echo "                       or: pkg_add pcre"
        fi
        ########## libyaml ###############
        echo -n "Checking for YAML (libyaml): "
        pkg_info -Ex "^libyaml\-" 2>&1 >/dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: portinstall libyaml"
            echo "                       or: pkg_add libyaml"
        fi
        ########## nginx ###############
        echo -n "Checking for NGINX: "
        pkg_info -Ex "^nginx\-" 2>&1 >/dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: portinstall nginx"
            echo "                       or: pkg_add nginx"
        fi
        ########## libxml2 ###############
        echo -n "Checking for XML2 (libxml2): "
        pkg_info -Ex "^libxml2\-" 2>&1 >/dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: portinstall libxml2"
            echo "                       or: pkg_add libxml2"
        fi
        ;;
    Linux)
        # checking Distribution
        if [ ! -e "/etc/redhat-release" ];
        then
            echo "Unsupported Distributionx"
            exit 1
        fi

        ########## pcre ###############
        echo -n "Checking for PCRE: "
        rpm -qa | egrep "^pcre-[0-9]" &> /dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: yum install pcre"
        fi
        ########## libyaml ###############
        echo -n "Checking for YAML (libyaml): "
        rpm -qa | egrep "^libyaml-[0-9]" &> /dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install download latest packages from http://packages.sw.be/libyaml/ ( or any other source ) and install via rpm"
            echo "   Example: wget http://packages.sw.be/libyaml/libyaml-0.1.3-1.el5.rf.i386.rpm"
            echo "            rpm -ivh libyaml-0.1.3-1.el5.rf.i386.rpm"
        fi
        ########## nginx ###############
        echo -n "Checking for NGINX: "
        NGX=`rpm -qa | egrep "^nginx-[0-9]"`
        if [ $? -eq 0 ];
        then
            NGX_V=`echo ${NGX} | cut -d\- -f2 | cut --output-delimiter="" -d\. -f1,2`
            if [ ${NGX_V} -ge 7 ];
            then
                echo "Ok"
            else
                echo "Version less than 0.7"
            fi
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   You can download latest package from http://rpm.pbone.net"
        fi
        ########## libxml2 ###############
        echo -n "Checking for XML2 (libxml2): "
        rpm -qa | egrep "^libxml2-[0-9]" &> /dev/null
        if [ $? -eq 0 ];
        then
            echo "Ok"
        else
            echo "Not Found ( Or not installed via packages )"
            echo "   To install it just run: yum install libxml2"
        fi
        ;;
    *)
        echo "Unsupported OS";
        exit 1
        ;;
esac

########## cPanel Version Check ( should be greater that 1125 ) ###############
echo -n "Checking cPanel Version: "
if [ ! -e /usr/local/cpanel/cpanel ];
then
    echo "Not Installed"
    exit 1
else
    CPANEL_VERASION=`/usr/local/cpanel/cpanel -V | awk -F\. '{print $1$2}'`
    if [ ${CPANEL_VERASION} -ge 1125 ];
    then
        echo "Ok"
    else
        echo "Version less than 11.25"
        exit 1
    fi
fi

########## check cPanel Hooks ###############
HOOKS="/usr/local/cpanel/hooks/addondomain/addaddondomain /usr/local/cpanel/hooks/addondomain/deladdondomain /usr/local/cpanel/hooks/subdomain/addsubdomain /usr/local/cpanel/hooks/subdomain/delsubdomain /usr/local/cpanel/hooks/park/park /usr/local/cpanel/hooks/park/unpark /scripts/postwwwacct /scripts/postkillacct /scripts/after_apache_make_install /scripts/postupdateuserdomains"

HOOK_EXIST=0

for HOOK in ${HOOKS};
do
    if [ -e ${HOOK} ];
    then
        if [ "`readlink ${HOOK}`" != "/usr/local/nci/bin/"`basename ${HOOK}` ];
        then
            if [ ${HOOK_EXIST} -eq 0 ];
            then
                echo "Seems that one of required hooks is already installed"
                echo "Please contact support@remsys.com and include following information in your message:"
                echo "------------------"
                HOOK_EXIST=1
            fi
            ls -l ${HOOK}
        fi
    else
        # checking if symlink that we have an nonexistent symlink
        if [ -L ${HOOK} ];
        then
            if [ ${HOOK_EXIST} -eq 0 ];
            then
                echo "Seems that one of required hooks is already installed"
                echo "Please contact support@remsys.com and include following information in your message:"
                echo "------------------"
                HOOK_EXIST=1
            fi
            ls -l ${HOOK}
        fi
    fi
done

if [ ${HOOK_EXIST} -eq 1 ];
then
    exit 1
fi

exit 0

