#!/usr/bin/env zsh
# Copyright (c) 2017 Dyne.org Foundation
# live-sdk is written and maintained by Ivan J. <parazyd@dyne.org>
#
# This file is part of live-sdk
# devuan-minimal-live.blend is maintained by KatolaZ <katolaz@freaknet.org>
#
# This source code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this source code. If not, see <http://www.gnu.org/licenses/>.

BLENDPATH="${BLENDPATH:-$(dirname $0)}"

source "$BLENDPATH/config"
source "$BLENDPATH/config-docker"
source "$BLENDPATH/helpers"
source "$BLENDPATH/helpers-docker"
source "$BLENDPATH/sysconfig"

blend_release_path="$BLENDPATH/$release/"
blend_scripts="${blend_release_path}/scripts"

###source $BLENDPATH/$release/devuan-minimal-live.release

blend_preinst() {
	fn blend_preinst
	req=(blend_name username userpass)
	ckreq || return 1

	notice "executing $blend_name preinst"

    cat <<-EOF | sudo tee "$strapdir/add-user" >/dev/null
		#!/bin/sh
		useradd -s /bin/bash -m "${username}" || exit 1
		echo "${username}:${userpass}" | chpasswd || exit 1
    EOF
    chroot-script -d add-user || { zerr; return 1; }

#    notice "copying blend-specific debs into $R/extra/custom-packages"
#
#    sudo mkdir -p "$strapdir/extra_packages" || { zerr; return 1; }
#    sudo cp -fv "$blend_release_path"/custom_debs/*.deb "$strapdir/extra_packages" || { zerr; return 1; }
#    cat <<-EOF
#	#!/bin/sh
#	cd custom-packages
#	dpkg -i *_all.deb *_${arch}.deb
#	apt-get -f install
#	cd - >/dev/null
#	rm -rf custom-packages
#    EOF
#    chroot-script -d install-custpackages || { zerr; return 1; }
}

blend_postinst() {
	fn blend_postinst
	req=(strapdir blend_name)
	ckreq || return 1

	notice "executing $blend_name postinst"

	#pushd "$strapdir"
	#	sudo rsync -avx --no-o --no-g "$blend_release_path"/rootfs_overlay/* . || { zerr; return 1; }
	#	sudo chmod 755 etc/init.d/boot_beep
	#popd

	blend_finalize || { zerr; return 1 }
}


put_finalize(){

	cat <<-EOF | sudo tee ${strapdir}/finalize >/dev/null
		#!/bin/sh

		## permissions
		for i in cdrom floppy audio dip video plugdev netdev lpadmin scanner; do
			gpasswd -a ${username} \$i
		done

		chsh -s "${default_shell}" ${username}
		chown -R 1000:1000 /home/${username}

		rm -f /etc/fstab
		rm -f /etc/popularity-contest.conf
		rm -f /var/lib/dbus/machine-id

		# Disable contrib and non-free after installing firmware.
		sed -i 's/ contrib//g' /etc/apt/sources.list
		sed -i 's/ non-free//g' /etc/apt/sources.list

		## Disable updates
		sed -i '/-updates/s/^/## /g' /etc/apt/sources.list

		## replace pkgmaster with deb.devuan.org
		sed -i 's/pkgmaster/deb/g'


		## insert  service boot_beep
		insserv boot_beep

		##  Configure framebuffer
		setcap 'cap_sys_tty_config+ep' /usr/bin/fbterm                    
		chmod u+s /usr/bin/fbterm   

		## install additional packages
		#cd /extra/custom-packages
		#dpkg -i \$(find ./ -type f -name '*.deb')

		## package list
		#dpkg -l | awk '/^ii/ { print \$2 " " \$3 }' > /home/${username}/package_list

		##apt-get update
		#apt-get clean
		#rm /var/cache/apt/*.bin
		#updatedb
	EOF
}

blend_finalize() {
	fn blend_finalize
	req=(strapdir username default_shell)
	ckreq || return 1

	put_finalize

	#chroot-script -d finalize || zerr
	chroot-script finalize || zerr

}
