#!/bin/bash

pear_scripts() {
  script_file="/etc/pear/pear_scripts/scripts.sh"
  if [ -f "${script_file}" ]; then
    chmod +x ${script_file} && ${script_file}
  fi
}

start_app() {
  pear_scripts
  /usr/sbin/pear_restart --install_path=/ &
  echo 3 > /proc/sys/net/ipv4/tcp_fastopen
  echo 1 > /proc/sys/net/ipv4/tcp_sack
  echo 1 > /proc/sys/net/ipv4/tcp_no_metrics_save
  echo 0 > /proc/sys/net/ipv4/tcp_slow_start_after_idle
  echo "4096 524288 524288" > /proc/sys/net/ipv4/tcp_rmem
  echo "4096 262144 524288" > /proc/sys/net/ipv4/tcp_wmem
}

stop_app() {
  killall -9 pear_restart
  sleep 1
  killall -9 pear_update
  killall -9 pear_monitor
  killall -9 pear_httpd
  killall -9 pear_s
  killall -9 socks_server
  killall -9 scan.sh
}

case "$1" in
  start)
    stop_app
    start_app
    ;;
  stop)
    stop_app
    ;;
  reload|restart|force-reload)
    stop_app
    start_app
    ;;
  *)
    echo "$0 start|stop|reload|restart"
    exit 1
esac

exit 0
