#!/bin/bash

. ${TOOLKIT}-functions.sh

# BUILD SQUID CONFIG FILE

orig=`filepath squid.conf`
outfile="squid.conf.tmp"


# build acl permission (store in temp db)
#
[ "X$(jtconf squid.acl_defines)" != "X" ] && {
	putwarning "DEPRECATED: squid.acl_defines" \
		"this definition will be ignored, and replaced by a list built" \
		"on-the-fly using perms.* definitions, using module temporary" \
		"local db $MODLOCALDB"
}
cp /dev/null $MODLOCALDB
echo "[squid]" >>$MODLOCALDB
echo "  acl_defines	^^BLOCK^^" >>$MODLOCALDB
for ip in $(jtconf perms.admins_iplist)
do
	echo "	acl net_manager src $ip" >>$MODLOCALDB
done
for ip in $(jtconf perms.applications_iplist)
do
	echo "	acl net_allowed src $ip" >>$MODLOCALDB
done
echo "^^BLOCK^^" >>$MODLOCALDB


# temp env entries for parsing
#
export tmp_anon_conf=


# anonymous proxing
#
getconfirm squid.anonymous && {
	case $DISTRIB_RELEASE in
	  ##9*)	file="anon.conf-squid2" ;;
	  *)	file="anon.conf" ;;
	esac
	echo "  enabling ANONYMOUS proxying (add $file)"
	path=`filepath $file` || exit_err 1 "can't find $file"
	tmp_anon_conf=`cat $path`
}

jtconf-parse --simple $orig >$outfile




# remove squid2 if installed
#
dpkg -l squid | grep -q '^ii  squid  *2\.' && {
	remove_pkgs squid
}

[ -d /etc/squid3 ] || {
	mkdir /etc/squid3
}

[ ! -L /etc/squid ] && {
	cd /etc
	[ -d squid ] && {
		putwarning "/etc/squid" \
			"squid now uses /etc/squid3 directory instead of /etc/squid;" \
			"/etc/squid will be renamed to /etc/squid.old and a symlink" \
			"will be created squid -> squid3"
		mv squid squid.old || exit $?
	}
}
updatelink /etc/squid3 /etc/squid
updatelink /var/log/squid3 /var/log/squid


exit 0
