#!/bin/sh
#
# (c) Lorenzo Canovi (KUBiC Labs, CH) <packager@kubiclabs.com>
# for copyright see /usr/share/doc/ku-base/copyright
#
# CMDSTR="ku-rebuild-var-tmpfs v1.8 (2023-03-20)"
#
### BEGIN INIT INFO
# Provides:          ku-rebuild-var-tmpfs
# Required-Start:    mountall-bootclean
# Required-Stop:
# Should-Start:      
# Should-Stop:
# Default-Start:     S
# Default-Stop:      
# Short-Description: rebuild missing dir/files under /var
# Description:       rebuild missing dir/files under /var
### END INIT INFO
#
# this script try to fixes /var missing dirs and files
# (always usefull, but is mandatory on machines where /var is on
# a tmpfs, like alix boxes)
#
# there is only "start" state
#
tag="[ku]"

set -e -u

. /lib/lsb/init-functions

case "${1:-}" in
	start)	;;
	*)	exit 0 ;;
esac

dirs="
	/tmp,root:root,1777
	/var/tmp,root:root,1777

	/var/log
	/var/log/apt
	/var/log/apache2,root:root
	/var/log/iptraf
	/var/log/openvpn,root:root
	/var/log/fsck
	/var/log/webmin
	/var/log/samba,root:root
	/var/log/proftpd,root:root

	/var/cache/debconf
	/var/cache/apt
	/var/cache/apt/archives
	/var/cache/apt/archives/partial
	/var/cache/ldconfig
	/var/cache/nscd

	/var/run/network
"



list_files()
{
	local file=
	local conf=

	echo "/var/log/dmesg"
	echo "/var/log/proftpd/proftpd.log"
	echo "/var/log/wtmp,root:utmp,664"
	echo "/var/log/lastlog"
	echo "/var/log/syslog"
	echo "/var/log/messages"

	# add files from [r]syslog configs
	#
	for conf in $(ls /etc/syslog.conf /etc/rsyslog.d/*.conf 2>/dev/null || :)
	do
    		cat $conf | sed \
			-e 's/#.*//' \
			-e 's#-/#/#' \
			-e "s/[ ,	][ ,	]*/\n/g" \
		| while read file
    		do
			case "$file" in
				/var/log/*) ;;
				*) continue ;;
			esac
			echo $file
    		done
	done
}

makedir()
{
	local dir=$1
	local owner=$2
	local perms=$3
	local path=
	local comp=
	local dirname=
	local oifs="$IFS"

	IFS="/"
	for comp in $(echo "$dir" | sed -e 's#^/##')
	do
		path="$path/$comp"

		[ -d "$path" ] || {
			$DEBUG && echo "D> mkdir '$path'"
			mkdir "$path" || {
				IFS="$oifs"
				return $?
			}
		}
		[ "$path" != "$dir" ] && continue

		dirname=$(dirname "$path")

		$DEBUG && {
			local pstat=$(stat --format '%A %U:%G' "$path")
		}
		if [ "$owner" != "" ]
		then
			chown $owner "$path"
		else
			chown --reference "$dirname" "$path"
		fi
		if [ "$perms" != "" ]
		then
			chmod $perms "$path"
		else
			chmod --reference "$dirname" "$path"
		fi
	done
	$DEBUG && {
		local nstat=$(stat --format '%A %U:%G' "$path")
		[ "$nstat" != "$pstat" ] && {
			echo "D> now:  $pstat $path"
			echo "D> -->   $nstat"
		}
	}

	IFS="$oifs"
	return 0
}


# (MAIN)

DEBUG=${DEBUG:-"false"}

$DEBUG && {
	log_action_begin_msg() { echo "$@"; }
	log_action_end_msg() { :; }
}

log_action_begin_msg "$tag rebuilding /var structure ..."



# fix up things

for dir in $dirs
do
	owner=
	perms=

	echo "$dir" | grep -q "," && {
		perms=$(echo "$dir" | cut -d',' -f3)
		owner=$(echo "$dir" | cut -d',' -f2)
		dir=$(echo "$dir" | cut -d',' -f1)
	}
	makedir "$dir" "$owner" "$perms"
done


for file in $(list_files | sort -u)
do
	owner=
	perms=

	echo "$file" | grep -q "," && {
		perms=$(echo "$file" | cut -d',' -f3)
		owner=$(echo "$file" | cut -d',' -f2)
		file=$(echo "$file" | cut -d',' -f1)
	}

	dir=$(dirname "$file")
	[ -d "$dir" ] || makedir "$dir" "$owner" ""

	perms=${perms:-"640"}

	case $file in
	  /var/log/*)	owner=${owner:-"syslog:adm"} ;;
	  *)		owner=${owner:-"root:root"} ;;
	esac

	[ -f "$file" ] || {
		$DEBUG && " creating '$file' $owner $perms"
		: >"$file"
	}
	chown $owner "$file"
	chmod $perms "$file"
done

log_action_end_msg 0

exit 0
