#!/bin/bash
# squid.sh : a simple squid installer by Brain 0verride
# https://twitter.com/brain0verride
# https://www.christophe-casalegno.com

replacer='sed' 				# command used for replacement 
repoptions='-i' 			# options for the replacer
downloader='apt -y -f -d install'
installer='apt -y -f install' 

sources_list='/etc/apt/sources.list.d'
auto='debconf-set-selections'
export DEBIAN_FRONTEND=noninteractive
squid_conf='/etc/squid/squid.conf'

needps='vim lsb-release apt-transport-https ca-certificates wget curl lynx dirmngr software-properties-common'

function deploy()
{
  installer="$1"
  packs="$2"

  echo "Installing packages : $packages..."
  $downloader $packs
  $installer $packs
}

function sourcesupdate()
{
  apt-get update
}

function writefile()

{
option=$1
file2write="$2"
contentfile="$3"

if [ "$option" = "add" ]
then

cat >>$file2write <<EOF
$contentfile
EOF

fi

if [ "$option" = "create" ]
then

cat >$file2write <<EOF
$contentfile
EOF

fi
}

sourcesupdate
apt -y upgrade

deploy "$installer" "$needps"

sourcesupdate

deploy "$installer" "squid apache2-utils"

content_squid='
# StackX configuration by ScalarX Technology - https://scalarx.com

auth_param basic program /usr/lib/squid3/basic_ncsa_auth /etc/squid/users
auth_param basic children 5
auth_param basic realm ScalarX Proxy-Caching Server
auth_param basic credentialsttl 48 hours

acl Users proxy_auth REQUIRED
acl SSL_ports port 443
acl CONNECT method CONNECT

http_access allow Users
forwarded_for off
http_access allow localhost 
http_port 3128

coredump_dir /var/spool/squid3

refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .             0	20%	4320 
'

writefile "create" "$squid_conf" "$content_squid" # write squid configuration





