#!/bin/bash
#
#chkconfig: - 99 01
#description: controls Frontier services

if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
else
    PATH="/sbin:/usr/sbin:/bin:/usr/bin"
    export PATH
fi

MYNAME="`basename $0`"

if [ -f /etc/sysconfig/$MYNAME ]; then
    . /etc/sysconfig/$MYNAME
fi

FDLIMIT="`ulimit -n`"
if [ $FDLIMIT -lt 4096 ]; then
    FDLIMIT=4096
    ulimit -n $FDLIMIT
fi
let BACKLOGMAX=$FDLIMIT/4
if [ "`sysctl -n net.core.somaxconn`" -lt $BACKLOGMAX ]; then
    sysctl -q -w net.core.somaxconn=$BACKLOGMAX
fi

FRONTIER_USER=squid
case "$1" in
    *start)
	# require user home directory to exist because otherwise cron will
	#   fail to run user's log cleaning jobs on RHEL6 
	eval FRONTIER_HOME=~${FRONTIER_USER}
	if [ ! -d $FRONTIER_HOME ]; then
	    echo "ERROR: $FRONTIER_USER login home directory $FRONTIER_HOME does not exist" >&2
	    exit 1
	fi
	# make sure /var/run/squid exists, because on some systemd systems
	#   it might not yet be there
	if [ ! -d /var/run/squid ]; then
	    mkdir /var/run/squid
	    chown $FRONTIER_USER /var/run/squid
	    chmod 755 /var/run/squid
	fi
	;;
esac

runuser -s /bin/bash $FRONTIER_USER /usr/sbin/fn-local-squid.sh $1
