#!/bin/sh
# This script is executed inside the image chroot before packing up.
# Architecture/platform specific code goes here, like kernel install
# and configuration

env-update
. /etc/profile

setup_boot() {
	# enable sshd by default
	rc-update add sshd default
	# enable logger by default
	rc-update add syslog-ng boot
	rc-update add vixie-cron boot
	# Linkstation Pro/Live Deamon
	rc-update add ntp-client default
	rc-update add ntpd default
	# enable dbus, of course, and also NetworkManager
	rc-update add dbus boot

	# we are on eth0 in the Medion STG-212 so create the init.d link
	ln -sf net.lo /etc/init.d/net.eth0
	rc-update add net.eth0 default

	# select the first available kernel
	eselect uimage set 1

	# cleaning up deps
	rc-update --update
}

setup_startup_caches() {
	mount -t proc proc /proc
	/lib/rc/bin/rc-depend -u
	# Generate openrc cache
	[[ -d "/lib/rc/init.d" ]] && touch /lib/rc/init.d/softlevel
	[[ -d "/run/openrc" ]] && touch /run/openrc/softlevel
	/etc/init.d/savecache start
	/etc/init.d/savecache zap
	ldconfig
	ldconfig
	umount /proc
}


setup_users() {
	# setup root password to... root!
	echo root:sablink | chpasswd
}

setup_serial() {
	# setup serial login
	sed -i "s:^s0.*::" /etc/inittab
	echo "s0:12345:respawn:/sbin/agetty 115200 ttyS0 vt100" >> /etc/inittab
}

setup_fallback_network() {
	# setup fallback network
	echo "" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "# Sablink Core Network Config" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "# DHCP IP / route configuration, with fallback to a static IP / route" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "config_eth0=\"dhcp\"" >> /etc/conf.d/net
	echo "fallback_eth0=\"192.168.178.103 netmask 255.255.255.0 broadcast 192.168.178.255\"" >> /etc/conf.d/net
	echo "fallback_route_eth0=\"default via 192.168.178.1\"" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "# pure static IP / route configuration, should the above not work even with fallback.." >> /etc/conf.d/net
	echo "# (in that case, comment the whole DHCP section and uncomment this one)" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "#config_eth0=\"192.168.11.3 netmask 255.255.255.0 broadcast 192.168.11.255\"" >> /etc/conf.d/net
	echo "#routes_eth0=\"default via 192.168.11.1\"" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "# DNS configuration" >> /etc/conf.d/net
	echo "#" >> /etc/conf.d/net
	echo "" >> /etc/conf.d/net
	echo "dns_servers_eth0=\"192.168.178.1\"" >> /etc/conf.d/net
}

setup_fstab() {
	sed -e "s!/BOOT!/sda1!g" -e "s!/ROOT!/sda2!g" -e "s!SWAP!sda3!g" -e "s!ext3!btrfs!g" -i /etc/fstab
}

setup_layman() {
	sed -e "s!/source!/#source!g" -i /etc/make.conf
}

setup_users
setup_boot
setup_serial
setup_startup_caches
setup_fallback_network
setup_fstab
setup_layman

exit 0

