#!/bin/bash
# CWP Big File Upload for all php versions
# Default max execution time is 300

if [ ! -z "$1" ];then
	BF_SIZE=$1

	if [ "$BF_SIZE" -ge "2" ];then

	BF_UPLOAD_MAX_FILESIZE=$((BF_SIZE))
	BF_POST_MAX_SIZE=$((BF_SIZE+5))
	BF_MEMORY_LIMIT=$((BF_SIZE+24))
	BF_MAX_INPUT_TIME="300"
	BF_MAX_EXECUTION_TIME="300"

	echo "New Configuration:"
	echo "---------------------"
	echo "upload_max_filesize = $((BF_UPLOAD_MAX_FILESIZE))M"
	echo "post_max_size = $((BF_POST_MAX_SIZE))M"
	echo "memory_limit = $((BF_MEMORY_LIMIT))M"
	echo "max_input_time = $((BF_MAX_INPUT_TIME))"
	echo "max_execution_time = $((BF_MAX_EXECUTION_TIME))"
	echo "---------------------"

	# Setup All PHP selectors CGI/FPM
	for i in `ls /opt/alt/php*/usr/php/php.ini`;do
		if [ -e "$i" ];then
			echo "Editing File: $i"
			sed -i "s/^upload_max_filesize.*/upload_max_filesize = $((BF_UPLOAD_MAX_FILESIZE))M/g" $i
			sed -i "s/^post_max_size.*/post_max_size = $((BF_POST_MAX_SIZE))M/g" $i
			sed -i "s/^memory_limit.*/memory_limit = $((BF_MEMORY_LIMIT))M/g" $i
			sed -i "s/^max_input_time.*/max_input_time = $((BF_MAX_INPUT_TIME))/g" $i
			sed -i "s/^max_execution_time.*/max_execution_time = $((BF_MAX_EXECUTION_TIME))/g" $i
		fi
	done

	# Setup Main PHP.ini (PHP Switcher)
	BF_MAIN_PHP_INI="/usr/local/php/php.ini"
	echo "Editing File: $BF_MAIN_PHP_INI"
	sed -i "s/^upload_max_filesize.*/upload_max_filesize = $((BF_UPLOAD_MAX_FILESIZE))M/g" $BF_MAIN_PHP_INI
	sed -i "s/^post_max_size.*/post_max_size = $((BF_POST_MAX_SIZE))M/g" $BF_MAIN_PHP_INI
	sed -i "s/^memory_limit.*/memory_limit = $((BF_MEMORY_LIMIT))M/g" $BF_MAIN_PHP_INI
	sed -i "s/^max_input_time.*/max_input_time = $((BF_MAX_INPUT_TIME))/g" $BF_MAIN_PHP_INI
	sed -i "s/^max_execution_time.*/max_execution_time = $((BF_MAX_EXECUTION_TIME))/g" $BF_MAIN_PHP_INI

	else
		echo "Please set the limit higher than 2"
	fi
else
	echo "Please set the limit"
fi