#!/usr/local/bin/cbsd
#v10.1.2
CBSDMODULE="sys"
MYARG=
MYOPTARG="arch basename destdir distribution nice name sourcedir target_arch ver"
MYDESC="Create default FreeBSD distribution in .txz files for /usr/freebsd-dist"
ADDHELP="

${H3_COLOR}Description${N0_COLOR}:

Create arhives from base/kernel as base.txz/kernel.txz/src.txz/ports.txz. This can 
be used, for example, to generate a FreeBSD distribution (e.g.: via 'cbsd jail2iso' 
script), or to distribute your base and kernel through a regular tar/gzip file.

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}arch=${N0_COLOR}         - <name>, use non-native architectures;
 ${N2_COLOR}basename=${N0_COLOR}     - Prefix for directory name, can be used for jail as alternative base.
 ${N2_COLOR}distribution=${N0_COLOR} - space-separated targets, e.g: \"base kernel\" (default). Can be:
                 - base   - create base.txz from ~cbsd/basejail/base_ARCH_TARGETARCH_VER;
                 - kernel - create kernel.txz from ~cbsd/basejail/PLATFORM-kernel_NAME_ARCH_VER;
                 - ports   - create ports.txz from /usr/ports;
                 - src     - create src.txz from ~cbsd/src/src_VER;
 ${N2_COLOR}destdir${N0_COLOR}       - <path> - alternative DESTDIR= path instead of ~cbsd/basejail/;
 ${N2_COLOR}nice=${N0_COLOR}         - num: overwrite default nice: '19'.
 ${N2_COLOR}name=${N0_COLOR}         - name of the kernel, default: 'GENERIC';
 ${N2_COLOR}sourcedir=${N0_COLOR}    - use alternative source dir instead of ~cbsd/basejail/base_XXXX;
 ${N2_COLOR}target_arch=${N0_COLOR}  - <name>, build non-native target arch.
 ${N2_COLOR}ver=${N0_COLOR}          - set version: '13.0', '13.1';
                 when version without minor version: e.g:
                 '13' or '12' - you will receive RELENG (stable=1), not release;

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd mkdistribution distribution=\"base\"
 # cbsd mkdistribution distribution=\"base\" sourcedir=/tmp/base-in-pkg
 # cbsd mkdistribution name=CBSD

${H3_COLOR}See also${N0_COLOR}:

  cbsd buildworld --help
  cbsd installworld --help
  cbsd srcup --help
  cbsd bases --help
  cbsd sources --help
  cbsd mkdistribution --help
  cbsd baseupdate --help
  cbsd install-pkg-world --help

"


. ${subrdir}/nc.subr
readconf buildworld.conf
sourcedir=
. ${cbsdinit}
. ${subrdir}/universe.subr

over="${ver}"
oarch="${arch}"
[ -z "${nice}" ] && nice=19
[ -z "${idprio}" ] && idprio=31		# dead/lock? see man BUGS

# By default ver=current version
. ${subrdir}/build.subr

# auto-detect for stable/release
strpos --str="${ver}" --search="."
pos=$?
if [ ${pos} -eq 0 ]; then
	stable=1
	ostable=1
else
	stable=0
	ostable=0
fi

init_base()
{
	if [ -n "${sourcedir}" ]; then
		[ ! -d "${sourcedir}" ] && err 1 "${W1_COLOR}${CBSD_APP} error: ${N1_COLOR} no such srcdir: ${N2_COLOR}${sourcedir}${N0_COLOR}"
		SRC_DIR="${sourcedir}"
		return 0
	fi

	exclude_files=""

	init_basedir

	exclude_files_lib32="/libexec/ld-elf32.so.1|\
/usr/bin/ldd32|\
/usr/lib32/.*|\
/usr/libexec/ld-elf32.so.1"

	exclude_files_rescue="/rescue/.*"

	exclude_files="^${BASE_DIR}(\
${exclude_files_lib32}|\
${exclude_files_rescue}|\
)"

	SRC_DIR="${BASE_DIR}"
}

init_ports()
{
	SRC_DIR="/usr/ports"
}

init_kernel()
{
	init_kerneldir
	SRC_DIR="${KERNEL_DIR}"
}

init_src()
{
	init_srcdir
}


## MAIN
if [ -n "${destdir}" ]; then
	DST_DIR="${destdir}"
	destdir=		# unset before init_{kerneldir,srcdir,basedir} func
else
	DST_DIR="${tmpdir}"
fi

[ ! -d "${DST_DIR}" ] && ${MKDIR_CMD} -p ${DST_DIR}

init_target_arch

if [ -z "${distribution}" ]; then
	DISTRIBUTION="base kernel"
else
	DISTRIBUTION="${distribution}"
fi

#set -x xtrace

for i in ${DISTRIBUTION}; do
	init_${i}

	if [ ! -d "${SRC_DIR}" ]; then
		${ECHO} "${N1_COLOR}No such source dir for ${i}: ${N2_COLOR}${SRC_DIR}${N0_COLOR}"
		continue
	fi

	${ECHO} "${N1_COLOR}WIP: ${i}, source: ${N2_COLOR}${SRC_DIR}${N0_COLOR}"
	cd ${SRC_DIR}

	if [ -n "${name}" ]; then
		dstname="${i}-${name}.txz"
	else
		dstname="${i}.txz"
	fi

	${NICE_CMD} -n ${nice} ${TAR_CMD} -cJf ${DST_DIR}/${dstname} .
	${ECHO} "${N1_COLOR} * ${DST_DIR}/${dstname}: ${N2_COLOR}done${N0_COLOR}"
done

exit 0
