#!/bin/bash

# Copyright (c) Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2018 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

USE_DA_MOD_LSAPI_RPM="no"

CONF="$(dirname "$0")/da_cb_install.conf"
[ -f "$CONF" ] && . "$CONF"

if [ "$USE_DA_MOD_LSAPI_RPM" != "yes" ]
then
    USE_DA_MOD_LSAPI_RPM="no"
fi


function do_exit() {
    # In order to forcefully fail current build even when custom_build script ignores exit code
    [ -f CMakeLists.txt ] && rm CMakeLists.txt
    exit $1
}

function getRPM() {
PARAM="$1"
RESULT=""
mkfifo /usr/share/lve/da_lsapi/da_install_lsapi
ls -1 /usr/share/lve/da_lsapi/$PARAM* 2>/dev/null 1> /usr/share/lve/da_lsapi/da_install_lsapi &
while read file_nm
do
    RESULT=$(echo "$file_nm" | grep $PARAM)
    if [ -n "$RESULT" ]
    then
        break
    fi
done <  /usr/share/lve/da_lsapi/da_install_lsapi
rm /usr/share/lve/da_lsapi/da_install_lsapi
echo "$RESULT"
}

SOCKPATH="/var/run/mod_lsapi"
mkdir -p /usr/share/lve/da_lsapi/

if [ "$#" -ne 2 ]
then
    echo "*** da_cb_install params error: 2 params should be provided (LSAPI_RELEASE and LSAPI_VERSION), $# is provided instead ***"
    echo "$(date): *** da_cb_install params error: 2 params should be provided (LSAPI_RELEASE and LSAPI_VERSION), $# is provided instead ***">>/usr/share/lve/da_lsapi/da_cb_install.log
    do_exit 1
fi

REPO="$1"
VER="$2"

if [ "$REPO" != "beta" -a "$REPO" != "stable" ]
then
    echo "*** da_cb_install params error: LSAPI_RELEASE param should be beta or stable, $REPO is provided instead ***"
    echo "$(date): *** da_cb_install params error: LSAPI_RELEASE param should be beta or stable, $REPO is provided instead ***">>/usr/share/lve/da_lsapi/da_cb_install.log
    do_exit 1
fi

if [ -z "$VER" ]
then
    echo "*** da_cb_install params error: void string is provided as LSAPI_VERSION ***"
    echo "$(date): *** da_cb_install params error: void string is provided as LSAPI_VERSION ***">>/usr/share/lve/da_lsapi/da_cb_install.log
    do_exit 1
fi

RESULT=""
RESULT_DEVEL=""
RESULT_SRC=""

echo "$(date): da_cb_install is invoked: LSAPI_RELEASE:$REPO; LSAPI_VERSION:$VER; USE_DA_MOD_LSAPI_RPM:$USE_DA_MOD_LSAPI_RPM" >>/usr/share/lve/da_lsapi/da_cb_install.log

yum install -y yum-utils python-lxml
REPOSIT=""
if [ "$REPO" == "beta" ];then
    REPOSIT="--enablerepo=cloudlinux-updates-testing"
fi

IS_CL6=$(uname -r | grep '\.el6')
if [ -z "$IS_CL6" ]
then
    yum install -y criu-lve criu-lve-devel crit-lve --disableexcludes=all "$REPOSIT"
fi

RESULT=$(getRPM liblsapi-"$VER")
RESULT_DEVEL=$(getRPM liblsapi-devel-"$VER")

if [ "$USE_DA_MOD_LSAPI_RPM" == "yes" ]
then
    RESULT_SRC=$(getRPM da-mod_lsapi-"$VER")
fi

if [ -z "$RESULT" ]
then
    pushd /usr/share/lve/da_lsapi/
    yumdownloader liblsapi-"$VER"* --disableexcludes=all --downloaddir=/usr/share/lve/da_lsapi/ "$REPOSIT"
    popd
fi

if [ -z "$RESULT_DEVEL" ]
then
    pushd /usr/share/lve/da_lsapi/
    yumdownloader liblsapi-devel-"$VER"* --disableexcludes=all --downloaddir=/usr/share/lve/da_lsapi/ "$REPOSIT"
    popd
fi

if [ "$USE_DA_MOD_LSAPI_RPM" == "yes" ]
then
    if [ -z "$RESULT_SRC" ]
    then
        pushd /usr/share/lve/da_lsapi/
        yumdownloader da-mod_lsapi-"$VER"* --disableexcludes=all --downloaddir=/usr/share/lve/da_lsapi/ "$REPOSIT"
        popd
    fi
fi

RESULT=$(getRPM liblsapi-"$VER")
RESULT_DEVEL=$(getRPM liblsapi-devel-"$VER")
if [ "$USE_DA_MOD_LSAPI_RPM" == "yes" ]
then
    RESULT_SRC=$(getRPM da-mod_lsapi-"$VER")
fi

if [ -z "$RESULT" ]
then
    echo "*** da_cb_install error: cannot download liblsapi $VER version ***"
    echo "$(date): *** da_cb_install error: cannot download liblsapi $VER version  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
    do_exit 1
fi

if [ -z "$RESULT_DEVEL" ]
then
    echo "*** da_cb_install error: cannot download liblsapi-devel $VER version ***"
    echo "$(date): *** da_cb_install error: cannot download liblsapi-devel $VER version  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
    do_exit 1
fi

if [ "$USE_DA_MOD_LSAPI_RPM" == "yes" ]
then
    if [ -z "$RESULT_SRC" ]
    then
        echo "*** da_cb_install error: cannot download da-mod_lsapi $VER version ***"
        echo "$(date): *** da_cb_install error: cannot download da-mod_lsapi $VER version  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
        do_exit 1
    fi
fi

if [ "$USE_DA_MOD_LSAPI_RPM" == "yes" ]
then
    echo "da_cb_install: mod_lsapi sources will be provided by da-mod_lsapi.rpm"
    echo "$(date): da_cb_install: mod_lsapi sources will be provided by da-mod_lsapi.rpm">>/usr/share/lve/da_lsapi/da_cb_install.log

    # Update source package only if it is not installed or in case of version mismatch
    if ! rpm -q da-mod_lsapi | grep -qF "da-mod_lsapi-$VER.cloudlinux"
    then
        if ! rpm -Uhv "$RESULT_SRC" --oldpackage
        then
            echo "*** da_cb_install error: cannot update mod_lsapi from $RESULT_SRC ***"
            echo "$(date): *** da_cb_install error: cannot update mod_lsapi from $RESULT_SRC  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
            do_exit 1
        fi
    fi
    SRCPKG="/usr/share/lve/modlscapi/DirectAdmin/mod_lsapi-${VER}.tar.gz"
    if [ ! -f "$SRCPKG" ]
    then
        echo "*** da_cb_install error: cannot find da_mod_lsapi sources $SRCPKG ***"
        echo "$(date): *** da_cb_install error: cannot find da_mod_lsapi sources $SRCPKG  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
        do_exit 1
    fi

    mkdir da-mod_lsapi-tmp
    cd da-mod_lsapi-tmp
    tar xf "$SRCPKG"

    if [ ! -d "mod_lsapi-${VER}" ]
    then
        cd ..
        rm -fr da-mod_lsapi-tmp
        echo "*** da_cb_install error: cannot find da_mod_lsapi sources root ***"
        echo "$(date): *** da_cb_install error: cannot find da_mod_lsapi sources root ***">>/usr/share/lve/da_lsapi/da_cb_install.log
        do_exit 1
    fi
    cd ..

    # To prevent copiing them into working mod_lsapi directory of the custom build
    rm -f da-mod_lsapi-tmp/mod_lsapi-${VER}/install/da_cb_install*

    # Copy sources from the tarball provided by da_mod_lsapi into the working directory of the custom build
    cp -fr da-mod_lsapi-tmp/mod_lsapi-${VER}/* .
    rm -fr da-mod_lsapi-tmp
fi

# Update liblsapi* packages only if liblsapi is not installed or in case of version mismatch
# liblsapi-devel contains dep from liblsapi with the same version, so check liblsapi inly
if ! rpm -q liblsapi | grep -qF "liblsapi-$VER.el"
then
    if ! rpm -Uhv "$RESULT" "$RESULT_DEVEL" --oldpackage
    then
        echo "*** da_cb_install error: cannot update liblsapi from $RESULT and $RESULT_DEVEL ***"
        echo "$(date): *** da_cb_install error: cannot update liblsapi from $RESULT and $RESULT_DEVEL  ***">>/usr/share/lve/da_lsapi/da_cb_install.log
        do_exit 1
    fi
fi

if [ ! -e /etc/cl.selector/lsphp -a -e /usr/sbin/cagefsctl -a -e /etc/cl.selector ]; then
    ln -s /usr/local/bin/lsphp /etc/cl.selector/lsphp
    /usr/sbin/cagefsctl --update-etc
    RES=`cat /etc/cl.selector/native.conf | grep lsphp=/usr/local/bin/lsphp `
    if [ -z "$RES" ]; then
        echo "lsphp=/usr/local/bin/lsphp" >> /etc/cl.selector/native.conf
    fi
    /usr/sbin/cagefsctl --setup-cl-selector

    echo "da_cb_install: reconfiguration completed"
    echo "$(date): da_cb_install: reconfiguration completed">>/usr/share/lve/da_lsapi/da_cb_install.log
fi

if [ ! -e "$SOCKPATH" ]; then
    mkdir -p "$SOCKPATH"
fi

chown apache:apache "$SOCKPATH"
chmod 755 "$SOCKPATH"
echo "$(date): da_cb_install: completed successfully, version $VER">>/usr/share/lve/da_lsapi/da_cb_install.log
exit 0
