#!/bin/bash

. ${TOOLKIT}-functions.sh

# 2016.11.21
# 2.4 uses .conf extension for sites (fucking prjadmins, you don't have
# anything better to do than implement useless but disrupting design changes?)
#
a2_version=$(dpkg -l apache2 | grep "^ii " | sed -e 's/^ii  apache2 *//' -e 's/ .*//')
conf_ext=

case $a2_version in
  2.2.*)
    purgefiles \
	/etc/apache2/sites-*/ku-auto-virtual.conf
    ;;
  2.4.*)
    purgefiles \
	/etc/apache2/sites-*/ku-auto-virtual
     
    conf_ext=".conf"
    ;;
esac
installfile ku-auto-virtual$conf_ext /etc/apache2/sites-available/ root:root 440


# SITES
# - enables vhosts default sites, disable non-vhost ones
#
apache2_enable_site ku-auto-virtual$conf_ext		|| exit $?
apache2_enable_site ku-default-ssl-vhosts$conf_ext	|| exit $?
apache2_disable_site ku-default$conf_ext		|| exit $?
apache2_disable_site ku-default-ssl$conf_ext		|| exit $?

# - disables standard /usr/lib/cgi-bin directory, vhosts haves own one
#
apache2_disable_conf serve-cgi-bin || :

# create default serverdir if not exists
#
docroot=$(jtconf apache.autovirtual_root)	|| exit $?
myname=$(uname -n)
myname_s=$(uname -n | sed -e 's/\..*//')

# this can contain unbound vars, so we disable check
set +u
. /etc/apache2/envvars
set -u

# ... and link the default dir to this hostname
#
cd $docroot
other_names=$(jtconf apache.default_names 2>/dev/null || :)
customer=$(jtconf customer.shortname)	|| exit_missing_define customer.shortname

for name in $myname $myname_s $other_names
do
	updatelink default $name
done
cd $docroot/default
rm -f favicon.ico
ln -s ../../../images_$customer/favicon.ico . || :
cd $RUNDIR


# system docs / apc status script
#
file=/usr/share/doc/php-apc/apc.php.gz
dest=$(jtconf www.docs)/apc-status.php

# 2015.07.13
#	removed apc stuffs, causes apache2 segfault 11
#
purgefiles $dest

#if getconfirm apache.allow_sysdoc
#then
	#[ -f $file ] && {
		#gzip -d <$file >./apc-status.php
		#installfile ./apc-status.php $dest $APACHE_RUN_USER:$APACHE_RUN_GROUP 440
	#}
#else
	#purgefiles $dest
#fi




# mysql autodump task
#
file=ku-mysql-autodump.cfg
dest=/etc/$file
fpath=$(filepath $file)
[ -f $dest ] || {
	installfile $fpath $dest root:root 600 || exit $?
}

# sudo entry for running ku-netdiscover helper
#
sudofile="/etc/sudoers"
command="/usr/sbin/ku-netdiscover"

grep -q "^$APACHE_RUN_USER[ ,	].*$command$" $sudofile || {
	echo "  patching $sudofile for $command helper"
	echo "" >>$sudofile
	echo "#[ku] added by kusa on $(date)" >>$sudofile
	echo "$APACHE_RUN_USER	ALL=NOPASSWD: $command" >>$sudofile
}



#------------------------------
# awstats
#------------------------------

tdir="/etc/awstats"
vdir="$(jtconf path.wwwdir)/vhosts"
donefile=${MODRUNDIR}/awstats.done

:>$donefile

[ -d $tdir ] && {
	cd ${MODRUNDIR}

	# all the config files buildup is made in a subshell, to avoid
	# environment pollution
	#
	(

	awstats_template=$(filepath "awstats-conf-template")

	# site enabled stats are subsections of [awstats]
	#
	for site in $(jtconf --list awstats. 2>/dev/null)
	do
		printf "  %-30s  " $site

		tmp_site_domain=$(	jtconf $site.site_domain) || {
			echo "skip (site_domain not defined)"
			continue
		}
		[ -d "$vdir/$tmp_site_domain" ] || {
			echo "skip (dir '$vdir/$tmp_site_domain' not exists)"
			continue
		}
		echo "$tmp_site_domain"

		tmp_dir_data="$vdir/$tmp_site_domain/awstats-data"
		tmp_logo_link="http://$tmp_site_domain"

  		tmp_logfile=$(		jtconf $site.logfile) || continue
  		tmp_host_aliases=$(	jtconf $site.host_aliases) || continue
  		tmp_dns_lookup=$(	jtconf $site.dns_lookup) || continue
  		tmp_skip_files=$(	jtconf $site.skip_files) || continue
  		tmp_skip_user_agents=$(	jtconf $site.skip_user_agents) || continue
  		tmp_skip_hosts=$(	jtconf $site.skip_hosts) || continue
  		tmp_logo=$(		jtconf $site.logo) || continue

		tmp_statsdir="$vdir/$tmp_site_domain/docs/stats"

		export tmp_site_domain tmp_dir_data tmp_logo_link tmp_host_aliases \
			tmp_dns_lookup tmp_logo tmp_logfile \
			tmp_skip_files tmp_skip_user_agents tmp_skip_hosts \
			tmp_statsdir

		tmp_host_aliases=${tmp_host_aliases:-$tmp_site_domain}

		outfile="$tdir/awstats.$tmp_site_domain.conf"

		installfile $awstats_template $outfile root:root 440

		owner=$(stat --format "%U:%G" $vdir)

		create_dir --fixperms $tmp_statsdir $owner 2755

		[ -f $tmp_statsdir/.htaccess ] || {
			installfile awstats.htaccess $tmp_statsdir/.htaccess $owner 660
		}
		# default htaccess user
		defaccess=$(jtconf awstats_program.htaccess_default_user 2>/dev/null) && {
			defpass=$(jtconf awstats_program.htaccess_default_password 2>/dev/null) && {
				[ "$defaccess" != "" -a "$defpass" != "" ] && {
					echo -n "    resetting default htaccess user '$defaccess' ... "
					newflag=
					[ -f $tmp_statsdir/.htpasswd ] || newflag="c"
					htpasswd -bs$newflag $tmp_statsdir/.htpasswd $defaccess $defpass
				}
			}
		}

		# permsfix
		chown $owner $(ls $tmp_statsdir/.ht{access,passwd} 2>/dev/null || :)
		chmod 660 $(ls $tmp_statsdir/.ht{access,passwd} 2>/dev/null || :)

		basename "$outfile" >>$donefile
	done

	)

	# awstats cleanup
	#
	echo "  old config files cleanup ..."
	do_not_edit=$(jtconf common.do_not_edit)	# tag used to identify our files
	cd $tdir
	for file in $(ls *.*.conf 2>/dev/null || :)
	do
		if grep -q "^$file$" $donefile
		then
			echo "    keep (ok)     $file"
		else
			if fgrep -q "$do_not_edit" $file
			then
				echo "    purged        $file"
				rm -f $file
			else
				echo "    keep (custom) $file"
			fi
		fi
	done
}

# install local robots file if not exists
#
file="/etc/awstats/robots.local.pm"
[ -f $file ] || {
	installfile robots.local.pm $file root:root 440
}



# cleanup obsolete files
#
purgefiles /root/bin/awstats-vhosts /etc/cron.d/awstats \
	/etc/cron.d/kuwiki-updatepage-server-infos


$SOMETHING_CHANGED && {
	reload_service apache2 apache2 || exit 1
}
exit 0
