From 34a77f7822126263fed5c72aedaa65b58fee94ac Mon Sep 17 00:00:00 2001
From: P. J. McDermott <pj@pehjota.net>
Date: Mon, 07 Dec 2015 12:07:15 -0500
Subject: Merge branch 'feature/use-shld'

---
(limited to 'src/cmd')

diff --git a/src/cmd/build.sh b/src/cmd/build.sh
new file mode 100644
index 0000000..8d12691
--- /dev/null
+++ b/src/cmd/build.sh
@@ -0,0 +1,152 @@
+# "build" command
+#
+# Copyright (C) 2014  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_build_root=
+cmd_build_pkg_dir=
+cmd_build_build_deps=
+
+cmd_build_main()
+{
+	local root=
+	local dev=
+	local opkbuild_optstring=
+	local arch=
+	local plat=
+	local first_arg=
+	local arg=
+
+	if [ ${#} -lt 2 ]; then
+		print_cmd_usage 'build' >&2
+		exit 1
+	fi
+
+	root="${1}"
+	shift 1
+
+	dev=''
+	if is_block "${root}"; then
+		dev="${root}"
+		root="$(block_mount "${dev}")"
+	fi
+
+	cmd_build_root="${root}"
+
+	opkbuild_optstring="$(cat "${root}/usr/share/opkbuild/optstring")"
+	arch="$(cat "${root}/etc/proteanos_arch")"
+	plat="$(cat "${root}/etc/proteanos_plat")"
+	while getopts "${opkbuild_optstring}" opt 2>/dev/null; do
+		case "${opt}" in
+			a) arch="${OPTARG}";;
+			p) plat="${OPTARG}";;
+		esac
+	done
+
+	first_arg=true
+	cmd_build_pkg_dir=''
+	for arg in "${@}"; do
+		if ${first_arg}; then
+			set --
+			first_arg=false
+		else
+			set -- "${@}" "${cmd_build_pkg_dir}"
+		fi
+		cmd_build_pkg_dir="${arg}"
+	done
+
+	. "${root}/etc/os-release"
+	profile_set "${ID}"
+
+	if ! [ -d "${cmd_build_pkg_dir}" ]; then
+		error 2 "$(get_msg 'cmd_build_not_a_dir')" \
+			"${cmd_build_pkg_dir}"
+	fi
+	package_init "${cmd_build_pkg_dir}"
+	package_set_substvars "${arch}" "${plat}"
+
+	session_begin "${root}" "${cmd_build_pkg_dir}" cmd_build_fini false
+
+	cmd_build_build_deps="$(package_get_build_deps "${arch}" "${plat}")"
+	if [ "x${cmd_build_build_deps}" != 'x' ]; then
+		cmd_build_make_deps_pkg
+		session_exec opkg install ../builddeps.opk
+	fi
+
+	session_exec opkbuild "${@}"
+
+	cmd_build_fini
+
+	session_end
+
+	if [ "x${dev}" != 'x' ]; then
+		block_umount "${root}"
+	fi
+}
+
+cmd_build_make_deps_pkg()
+{
+	local pkg_dir=
+
+	pkg_dir="${cmd_build_root}$(session_dir)/builddeps"
+	mkdir -p "${pkg_dir}/control" "${pkg_dir}/data"
+	printf '2.0\n' >"${pkg_dir}/debian-binary"
+	cat >"${pkg_dir}/control/control" <<-EOF
+		Package: prokit-builddeps-$(session_id)
+		Source: prokit
+		Version: 1.0
+		Architecture: all
+		Platform: all
+		Depends: ${cmd_build_build_deps}
+		Description: Build dependencies metapackage generated by prokit
+		EOF
+	(cd "${pkg_dir}/data"; tar -czf ../data.tar.gz .)
+	(cd "${pkg_dir}/control"; tar -czf ../control.tar.gz .)
+	(cd "${pkg_dir}"; tar -czf ../builddeps.opk \
+		'debian-binary' 'data.tar.gz' 'control.tar.gz')
+	rm -Rf "${pkg_dir}"
+}
+
+cmd_build_fini()
+{
+	local session_dir=
+	local f=
+
+	session_dir="$(session_dir)"
+
+	if [ "x${cmd_build_build_deps}" != 'x' ]; then
+		session_exec opkg --autoremove remove \
+			prokit-builddeps-$(session_id)
+		rm "${cmd_build_root}${session_dir}/builddeps.opk"
+		# Hack to avoid this code branch if the function is called again
+		# in response to a signal.
+		cmd_build_build_deps=''
+	fi
+
+	for f in "${cmd_build_root}${session_dir}/"*; do
+		[ -e "${f}" ] || continue
+		[ "x${f##*/}" = 'xwd' ] && continue
+		mv "${f}" "${cmd_build_pkg_dir}/.."
+	done
+}
+
+cmd_build_register()
+{
+	register_cmd 'build'
+}
+__init cmd_build_register
diff --git a/src/cmd/help.sh b/src/cmd/help.sh
new file mode 100644
index 0000000..c699c73
--- /dev/null
+++ b/src/cmd/help.sh
@@ -0,0 +1,47 @@
+# "help" command
+#
+# Copyright (C) 2013  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_help_main()
+{
+	local cmd=
+
+	if [ ${#} -eq 1 ]; then
+		cmd="${1}"
+		if is_cmd "${cmd}"; then
+			print_cmd_usage "${cmd}"
+			return 0
+		fi
+	fi
+
+	printf "$(get_msg 'cmd_help_head')\n\n" "${0}"
+
+	printf "$(get_msg 'cmd_help_opts_head')\n"
+	print_opt_summaries
+	printf '\n'
+
+	printf "$(get_msg 'cmd_help_summary_head')\n"
+	print_cmd_summaries
+}
+
+cmd_help_register()
+{
+	register_cmd 'help'
+}
+__init cmd_help_register
diff --git a/src/cmd/install.sh b/src/cmd/install.sh
new file mode 100644
index 0000000..e0bdc4e
--- /dev/null
+++ b/src/cmd/install.sh
@@ -0,0 +1,81 @@
+# "install" command
+#
+# Copyright (C) 2013, 2014  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_install_optstring='a:p:m:F'
+
+cmd_install_main()
+{
+	local suite=
+	local chroot=
+	local profile=
+	local foreign=
+	local dev=
+
+	if ! get_options "${@}"; then
+		print_cmd_usage 'install' >&2
+		exit 1
+	fi
+	shift $(($OPTIND - 1))
+
+	if [ ${#} -lt 2 ]; then
+		print_cmd_usage 'install' >&2
+		exit 1
+	fi
+
+	# TODO: Consider making suite optional, getting a default suite from the
+	# profile.
+	suite="${1}"
+	chroot="${2}"
+
+	if [ "x${suite%%:*}" != "x${suite}" ]; then
+		profile="${suite%%:*}"
+		suite="${suite#*:}"
+	else
+		profile='proteanos'
+	fi
+	profile_set "${profile}"
+	suite="$(profile_normalize_suite "${suite}")"
+
+	if [ "x${cmd_install_opt_F+set}" = 'xset' ]; then
+		foreign=true
+	else
+		foreign=false
+	fi
+
+	dev=''
+	if is_block "${chroot}"; then
+		dev="${chroot}"
+		chroot="$(block_mount "${dev}")"
+	fi
+
+	install_system "${cmd_install_opt_m-}" "${suite}" \
+		"${cmd_install_opt_a-}" "${cmd_install_opt_p-}" \
+		"${chroot}" "${foreign}"
+
+	if [ "x${dev}" != 'x' ]; then
+		block_umount "${chroot}"
+	fi
+}
+
+cmd_install_register()
+{
+	register_cmd 'install'
+}
+__init cmd_install_register
diff --git a/src/cmd/installer-pc.sh b/src/cmd/installer-pc.sh
new file mode 100644
index 0000000..dd01506
--- /dev/null
+++ b/src/cmd/installer-pc.sh
@@ -0,0 +1,101 @@
+# "installer-pc" command
+#
+# Copyright (C) 2014  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_installer_pc_optstring='a:p:m:'
+
+cmd_installer_pc_main()
+{
+	local suite=
+	local dev=
+	local profile=
+	local root=
+	local b4=
+	local b3=
+	local b2=
+	local b1=
+
+	if ! get_options "${@}"; then
+		print_cmd_usage 'installer-pc' >&2
+		exit 1
+	fi
+	shift $(($OPTIND - 1))
+
+	if [ "x${cmd_installer_pc_opt_a-}" = 'x' ]; then
+		print_cmd_usage 'installer-pc' >&2
+		exit 1
+	fi
+	if [ "x${cmd_installer_pc_opt_p-}" = 'x' ]; then
+		print_cmd_usage 'installer-pc' >&2
+		exit 1
+	fi
+	if [ ${#} -ne 2 ]; then
+		print_cmd_usage 'installer-pc' >&2
+		exit 1
+	fi
+
+	suite="${1}"
+	dev="${2}"
+	check_block "${dev}"
+
+	if [ "x${suite%%:*}" != "x${suite}" ]; then
+		profile="${suite%%:*}"
+		suite="${suite#*:}"
+	else
+		profile='proteanos'
+	fi
+	profile_set "${profile}"
+	suite="$(profile_normalize_suite "${suite}")"
+
+	cmd_installer_pc_make_partition_and_fs "${dev}"
+	root="$(block_mount "${dev}1")"
+
+	install_system "${cmd_installer_pc_opt_m-}" "${suite}" \
+		"${cmd_installer_pc_opt_a-}" "${cmd_installer_pc_opt_p-}" \
+		"${root}" false
+
+	if [ -f "${root}/sbin/lilo" ]; then
+		read b4 b3 b2 b1 <<-EOF
+			$(od -An -tx1 -v -j 440 -N 4 "${dev}")
+			EOF
+		# This ln command won't be needed once lilo 24.1-1 is uploaded.
+		ln "${root}/boot/vmlinuz" "${root}/boot/vmlinuz.old"
+		session_begin "${root}" . : false
+		session_exec install-lilo \
+			"${dev}" "PARTUUID=${b1}${b2}${b3}${b4}-01"
+		session_end
+	fi
+
+	block_umount "${root}"
+}
+
+cmd_installer_pc_make_partition_and_fs()
+{
+	local dev="${1}"
+
+	dd if=/dev/zero of="${dev}" bs=512 count=1
+	printf 'n\np\n1\n\n\nt\n83\na\n1\nw\n' | fdisk "${dev}"
+	mke2fs -t ext4 "${dev}1"
+}
+
+cmd_installer_pc_register()
+{
+	register_cmd 'installer-pc'
+}
+__init cmd_installer_pc_register
diff --git a/src/cmd/local.mk b/src/cmd/local.mk
new file mode 100644
index 0000000..a6dd3bd
--- /dev/null
+++ b/src/cmd/local.mk
@@ -0,0 +1,8 @@
+prokit_SOURCES += \
+	src/cmd/help.sh \
+	src/cmd/version.sh \
+	src/cmd/install.sh \
+	src/cmd/shell.sh \
+	src/cmd/opkg.sh \
+	src/cmd/build.sh \
+	src/cmd/installer-pc.sh
diff --git a/src/cmd/opkg.sh b/src/cmd/opkg.sh
new file mode 100644
index 0000000..e1fc8ce
--- /dev/null
+++ b/src/cmd/opkg.sh
@@ -0,0 +1,112 @@
+# "opkg" command
+#
+# Copyright (C) 2014  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_opkg_opks=
+
+cmd_opkg_main()
+{
+	local root=
+	local dev=
+	local first_arg=
+	local install_cmd=
+	local arg=
+	local new_fname=
+
+	if [ ${#} -lt 1 ]; then
+		print_cmd_usage 'opkg' >&2
+		exit 1
+	fi
+
+	root="${1}"
+	shift 1
+
+	dev=''
+	if is_block "${root}"; then
+		dev="${root}"
+		root="$(block_mount "${dev}")"
+	fi
+
+	first_arg=true
+	install_cmd=false
+	cmd_opkg_opks=''
+	for arg in "${@}"; do
+		if ${first_arg}; then
+			set --
+			first_arg=false
+		fi
+		if ${install_cmd}; then
+			case "${arg}" in
+				*.opk|*.ipk|*.deb)
+					if ! [ -e "${arg}" ]; then
+						set -- "${@}" "${arg}"
+						continue
+					fi
+					rand
+					new_fname="/tmp/prokit.${rand_x}.opk"
+					cmd_opkg_opks="$(printf '%s %s\n' \
+						"${cmd_opkg_opks}" \
+						"${root}${new_fname}")"
+					cp "${arg}" "${root}${new_fname}"
+					set -- "${@}" "${new_fname}"
+					;;
+				*)
+					set -- "${@}" "${arg}"
+					;;
+			esac
+		else
+			set -- "${@}" "${arg}"
+			if [ "x${arg}" = 'xinstall' ]; then
+				install_cmd=true
+			fi
+		fi
+	done
+
+	. "${root}/etc/os-release"
+	profile_set "${ID}"
+
+	session_begin "${root}" . cmd_opkg_fini false
+
+	session_exec opkg "${@}"
+
+	cmd_opkg_fini
+	session_end
+
+	if [ "x${dev}" != 'x' ]; then
+		block_umount "${root}"
+	fi
+}
+
+cmd_opkg_fini()
+{
+	case "${cmd_opkg_opks}" in
+		*[!\ ]*)
+			rm -f ${cmd_opkg_opks}
+			# Hack to avoid this code branch if the function is
+			# called again in response to a signal.
+			cmd_opkg_opks=''
+			;;
+	esac
+}
+
+cmd_opkg_register()
+{
+	register_cmd 'opkg'
+}
+__init cmd_opkg_register
diff --git a/src/cmd/shell.sh b/src/cmd/shell.sh
new file mode 100644
index 0000000..468da0f
--- /dev/null
+++ b/src/cmd/shell.sh
@@ -0,0 +1,62 @@
+# "shell" command
+#
+# Copyright (C) 2014  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_shell_main()
+{
+	local root=
+	local dev=
+
+	if [ ${#} -lt 1 ]; then
+		print_cmd_usage 'shell' >&2
+		exit 1
+	fi
+
+	root="${1}"
+	shift 1
+
+	dev=''
+	if is_block "${root}"; then
+		dev="${root}"
+		root="$(block_mount "${dev}")"
+	fi
+
+	. "${root}/etc/os-release"
+	profile_set "${ID}"
+
+	session_begin "${root}" . : false
+	if [ ${#} -eq 0 ]; then
+		session_exec /bin/sh
+		printf '\n'
+		info "$(get_msg 'cmd_shell_exiting')"
+	else
+		session_exec "${@}"
+	fi
+	session_end
+
+	if [ "x${dev}" != 'x' ]; then
+		block_umount "${root}"
+	fi
+}
+
+cmd_shell_register()
+{
+	register_cmd 'shell'
+}
+__init cmd_shell_register
diff --git a/src/cmd/version.sh b/src/cmd/version.sh
new file mode 100644
index 0000000..0105f20
--- /dev/null
+++ b/src/cmd/version.sh
@@ -0,0 +1,33 @@
+# "version" command
+#
+# Copyright (C) 2013  Patrick "P. J." McDermott
+#
+# This file is part of the ProteanOS Development Kit.
+#
+# The ProteanOS Development Kit is free software: you can redistribute
+# it and/or modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# The ProteanOS Development Kit is distributed in the hope that it
+# will be useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with the ProteanOS Development Kit.  If not, see
+# <http://www.gnu.org/licenses/>.
+
+cmd_version_main()
+{
+	printf '%s (%s) %s\n' "${0##*/}" "${PACKAGE_NAME}" "${PACKAGE_VERSION}"
+
+	printf "$(get_msg 'cmd_version_copyright')\n" \
+		'2012, 2013, 2014' 'Patrick "P. J." McDermott'
+}
+
+cmd_version_register()
+{
+	register_cmd 'version'
+}
+__init cmd_version_register
--
cgit v0.9.1