#!/bin/bash

# Systemd services
binfmt_fixups_service (){
tee /etc/systemd/system/binfmt-fixups.service <<EOF
[Unit]
Description=Systemd Binfmt Fixups
ConditionFileIsExecutable=/usr/local/sbin/systemd-binfmt-fixups
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/local/sbin/systemd-binfmt-fixups
TimeoutSec=0
RemainAfterExit=yes
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target
EOF
systemctl enable binfmt-fixups.service
}

credentials_service (){
tee /etc/systemd/system/credentials.service <<EOF
[Unit]
Description=Credentials
After=firstboot.service network.target
Before=rc-local.service
ConditionPathExists=/usr/local/bin/credentials

[Service]
ExecStart=/usr/local/bin/credentials > /dev/null 2>&1
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
}

firstboot_service (){
tee /etc/systemd/system/firstboot.service <<EOF
[Unit]
Description=Resize ROOTFS partition
After=systemd-remount-fs.service sysinit.target local-fs.target
ConditionPathExists=/usr/local/sbin/firstboot
DefaultDependencies=no

[Service]
ExecStart=/usr/local/sbin/firstboot
Type=oneshot
RemainAfterExit=no
StandardOutput=journal+console
StandardError=journal+console

[Install]
WantedBy=multi-user.target
EOF
}

governor_service (){
tee /etc/systemd/system/governor.service <<EOF
[Unit]
Description=Governor
ConditionPathExists=/sys/devices/system/cpu/online
After=rc-local.service

[Service]
ExecStart=/usr/local/bin/governor -r &>/dev/null
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
}

kali_services (){
systemctl enable avahi-daemon
systemctl enable bluetooth
systemctl enable ntpsec
systemctl enable ssh
systemctl disable systemd-networkd
systemctl mask systemd-networkd-wait-online
}

led_service (){
tee /etc/systemd/system/leds.service <<EOF
[Unit]
Description=LEDs
ConditionPathExists=/usr/local/sbin/leds

[Service]
ExecStart=/usr/local/sbin/leds &>/dev/null
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
systemctl enable leds
}

rc-local_service (){
tee /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
EOF
tee /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0
EOF
chmod +x /etc/rc.local
}

systemd_services (){
systemctl enable rc-local
#systemctl enable ifplugd
systemctl enable zramswap
systemctl enable bluetooth
systemctl enable firstboot
systemctl enable credentials
systemctl enable governor
if [[ "$DISTRO_VERSION" == "bullseye" ]] && [[ "$BOARD" == "bcm2708" ]]; then
	mkdir -p /etc/systemd/system/haveged.service.d
	echo -e '[Service]\nSystemCallFilter=uname' > /etc/systemd/system/haveged.service.d/fix.conf
	systemctl daemon-reload
	systemctl restart haveged
fi
if [[ -f "/etc/systemd/system/multi-user.target.wants/ondemand.service" ]]; then systemctl disable ondemand; fi
if [[ "$DISTRO" =~ ^(debian|kali|raspbian)$ ]]; then systemctl enable resolvconf; fi
#systemctl enable serial-getty@ttyUSB0.service
}

# devuan
sysvinit_services (){
chmod +x /etc/init.d/governor
chmod +x /etc/init.d/firstboot
chmod +x /etc/init.d/leds
chmod +x /etc/init.d/bthelper
chmod +x /etc/init.d/zramswap
chmod +x /etc/init.d/credentials
update-rc.d -f hwclock.sh remove
update-rc.d firstboot defaults S
update-rc.d governor defaults 2
update-rc.d leds defaults 2
update-rc.d bthelper defaults 2
update-rc.d zramswap defaults 2
update-rc.d credentials defaults 2
}
