#!/usr/local/bin/cbsd
#v10.1.0
MYARG=""
MYOPTARG=""
MYDESC="Update base jail"
CBSDMODULE="sys"
ADDHELP=""

. ${subr}
. ${system}
. ${strings}
. ${tools}

init $*

readconf baseupdate.conf

generate_freebsd_config()
{
	/bin/cat << EOF > "${1}"
KeyPrint ${keyprint}
ServerName ${servername}
Components ${components}
IgnorePaths
IDSIgnorePaths /usr/share/man/cat
IDSIgnorePaths /usr/share/man/whatis
IDSIgnorePaths /var/db/locate.database
IDSIgnorePaths /var/log
UpdateIfUnmodified /etc/ /var/ /root/ /.cshrc /.profile
MergeChanges /etc/ /boot/device.hints
EOF
}

update_base()
{
	local _i
	sqlfile="local"

	_sql="SELECT platform,name,arch,targetarch,ver FROM bsdbase"
	cbsdsql ${sqlfile} ${_sql} | while read _platform _name _arch _targetarch _ver; do
		base="${_name}_${_arch}_${_targetarch}_${_ver}"
		basepath=${workdir}/base/${base}
		${ECHO} ${MAGENTA}"Updating ${basepath}"${NORMAL}
		case "${platform}" in
			HardenedBSD)
				/usr/sbin/hbsd-update -r "${basepath}"
				;;
			FreeBSD)
				updateconf=$( /usr/bin/mktemp )
				trap "/bin/rm -f ${update_conf}" HUP INT ABRT BUS TERM EXIT
				generate_freebsd_config "${updateconf}"
				FBSD_UPDATE="/usr/sbin/freebsd-update -f ${updateconf} -b ${basepath} --not-running-from-cron"
				/usr/bin/env PAGER=/bin/cat ${FBSD_UPDATE} fetch install
				;;
		esac
	done
}

sqldelimer=" "
update_base
