#!/usr/bin/env bash
## Setting init in grub can use the pre-exiting init system
# init=/lib/sysvinit/init
# init=/lib/systemd/systemd

echo "deb http://Debian-repository.JdeBP.info./ stable main" > /etc/apt/sources.list-available/nosh.list
ln -s /etc/apt/sources.list-available/nosh.list  /etc/apt/sources.list.d/nosh.list 
#curl "http://Debian-repository.JdeBP.info./repository_signing_key.pub" > /usr/share/keyrings/jdebp-debian-repository.gpg
curl "https://JdeBP.EU./Repository/debian/repository_signing_key.pub" > /usr/share/keyrings/jdebp-debian-repository.gpg


apt update

## https://jdebp.eu/Softwares/nosh/debian-binary-packages.html
# Documentation
apt install -y nosh-guide

apt install -y \
	nosh-exec \
	nosh-service-management \
	nosh-terminal-management \
	nosh-systemv-shims

apt install -y \
	nosh-run-system-manager \
	nosh-run-local-syslog \
	nosh-run-klog \
	nosh-run-udev \
	nosh-run-user-vt 
	#nosh-run-kernel-vt \

redo -C /etc/system-control/convert/ all

## Toolsets

## (exec, nosh, and the chain-loading utilities)
#apt install -y nosh-exec

## (system-manager, service-manager, system-control, service-control and related utilities)
#apt install -y nosh-service-management

## (console-terminal-emulator, vc-get-tty, pty-get-tty, ttylogin-starter and related utilities)
#apt install -y nosh-terminal-management

## (ptybandage, ptyrun and related utilities)
apt install -y nosh-terminal-extras

## (tcpserver shim)
apt install -y nosh-ucspi-tcp-shims

## (Z Shell completions for the toolsets)
apt install -y nosh-zsh-completion

## (systemd and servicectl shims for systemd compatibility)
#apt install -y nosh-systemd-shims

## (halt, reboot, poweroff, telinit, chkconfig, and related shims for old-style System 5/BSD compatibility)
apt install -y nosh-systemv-shims

## (service shim for old-style System 5/BSD compatibility)
apt install -y nosh-service-command-shim

## (start, stop, initctl, and related shims for upstart compatibility)
#apt install -y nosh-upstart-shims

## (invoke-rc.d and update-rc.d shims for Debian compatibility)
apt install -y nosh-debian-shims

## (fasthalt, fastboot, and fastpoweroff shims for BSD compatibility)
#apt install -y nosh-bsd-shims

## (rcctl and and related shims for OpenBSD compatibility)
#apt install -y nosh-openbsd-shims

## (utx and and related shims for FreeBSD compatibility)
#apt install -y nosh-freebsd-shims

## (resizecons, clear_console, and chvt shims for compatibility with the old kbd package)
apt install -y nosh-kbd-shims

## Service bundles 
apt install -y nosh-bundles

## system-manager as process #1 (removes nosh-run-via-systemd)
#apt install -y nosh-run-system-manager


## Base services
# includes virecover and Debian cron.
apt install -y nosh-run-debian-server-base

# CUPS, anacron, and Sun RPC.
#apt install -y nosh-run-debian-desktop-base_1.37_amd64.deb

## Change the Desktop Bus helper program
## Fix udev behaviour on non-systemd
# The nosh toolset comes with one such replacement helper program, dbus-daemon-launch-helper, that can use system-control to speak to nosh service management, initctl to speak to upstart service management, or systemctl to speak to systemd service management.

## Edit /etc/dbus-1/system.conf
## replace:
# <!-- This is a setuid helper that is used to launch system services -->
# <servicehelper>/usr/lib/dbus-1.0/dbus-daemon-launch-helper</servicehelper>
## with:
# <!-- This is a non-setuid helper that is used to tell service management to launch system services -->
# <servicehelper>/usr/local/bin/dbus-daemon-launch-helper</servicehelper>

DBUS_HOME=/usr/share/polkit-1
# xmlstarlet will parse then output valid xml but removes whitespace lines people should either use 
# xml or a user readable format.
#SERVICE_HELPER=/usr/local/bin/dbus-daemon-launch-helper
#xmlstarlet edit -L -u "/busconfig/servicehelper" -v ${SERVICE_HELPER} ${DBUS_HOME}/system.conf

# sed to edit xml is bad but works and preserves layout but does not guarantee valid xml.
COMMENT_OLD="<\!-- This is a setuid helper that is used to launch system services -->"
COMMENT_NEW="<\!-- This is a non-setuid helper that is used to tell service management to launch system services -->"
sed -i -e s"|${COMMENT_OLD}|${COMMENT_NEW}|" ${DBUS_HOME}/system.conf

SERVICE_HELPER_OLD="<servicehelper>/usr/lib/dbus-1.0/dbus-daemon-launch-helper</servicehelper>"
SERVICE_HELPER_NEW="<servicehelper>/usr/local/bin/dbus-daemon-launch-helper</servicehelper>"
sed -i -e s"|${SERVICE_HELPER_OLD}|${SERVICE_HELPER_NEW}|" ${DBUS_HOME}/system.conf

## Plug and play device managers

## (vdev)
#apt install nosh-run-vdev_1.37_amd64.deb (vdev)

##    (systemd's udev)
#apt install -y nosh-run-udev

##    (busybox mdev)
#apt install nosh-run-busybox-mdev

##    (suckless mdev)
#apt install nosh-run-suckless-mdev

## Virtual terminal services

## (old-style kernel virtual terminals)
#apt install -y nosh-run-kernel-vt

## (a new-style application-mode virtual terminal)
#apt install -y nosh-run-user-vt

## Freedesktop.org "kit" services
# (Freedesktop.org "kit" services — packagekit, consolekit, policykit, NetworkManager, ModemManager et al.) 
#apt install -y nosh-run-freedesktop-kits

## VirtualBox Guest Additions
#apt install -y nosh-run-virtualbox-guest

## kernel log service
#apt install -y nosh-run-klog

## local syslog() service
# old-style logging service to programs that still use /dev/log. 
#apt install -y nosh-run-local-syslog

# Index of ftp://repository.jdebp.info./debian/dists/stable/main/binary-amd64/
# 
# Packages
# Packages.bz2
# clockspeed_7_amd64.deb
# daemontools_7_amd64.deb
# djbdns-host_7_amd64.deb
# djbdns_7_amd64.deb
# leapsecs_7_amd64.deb
# libtai_7_amd64.deb
# multilog_7_amd64.deb
#
# nosh-bsd-services_1.13_amd64.deb
# nosh-bsd-shims_1.37_amd64.deb
# nosh-bsd-sockets_1.13_amd64.deb
# nosh-bundles_1.37_amd64.deb
# nosh-core-shims_1.37_amd64.deb
# nosh-debian-shims_1.37_amd64.deb
# nosh-exec_1.37_amd64.deb
# nosh-execline-shims_1.37_amd64.deb
# nosh-freebsd-shims_1.37_amd64.deb
# nosh-guide_1.37_amd64.deb
# nosh-kbd-shims_1.37_amd64.deb
# nosh-logrotate-shims_1.37_amd64.deb
# nosh-openbsd-shims_1.37_amd64.deb
# nosh-regular-services_1.13_amd64.deb
# nosh-regular-sockets_1.13_amd64.deb
#
# nosh-run-appletalk_1.37_amd64.deb
# nosh-run-busybox-mdev_1.37_amd64.deb
# nosh-run-debian-desktop-base_1.37_amd64.deb
# nosh-run-debian-server-base_1.37_amd64.deb
# nosh-run-freedesktop-kits_1.37_amd64.deb
# nosh-run-freedesktop-system-bus_1.37_amd64.deb
# nosh-run-kernel-vt_1.37_amd64.deb
# nosh-run-klog_1.37_amd64.deb
# nosh-run-local-syslog_1.37_amd64.deb
# nosh-run-openssh-server_1.37_amd64.deb
# nosh-run-suckless-mdev_1.37_amd64.deb
# nosh-run-system-manager_1.37_amd64.deb
# nosh-run-systemd-udev_1.37_amd64.deb
# nosh-run-udev_1.37_amd64.deb
# nosh-run-user-vt_1.37_amd64.deb
# nosh-run-vdev_1.37_amd64.deb
# nosh-run-via-systemd_1.37_amd64.deb
#
# nosh-service-command-shim_1.37_amd64.deb
# nosh-service-management_1.37_amd64.deb
# nosh-systemd-services_1.14_amd64.deb
# nosh-systemv-shims_1.37_amd64.deb
# nosh-terminal-extras_1.37_amd64.deb
# nosh-terminal-management_1.37_amd64.deb
# nosh-ucspi-tcp-shims_1.37_amd64.deb
# nosh-upstart-shims_1.37_amd64.deb
# nosh-zsh-completion_1.37_amd64.deb
# nosh_1.28_amd64.changes
#
# ptyget_7_amd64.deb
# publicfile_7_amd64.deb
# redo_1.4_amd64.deb
# taiclockd_7_amd64.deb
