#!/bin/sh
PATH=/bin:/usr/bin
ETC_PATH="/var/packages/Z-Push/etc"
. "$ETC_PATH"/package.cfg

if [ $TUNING_FPM -gt 0 ] && grep -q max_children=20 /etc/init/php-fpm.conf
then
   TS=$(date "+%Y.%m.%d-%H:%M:%S")
   cp /etc/init/php-fpm.conf /etc/init/php-fpm.conf.orig
   
   if !([ -f /etc/php/php-fpm.conf.orig ])
   then
      cp /etc/php/php-fpm.conf /etc/php/php-fpm.conf.orig
   fi

   /bin/sed -i -e "s~pm.min_spare_servers = 1~pm.min_spare_servers = 0~" /etc/php/php-fpm.conf
   
   # increase by x% in /etc/php/php-fpm.conf & /etc/init/php-fpm.conf
   if [ $TUNING_FPM == 50 ]
   then
      /bin/sed -i -e "s~pm.max_children = 20~pm.max_children = 30~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~pm.start_servers = 2~pm.start_servers = 3~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~respawn limit 5 10~respawn limit 7 15~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~int(max(20~int(max(30~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~max_children=20~max_children=30~" /etc/init/php-fpm.conf
      echo -e "Increased max_children by 50% to 30 and adjusted respawn limit in /etc/init/php-fpm.conf"
      echo -e "[$TS] Increased max_children by 50% to 30 and adjusted respawn limit in /etc/init/php-fpm.conf" >> $LOG
   fi
   if [ $TUNING_FPM == 75 ]
   then   
      /bin/sed -i -e "s~pm.start_servers = 2~pm.start_servers = 3~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~pm.max_children = 20~pm.max_children = 35~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~respawn limit 5 10~respawn limit 9 18~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~int(max(20~int(max(35~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~max_children=20~max_children=35~" /etc/init/php-fpm.conf
      echo -e "Increased max_children by 75% to 35 and adjusted respawn limit in /etc/init/php-fpm.conf"
      echo -e "[$TS] Increased max_children by 75% to 35 and adjusted respawn limit in /etc/init/php-fpm.conf" >> $LOG
   fi
   if [ $TUNING_FPM == 100 ]
   then   
      /bin/sed -i -e "s~pm.start_servers = 2~pm.start_servers = 4~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~pm.max_children = 20~pm.max_children = 40~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~respawn limit 5 10~respawn limit 10 20~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~int(max(20~int(max(40~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~max_children=20~max_children=40~" /etc/init/php-fpm.conf
      echo -e "Increased max_children by 100% to 40 and adjusted respawn limit in /etc/init/php-fpm.conf"
      echo -e "[$TS] Increased max_children by 100% to 40 and adjusted respawn limit in /etc/init/php-fpm.conf" >> $LOG
   fi
   if [ $TUNING_FPM == 150 ]
   then   
      /bin/sed -i -e "s~pm.start_servers = 2~pm.start_servers = 5~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~pm.max_children = 20~pm.max_children = 50~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~respawn limit 5 10~respawn limit 12 25~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~int(max(20~int(max(50~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~max_children=20~max_children=50~" /etc/init/php-fpm.conf
      echo -e "Increased max_children by 150% to 50 and adjusted respawn limit in /etc/init/php-fpm.conf"
      echo -e "[$TS] Increased max_children by 150% to 50 and adjusted respawn limit in /etc/init/php-fpm.conf" >> $LOG
   fi
   if [ $TUNING_FPM == 200 ]
   then   
      /bin/sed -i -e "s~pm.start_servers = 2~pm.start_servers = 6~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~pm.max_children = 20~pm.max_children = 60~" /etc/php/php-fpm.conf
      /bin/sed -i -e "s~respawn limit 5 10~respawn limit 15 30~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~int(max(20~int(max(60~" /etc/init/php-fpm.conf
      /bin/sed -i -e "s~max_children=20~max_children=60~" /etc/init/php-fpm.conf
      echo -e "Increased max_children by 200% to 60 and adjusted respawn limit in /etc/init/php-fpm.conf"
      echo -e "[$TS] Increased max_children by 200% to 60 and adjusted respawn limit in /etc/init/php-fpm.conf" >> $LOG
   fi
fi

if [ $TUNING_FPM -gt 0 ] && grep -q "pm = dynamic" /etc/php/php-fpm.conf
then
   if [ -f /etc/php/php-fpm.conf.new ]
   then
      cp /etc/php/php-fpm.conf.new /etc/php/php-fpm.conf
   else
      # change from dynamic to ondemand mode in /etc/php/php-fpm.conf 
      TS=$(date "+%Y.%m.%d-%H:%M:%S")
      if !([ -f /etc/php/php-fpm.conf.orig ])
      then
         cp /etc/php/php-fpm.conf /etc/php/php-fpm.conf.orig
      fi
      /bin/sed -i -e 's~dynamic~ondemand\npm.process_idle_timeout = 10s\npm.max_requests = 200~' /etc/php/php-fpm.conf

      cp /etc/php/php-fpm.conf /etc/php/php-fpm.conf.new
         
      echo -e "Changed from dynamic in ondemand mode in /etc/php/php-fpm.conf"
      echo -e "[$TS] Changed from dynamic in ondemand mode in /etc/php/php-fpm.conf" >> $LOG
   fi
fi
