#!/bin/bash

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

if [ ! -e "/usr/local/cpanel/cpanel" ]; then
    exit 0
fi

if [  ! -e "/proc/sys/fs/protected_symlinks_allow_gid" ]; then
    exit 0
fi

POSTUPCP="/scripts/postupcp"
TARGET="cloudlinux-linksafe"
COMMON_PATH="/usr/share/${TARGET}/cpanel/hooks"

deleteAllInclude "${POSTUPCP}" "postupcp" "/#${TARGET}/d" "${COMMON_PATH}"

chown -h :root /usr/local/cpanel/base/frontend/paper_lantern/styled/*
