#!/usr/bin/bash

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

##################################################
# Common fucntions                               #
##################################################
VERSION="0.1beta"

common_path_of_cpanel="/usr/share/lve/modproctitle"
common_current_date=`date +%Y-%m-%d`
common_tmp_path="$common_path_of_cpanel/tmp"

function getEasyApacheVer(){
    cat /var/cpanel/easy/apache/profile/_main.yaml | grep version | cut -d":" -f2 | tr -d ' ' | tr -d "'"
}

ea4_indicator="/etc/cpanel/ea4/is_ea4"
function isEA4(){
    # Broken symlink should be ok too, just the same as python's os.path.lexists
    [ -e "$ea4_indicator" -o -h "$ea4_indicator" ]
}

function getLogFile(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    echo "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p "$common_path_of_cpanel/logs"
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    prg=`basename "$0"`
    echo "[$current_date_time from $prg] $1" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function writeFileToLog(){
    if [ ! -e "$common_path_of_cpanel/logs" ];then
	mkdir -p $common_path_of_cpanel/logs
    fi
    current_date_time=`date +"%Y-%m-%d %k:%M:%S"`
    prg=`basename "$0"`
    echo "[$current_date_time from $prg] ----------------File Content $1 BEG---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
    if [ -e "$1" ];then
	cat $1 >> "$common_path_of_cpanel/logs/$common_current_date.log"
    fi
    echo "[$current_date_time from $prg] ----------------File Content $1 End---------------" >> "$common_path_of_cpanel/logs/$common_current_date.log"
}

function checkForAppNameSyntax(){
    isApache2_0syntax=`echo "$1" | grep [,\;@]`
    if [ -n "$isApache2_0syntax" ];then
	echo "$1" | cut -d',' -f 3 | tr '[:lower:]' '[:lower:]'
    else
	echo "$1"
    fi
}

function removeEmptyStringsFromFile(){
    filename="$1"
    res=`sed -e '/^$/d' $filename`
    echo "$res" > $filename
}

function deleteAllExcept(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed -n "$3" > "$common_tmp_path/$2.tmp.$$"
        echo "#!/bin/bash" > "$1"
        cat "$common_tmp_path/$2.tmp.$$" >> "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}

function deleteAllInclude(){
    #1 - hook
    #2 - tmp name
    #3 - pattern
    if [ ! -e "$common_tmp_path" ]; then
        mkdir -p "$common_tmp_path"
    fi
    if [ -e "$1" ];then
        cat "$1" | sed "$3" > "$common_tmp_path/$2.tmp.$$"
        cat "$common_tmp_path/$2.tmp.$$" > "$1"
        rm -f "$common_tmp_path/$2.tmp.$$"
    fi
}


function showBar {
 nmb=$(cat $0 | grep showBar | wc -l)
 let "nmb = $nmb"
 let "prct = $1 * 30 / $nmb"
 let "prct_n = $1 * 100 / $nmb"
 prg=`basename "$0"`
 echo -n "$prg: [" >&2
 for bar in {1..30}
 do
  if [ $bar -le $prct ];then
    echo -n "#" >&2
  else
    echo -n " " >&2
  fi
 done
 echo -ne "] ($prct_n%)\r" >&2
}

function get_command(){
    command=$(which $1)
    if [ $? != 0 ]; then
        writeToLog "Can't execute command $1..."
        exit 1;
    fi
    echo $command
}

function cmakeSorce(){
 if isEA4; then
    echo "CPanel EA4 detected. This package does not support it - exiting..."
    writeToLog "CPanel EA4 detected. This package does not support it - exiting..."
    exit 2
 fi
 cur=`pwd`
 log=`getLogFile`
 iscmake=$(get_command "cmake")
 CMAKE_SRC=$common_tmp_path/tmpcmakesrc
 if [ -e "$CMAKE_SRC" ];then
    rm -rf "$CMAKE_SRC"
 fi
 mkdir -p $CMAKE_SRC
 tar -zxvf $1 -C $CMAKE_SRC >>$log
 (cd $CMAKE_SRC/$2
            $iscmake . 1>>$log 2>&1 && make 1>>$log 2>&1 && make install 1>>$log 2>&1)
 makeproc=$?
 rm -rf $CMAKE_SRC
 cd "$cur" 
 if [ "$makeproc" -ne 0 ]; then
    echo -n "Compiling module failed. Please see description in log file "; getLogFile
    writeToLog "Compiling module failed"
 fi
 return $makeproc
}

function installModule_proctitle(){

        pathtosrc="$common_path_of_cpanel/tars/mod_proctitle.tar.gz"
        cmakeSorce $pathtosrc ""
        if [[ $? != 0 ]]; then
            writeToLog "Linking error mod_proctitle.tar.gz"
            exit 1
        else
            writeToLog "Module mod_proctitle compiled... Ok"
        fi
        if [ ! -e "/usr/local/apache/conf/conf.d" ];then
            mkdir -p /usr/local/apache/conf/conf.d/
        fi
        if [ ! -e "/usr/local/apache/conf/conf.d/proctitle.conf" ];then
            cp -f "$common_path_of_cpanel/confs/proctitle.conf" /usr/local/apache/conf/conf.d/proctitle.conf
        fi
        return 0
}

