#!/usr/local/bin/cbsd
#v11.1.0
MYARG="file hosts ips"
MYOPTARG="template"
MYDESC="Simple manage records in hosts file"
CBSDMODULE="sys"
ADDHELP="file = path to hosts file\n\
hosts= host entry, e.g: jail1.my.domain\n\
ips= IP address for host entry\n\
template= (optional) alternative template marker\n"

. ${subrdir}/nc.subr

. ${cbsdinit}

. ${system}

# sample: cbsd mkjhosts file=/hosts ips="192.168.0.11" hosts="test" template="TEST"

[ ! -r "${file}" ] && err 1 "${N1_COLOR}mkjhosts: no ${N2_COLOR}${file}${N0_COLOR}"
[ -z "${template}" ] && template="CBSD autotpl for mkhostsfile"

tmpfile="${ftmpdir}/hosts.$$"
${CP_CMD} -a ${file} ${tmpfile}
${GREP_CMD} -v "${template}" ${tmpfile} > ${file}
echo "${ips} ${hosts} # ${template}" >> ${file}
${RM_CMD} -f ${tmpfile}
