#!/usr/local/bin/cbsd
#v13.1.16
CBSDMODULE="sys"
MYARG=""
MYOPTARG="arch basename destdir qemu register_basename stable target_arch ver"
MYDESC="Install base-in-package FreeBSD base"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

Install FreeBSD base files to ~cbsd/basejail/ directory after 'cbsdbuildworld'.

${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}destdir${N0_COLOR}            - <path> - alternative DESTDIR= path instead of ~cbsd/basejail/.
 ${N2_COLOR}qemu${N0_COLOR}              - '0' - copy (default) or '1' - do not copy the 
                     qemu emulator into base /bin;
 ${N2_COLOR}register_basename=${N0_COLOR} - <name> register with alternative/custom basename.
 ${N2_COLOR}stable=${N0_COLOR}            - 1 for RELENG_X.
 ${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 installworld
 # cbsd installworld destdir=/tmp/testbase

${H3_COLOR}See also${N0_COLOR}:

  cbsd buildworld --help
  cbsd bases --help
  cbsd world --help

"

. ${subrdir}/nc.subr
readconf buildworld.conf
register_basename=
qemu=
. ${cbsdinit}

if [ -n "${qemu}" ]; then
	oqemu="${qemu}"
else
	qemu=1
fi

. ${subrdir}/universe.subr

over="${ver}"
oarch="${arch}"

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

init_notify
init_target_arch
init_srcdir			# TODO: distribution in etcupdate tgz! not for stable version
init_supported_arch

if [ -n "${register_basename}" ]; then
	init_basedir -b ${register_basename}
else
	init_basedir
fi

#[ -x "${BASE_DIR}/bin/sh" ] &&  err 1 "${N1_COLOR}${CBSD_APP}: not empty, base already exist: ${N2_COLOR}${BASE_DIR}${N0_COLOR}"

[ ! -d "${BASE_DIR}" ] && ${MKDIR_CMD} -p ${BASE_DIR}
makelock ${BASE_DIR_LOCKFILE} "cleanup_bases"

# make base lock
echo $$ > ${BASE_DIR_LOCKFILE}

if [ -n "${register_basename}" ]; then
	_basename_args="basename=\"${register_basename}\""
else
	_basename_args=
fi

register_base arch=${arch} ver=${ver} source="install-pkg-world" auto_baseupdate=0 ${_basename_args}

base_status_is_maintenance_hard

# map amd64 -> x86_64 for DFLY
if [ "${platform}" = "DragonFly" ]; then
	err 1 "${N1_COLOR}${CBSD_APP}: base-in-pkg not supported yet for ${N2_COLOR}${platform}${N0_COLOR}"
	case "${arch}" in
		amd64)
			build_arch="x86_64"
			build_target_arch="x86_64"
			;;
	esac
else
	build_arch="${arch}"
	build_target_arch="${target_arch}"
fi

set -o errexit
cbsdlogger NOTICE ${CBSD_APP}: install-pkg-world started: ${MAKE_CMD} -C ${SRC_DIR} installworld DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}

${MKDIR_CMD} -p ${BASE_DIR}/usr/share/keys/
/usr/local/bin/rsync -avz /usr/share/keys/ ${BASE_DIR}/usr/share/keys/

readconf ${platform}-base_in_pkg.conf
readconf ${platform}-base_in_pkg-${ver}.conf

# we need to use system/global path
[ ! -d ${BASE_DIR}/etc/pkg ] && ${MKDIR_CMD} -p ${BASE_DIR}/etc/pkg
${CAT_CMD} > ${BASE_DIR}/etc/pkg/base-in-pkg.conf <<EOF
base-in-pkg: {
	conservative_upgrade: no,
	priority: 10
	url: "${base_in_pkg_repo_url}",
	mirror_type: "none",
	enaled: yes
}
EOF

# save/restore old ?
[ ! -r ${BASE_DIR}/etc ] && ${MKDIR_CMD} ${BASE_DIR}/etc
${CAT_CMD} > ${BASE_DIR}/etc/resolv.conf <<EOF
nameserver 8.8.8.8              # CBSD makeresolv function
nameserver 8.8.4.4              # CBSD makeresolv function
EOF

env SIGNATURE_TYPE=none ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -r ${BASE_DIR} update
env SIGNATURE_TYPE=none ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -r ${BASE_DIR} install -y FreeBSD-runtime
env SIGNATURE_TYPE=none ASSUME_ALWAYS_YES=yes /usr/sbin/pkg -r ${BASE_DIR} install -y ${base_in_pkg_list_active}

${RM_CMD} -f ${BASE_DIR}/usr/local/etc/pkg/repos/pkg.convectix.com.conf
${CP_CMD} -a /usr/sbin/pkg ${BASE_DIR}/usr/sbin/

# not in PKG ?
${CP_CMD} -a /sbin/rcorder ${BASE_DIR}/sbin/

#${MAKE_CMD} -C ${SRC_DIR} installworld DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}

cbsdlogger NOTICE ${CBSD_APP}: install-pkg-world ended: ${MAKE_CMD} -C ${SRC_DIR} installworld DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}

# install: target directory `/usr/jails/basejail/base_amd64_amd64_14.0/etc/mail' does not exist
for i in /etc/mail /boot; do
	[ ! -d ${BASE_DIR}${i} ] && ${MKDIR_CMD} -p ${BASE_DIR}${i}
done

if [ "${platform}" = "DragonFly" ]; then
	cbsdlogger NOTICE ${CBSD_APP}: distribution started: ${MAKE_CMD} -C ${SRC_DIR}/etc distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
	${MAKE_CMD} -C ${SRC_DIR}/etc distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
	cbsdlogger NOTICE ${CBSD_APP}: distribution ended: ${MAKE_CMD} -C ${SRC_DIR}/etc distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
else
	cbsdlogger NOTICE ${CBSD_APP}: distribution started: ${MAKE_CMD} -C ${SRC_DIR} distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
	${MAKE_CMD} -C ${SRC_DIR} distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
	cbsdlogger NOTICE ${CBSD_APP}: distribution ended: ${MAKE_CMD} -C ${SRC_DIR} distribution DESTDIR=${BASE_DIR} TARGET=${build_arch} TARGET_ARCH=${build_target_arch}
fi

set +o errexit

[ $? -ne 0 ] && err 1 "${N1_COLOR}Installworld error${N0_COLOR}"
[ -n "${oqemu}" ] && qemu="${oqemu}"
preparebase dst=${BASE_DIR} emulator=${emulator} qemu="${qemu}"

[ ! -f ${BASE_DIR}/bin/sh ] && err 1 "${N1_COLOR}No such /bin/sh in ${BASE_DIR}. Try to rebuild world${N0_COLOR}"

#idx platform name arch ver stable elf date
baseelf=
baseelf=$( ${miscdir}/elf_tables --ver ${BASE_DIR}/bin/sh 2>/dev/null )

[ -z "${baseelf}" ] && baseelf="0"
register_base arch=${arch} ver=${ver} target_arch="${target_arch}" source="install-pkg-world" auto_baseupdate=0 ${_basename_args}

${RM_CMD} -f ${BASE_DIR_LOCKFILE}

exit 0
