#!/bin/sh

# This script is run on a diskless client booted from an ALT Linux distribution.
# It turns a live system to a `bound', non-live one,
# essentially doing the job of an OS installer. :(
# The change can be committed to the server with overlays-create.

. shell-getopt

DST="${DST:-}"		# Where to get binaries
ADDHOSTS=""
FROMBIN="/usr/bin"
DEFAULT_LIVECD_USER="altlinux"

if test -r "$HOME/.config/netinst-overlays/overlays-create"; then
	CFG="$HOME/.config/netinst-overlays/overlays-create"
else
	CFG="$HOME/.overlays-create"
fi

argv=$(getopt \
	-n "overlays-init" \
	-o H:,T:, -l destination:,tool-path:,config-from: \
	-- "$@")
eval set -- $argv
while : ; do case "$1" in
	--) shift; break; ;;
	-H|--destination|--config-from) shift; DST="$1" ;;
	-T|--tool-path) shift; FROMBIN="$1" ;;
esac; shift
done

if [ -n "$DST" ]; then
	# populate config
	mkdir -p bin
	echo "Getting setup from $DST"
	mkdir -p ${CFG%/?*}
	ssh $DST ${FROMBIN:+$FROMBIN/}overlays-manage setup > "$CFG" || exit 1
	echo "DST='$DST'" >> "$CFG"
	echo "Getting overlays-create from $DST"
	scp $DST:${FROMBIN:+$FROMBIN/}overlays-create bin/ || exit 2
else
	# read existing config
	test -r "$CFG" && . "$CFG"
fi
test -z "$ADDHOSTS" || echo "Adding hostkeys"
for N in $ADDHOSTS; do ssh -n $N; done

# THE PASSWORD
passwd root || {
	echo "Using default root password. Take care!"
	sed -i 's/:[^:]*/:$2a$08$XzZsVoWF3x9BZ0Es6Q3J7ucVNA59dlwcCj.Ki6ArpOyxyI88HMgUG/' /etc/tcb/root/shadow
}

# No free root for default user
sed -i "/$DEFAULT_LIVECD_USER/d" /etc/sudoers

# No wheel for default user
sed -i "/^wheel:/s/,$DEFAULT_LIVECD_USER//" /etc/group

# Enable repo
sed '/#rpm.*http:/s/^#//' /etc/apt/sources.list.d/alt.list >> /etc/apt/sources.list

# Manage localtime through classic clock service
sed -i 's/^#ZONE/ZONE/' /etc/sysconfig/clock
rm -f /etc/localtime

# Fix bugs in various components:
# https://bugzilla.altlinux.org/show_bug.cgi?id=34466
sed -i "/shell_config_set .*options DISABLED 'no'/s/'no'/'yes'/" /etc/init.d/livecd-save-nfs
cat > /etc/systemd/system/fix-network.service <<@@@
[Service]
Type=simple
@@@

# Stop some services
if [ -d /run/systemd/system ]; then
	SYSTEMCTL=systemctl # avoiding dependency on systemctl
	$SYSTEMCTL disable alteratord
else
	chkconfig alteratord off
fi

# Disable unprivileged storage mount with udisks
PK_UDISKS="/usr/share/polkit-1/actions/org.freedesktop.udisks2.policy"
test -r "$PK_UDISKS" &&
ed "$PK_UDISKS" <<@@@
/<description>Mount a filesystem on a system device
/<defaults/+1,/<.defaults/-1d
w
q
@@@

# remove some packages and files
rpm -e livecd-install || :
#find /home/$DEFAULT_LIVECD_USER -name livecd-install.desktop -exec rm {} \;
find /home/$DEFAULT_LIVECD_USER -name livecd-install.desktop -delete

# Record destination hostname
#HST="${DST##*@}"
#{ host "$HST" || host "${HST%%.*}"; } |
#  awk '/has address/{print $4,$1,gensub(/[.].*/,"",$1)}' >> /etc/hosts

# TODO: overlays-create initial -> overlays-create, deduce the preset automatically.
# For that, we need to enumerate mounted overlays from the running system,
# that requires changes down in the stack.
echo "The system is ready."
echo "You can commit your changes with 'overlays-create initial' now"
