#!/bin/bash -e

. ./xi-sys.cfg

# Was previous step completed?
if [ ! -f installed.sourceguardian ]; then
	echo "Sourceguardian was not configured - run previous script" >&2
	exit 1
fi

# Was this step already completed?
if [ -f installed.phpini ]; then
	echo "PHP already configured - skipping."
	exit 0
fi

# Set timezone in PHP script (this will also be done by the PHP on final install)
if [ -r /etc/sysconfig/clock ]; then
    . /etc/sysconfig/clock
    timezone="$ZONE"
elif [ `command -v timedatectl` ]; then
    timezone=$(timedatectl | awk '/Time zone:/ {print $3}')
fi

# Set filesize and post limits
sed -i "s/^upload_max_filesize =.*/upload_max_filesize = 20M/" "$phpini" || true
sed -i "s/^post_max_size =.*/post_max_size = 21M/" "$phpini" || true
sed -i "s/^;* *max_input_vars =.*/max_input_vars = 90000/" "$phpini" || true


# Set php.ini defaults
echo "Checking php.ini defaults..."
for file in $(find /etc -name "php.ini"); do
    echo "Checking max_execution_time for PHP in $file..."
    if ! execution_time_line=$(grep "^max_execution_time =" $file); then
        # Uncommented line not found, look for commented line, replace with our configuration
        if execution_time_line=$(grep "^;max_execution_time =" $file); then
            commented_execution_time_line=$(echo $execution_time_line | tail -n 1)
            echo "Found commented max_execution_time value, setting to 600..."
            sed -i "s/$commented_execution_time_line/max_execution_time = 600/g" $file
        fi
    else
        execution_time=$(echo $execution_time_line | awk '{print $3}')
        if [ ! -z "$execution_time_line" ] && ([[ $execution_time_line = ";"* ]] || [ "$execution_time" -lt "600" ]); then
            echo "max_execution_time is less than 600 in $file, setting to 600..."
            sed -i "s/$execution_time_line/max_execution_time = 600/g" $file
        fi
    fi

    # Check max_input_time
    echo "Checking max_input_time for PHP in $file..."
    if ! max_input_time_line=$(grep "^max_input_time =" $file); then
        # Uncommented line not found, look for commented line, replace with our configuration
        if max_input_time_line=$(grep "^;max_input_time =" $file); then
            commented_max_input_time_line=$(echo $max_input_time_line | tail -n 1)
            echo "Found commented max_input_time value, setting to 1200..."
            sed -i "s/$commented_max_input_time_line/max_input_time_line = 1200/g" $file
        fi
    else
        input_time=$(echo $max_input_time_line | awk '{print $3}')
        if [ ! -z "$max_input_time_line" ] && ([[ $max_input_time_line = ";"* ]] || [ "$input_time" -lt "1200" ]); then
            echo "max_input_time is less than 1200 in $file, setting to 1200..."
            sed -i "s/$max_input_time_line/max_input_time = 1200/g" $file
        fi
    fi

    # Check max_input_vars
    echo "Checking max_input_vars for PHP in $file..."
    if ! max_input_vars_line=$(grep "^max_input_vars =" $file); then
        # Uncommented line not found, look for commented line, replace with our configuration
        if max_input_vars_line=$(grep "^;max_input_vars =" $file); then
            commented_max_input_vars_line=$(echo $max_input_vars_line | tail -n 1)
            echo "Found commented max_input_vars value, setting to 50000..."
            sed -i "s/$commented_max_input_vars_line/max_input_vars = 50000/g" $file
        fi
    else
        input_vars=$(echo $max_input_vars_line | awk '{print $3}')
        if [ ! -z "$max_input_vars_line" ] && ([[ $max_input_vars_line = ";"* ]] || [ "$input_vars" -lt "50000" ]); then
            echo "max_input_vars is less than 50000 in $file, setting to 50000..."
            sed -i "s/$max_input_vars_line/max_input_vars = 50000/g" $file
        fi
    fi

    # Check memory_limit
    echo "Checking memory_limit for PHP in $file..."
    if ! memory_limit_line=$(grep "^memory_limit =" $file); then
        # Uncommented line not found, look for commented line, replace with our configuration
        if memory_limit_line=$(grep "^;memory_limit =" $file); then
            commented_memory_limit_line=$(echo $memory_limit_line | tail -n 1)
            echo "Found commented memory_limit value, setting to 1024M..."
            sed -i "s/$commented_memory_limit_line/memory_limit = 1024M/g" $file
        fi
    else
        memory_limit=$(echo $memory_limit_line | awk '{print $3}')
        # Extract the integer value from the memory limit
        int_memory_limit=$(echo $memory_limit | sed 's/[^0-9]*//g')
        # Compare the extracted integer value with another value
        if [ ! -z "$memory_limit_line" ] && ([[ $memory_limit_line = ";"* ]] || [ "$int_memory_limit" -lt 1024 ]); then
            echo "memory_limit is less than 1024M in $file, setting to 1024M..."
            sed -i "s/$memory_limit_line/memory_limit = 1024M/g" $file
        fi
    fi

    if [ -n $timezone ]; then
        echo "Setting date.timezone for PHP in $file..."
        sed -i "s|^;date\.timezone =.*|date.timezone = $timezone|" "$file" || true
    fi

done


# Do some basic session changes to disable defaults
sed -i "/session.upload_progress.enabled =.*/c\session.upload_progress.enabled = 0" "$phpini" || true
sed -i "/session.use_strict_mode =.*/c\session.use_strict_mode = 1" "$phpini" || true

echo "PHP configured OK"
touch installed.phpini

