#!/bin/sh -e

[ "$(id -u)" -eq 0 ] || { echo 'This script must be run as root!'; exit 1; }

if [ ! -d /home/cb ]
then
	echo "Adding user cb"
	adduser cb
	addgroup cb audio
	addgroup cb input
	addgroup cb netdev
	addgroup cb video
	addgroup cb wheel
	sed -in "s/^tty1.*/tty1::respawn:\/bin\/login -f cb/" /etc/inittab
fi


if ! grep -q sodface.com /etc/apk/repositories
then
	echo "http://www.sodface.com/repo" >> /etc/apk/repositories
	wget -O - http://www.sodface.com/repo/sodface-pub-key.tar.gz \
		| tar xzvf - -C /etc/apk/keys
	apk update
fi

if [ ! -x /usr/bin/herbstluftwm ]
then
	apks="
		adwaita-icon-theme
		font-opensans
		herbstluftwm
		mesa-dri-gallium
		mesa-egl
		ttf-dejavu
		"
	setup-xorg-base ${apks}

	apks="
		alsa-utils
		brightnessctl
		doas
		firefox
		jivelite
		libsgestures-hlwm
		mpv
		openresolv
		rng-tools
		sakura
		sod-svkbd
		squeezelite
		util-linux-misc
		xlunch
		xset
		"
	apk add ${apks}

	echo "permit nopass :wheel" >> /etc/doas.d/doas.conf

	rc-update add alsa
	rc-update add iwd
	rc-update add swclock boot
	rc-update add rngd boot
fi

if [ "$1" = "jeep" ] && [ ! -x /usr/sbin/gpsd ] 
then
	apks="
		chrony
		gpsd
		gpsd-clients
		lms
		"
	apk add ${apks}

	cat <<-EOF > /etc/chrony/chrony.conf
		driftfile /var/lib/chrony/chrony.drift
		makestep 0.1 3
		refclock SHM 0 refid GPS precision 1e-1 offset 0 delay 0 stratum 2
	EOF

	cat <<-EOF >> /etc/conf.d/gpsd
		GPSD_OPTIONS="-n"
		DEVICES="/dev/ttyUSB0"
	EOF
	
	rc-update add chronyd
	rc-update add gpsd
	
	echo "permit nopass :wheel" >> /etc/doas.d/doas.conf
	echo "PARTUUID=478d0316-5a32-a049-bae6-e3af0883fce4 /srv ext4 rw,relatime 0 0" >> /etc/fstab
	
	sed -in '/^depend/,+5d' /etc/init.d/squeezelite
	cat <<-EOF >> /etc/conf.d/squeezelite
		SL_SERVERIP="-s 127.0.0.1"
		SL_USER=squeezelite
		SL_OPTS="-n Jeep -m 00:01:80:8a:45:e4"
	EOF

	sed -in '/^depend/,+3d' /etc/init.d/lms
	cat <<-EOF >> /etc/conf.d/lms
		LMS_OPTS="--advertiseaddr 127.0.0.1 --httpaddr 127.0.0.1 --cliaddr 127.0.0.1 --playeraddr 127.0.0.1 --nomysqueezebox"
	EOF

	ln -s /etc/init.d/networking /etc/init.d/net.eth0
	rc-update del networking boot
	rc-update add net.eth0
	echo "iface eth0 inet manual" > /etc/network/interfaces

	(crontab -l; echo "@reboot * * * * /usr/bin/gpssetclock.sh") | crontab -
fi

tmp=$(mktemp -td cb.XXXXXX)
wget -O - http://www.sodface.com/repo/cb/aopen-veyron/setup-cb.tar.gz \
	| tar xzvf - -C ${tmp}

install -Dm 644 ${tmp}/files/asound.conf -t /etc
install -Dm 644 ${tmp}/files/asound.state -t /var/lib/alsa
install -Dm 755 ${tmp}/files/autostart -t /home/cb/.config/herbstluftwm
install -Dm 755 ${tmp}/files/catch-gesture-hook.sh -t /usr/bin
install -Dm 755 ${tmp}/files/catch-xlunch-hook.sh -t /usr/bin
install -Dm 644 ${tmp}/files/dot-profile /home/cb/.profile
install -Dm 644 ${tmp}/files/dot-xinitrc /home/cb/.xinitrc
install -Dm 644 ${tmp}/files/dot-xserverrc /home/cb/.xserverrc
install -Dm 755 ${tmp}/files/file-picker.sh -t /usr/bin
install -Dm 644 ${tmp}/files/sakura.conf -t /home/cb/.config/sakura
install -Dm 644 ${tmp}/icons/* -t /home/cb/.config/icons
install -Dm 644 ${tmp}/video/* -t /srv/video
install -Dm 644 ${tmp}/wallpaper/* -t /home/cb/.config/wallpaper

if [ "$1" = "jeep" ]
then
	install -Dm 644 ${tmp}/files/blacklist_uas.conf -t /etc/modprobe.d
	install -Dm 755 ${tmp}/files/gpssetclock.sh -t /usr/bin
	install -Dm 644 ${tmp}/files/SlimDiscoveryApplet.lua -t /usr/share/jive/applets/SlimDiscovery
fi

chown -R cb:cb /home/cb /srv/video
rm -rf ${tmp}

echo "Done, please reboot."

