-
Notifications
You must be signed in to change notification settings - Fork 15
/
php_cleanup
executable file
·77 lines (65 loc) · 1.95 KB
/
php_cleanup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
### This is a simple script that wraps the AWK script and handles all
### the file munging.
SCRIPTNAME=${0##*/}
## Prints a usage help message.
function print_usage() {
echo "usage: $SCRIPTNAME -p|-d [-m <memory limit>] [php.ini]"
echo "Example: $SCRIPTNAME -p -m 2G /etc/php5/fpm/php.ini"
}
## Check the number of arguments.
if [ $# -lt 1 ] || [ $# -gt 4 ]; then
print_usage
exit 1
fi
## The default php.ini file.
INPUT_FILE=php.ini
## Getting the input filename. By default assumes that is called
## php.ini and is on the current directory.
## Getting the input filename. By default assumes that is called
## php.ini and is on the current directory.
## Get the input file in the case is given as argument.
case $# in
2)
INPUT_FILE=$2
;;
4)
INPUT_FILE=$4
;;
esac
## The AWK script that does the cleanup.
AWK_SCRIPT=$(dirname $0)/php_cleanup.awk
[ -r $AWK_SCRIPT ] || exit 0
## Create the temporary file for output.
TEMP_FILE=$(mktemp)
## Validate the given memory value.
function validate_memory() {
[ -n "$(echo $1 | awk '/^[[:digit:]]+[MG]$/ {print}')" ]
}
## Run the clean up of the php.ini file for a production environment.
while getopts dm:p OPT; do
case $OPT in
p|+p) # In a production environment.
$AWK_SCRIPT -v is_prod=1 $INPUT_FILE | uniq > $TEMP_FILE
;;
d|+d) # In a development environment.
$AWK_SCRIPT $INPUT_FILE | uniq > $TEMP_FILE
;;
m|+m)
if validate_memory $OPTARG; then
echo $TEMP_FILE
$AWK_SCRIPT -v is_prod=1 -v memory=$OPTARG $INPUT_FILE | uniq > $TEMP_FILE
else
print_usage
exit 4
fi
;;
*) # Otherwise print a usage message.
print_usage
exit 3
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
## Move the temporary file to the original one.
mv $TEMP_FILE $INPUT_FILE