#!/usr/bin/env zsh
# Copyright (c) 2016-2017 Dyne.org Foundation
# libdevuansdk maintained by Ivan J. <parazyd@dyne.org>
#
# This file is part of libdevuansdk
#
# 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/>.

arrs+=(base_packages_option core_packages_option extra_packages_option)
arrs+=(purge_packages_option)
arrs+=(core_packages base_packages extra_packages purge_packages)

bootstrap_complete_base() {
	fn bootstrap_complete_base "$@"
	req=(workdir strapdir os arch release)
	ckreq || return 1
	
	notice "bootstrapping $os $arch base"

	mkdir -p $strapdir

	export LANG=C
	export LC_ALL=C
	export DEBIAN_FRONTEND=noninteractive

	notice "running debootstrap stage 1"

	sudo DEBOOTSTRAP_DIR="$LIBPATH/extra/debootstrap" "$LIBPATH/extra/debootstrap/debootstrap" \
		--keyring="$LIBPATH/extra/devuan-keyring/keyrings/devuan-archive-keyring.gpg" \
		--foreign \
		--include "gnupg2" \
		--arch $arch $release $strapdir $mirror || zerr

	sudo mkdir -p $strapdir/tmp
	sudo chmod 1777 $strapdir/tmp

	## debootstrap stage 2
	notice "running debootstrap stage 2"
	sudo chroot $strapdir \
		/debootstrap/debootstrap --second-stage || zerr

	## write all system configuration
	notice "writing system configuration"
	conf_print_debconf     | sudo tee $strapdir/debconf.set              >/dev/null
	conf_print_fstab       | sudo tee $strapdir/etc/fstab                >/dev/null
	conf_print_hostname    | sudo tee $strapdir/etc/hostname             >/dev/null
	conf_print_hosts       | sudo tee $strapdir/etc/hosts                >/dev/null
	conf_print_netifaces   | sudo tee $strapdir/etc/network/interfaces   >/dev/null
	conf_print_resolvconf  | sudo tee $strapdir/etc/resolv.conf          >/dev/null
	conf_print_sourceslist | sudo tee $strapdir/etc/apt/sources.list     >/dev/null
	#conf_print_locales     | sudo tee $strapdir/etc/profile.d/locales.sh >/dev/null
}

bootstrap_thirdstage() {
	fn bootstrap_thirdstage
	req=(R os strapdir arch release core_packages base_packages extra_packages purge_packages)
	ckreq || return 1
	
	find $R/extra/gnupg-keys -type f -name "*.asc" -exec cp {} $strapdir/home \;
	
	#This is a dirty temporary solution until my next amprolla compiling of packages.gnuinos.org
	custom_deb_packages=$R/extra/missing-pkgs/$release	
	install-custdebs || { zerr; wrapup }
	custom_deb_packages=$blendlib/custom-pkgs/$release

	cat << EOF | sudo tee $strapdir/thirdstage >/dev/null
#!/bin/sh

apt-key add /home/*.asc
apt-get update
rm -f /tmp/*.deb
rm -f /home/*.deb

debconf-set-selections /debconf.set

echo "${rootcredentials}" | chpasswd
sed -i -e 's/KERNEL\!=\"eth\*|/KERNEL\!=\"/' \
	/lib/udev/rules.d/75-persistent-net-generator.rules
rm -f /etc/udev/rules.d/70-persistent-net.rules
export DEBIAN_FRONTEND=noninteractive

apt-get --yes --force-yes --fix-missing install ${core_packages}
apt-get --yes --force-yes --fix-missing install ${base_packages}
apt-get --yes --force-yes --fix-missing install ${extra_packages}
apt-get --yes --force-yes purge ${purge_packages}
##apt-get --yes --force-yes autoremove
apt-get clean

sed -e 's/# en_US.UTF-8/en_US.UTF-8/' -i /etc/locale.gen
locale-gen

rm -f /debconf.set
rm -f /etc/ssh/ssh_host_*
rm -f /root/.bash_history
#echo "1" > .keep
EOF
	
	chroot-script thirdstage    || { zerr; wrapup }
}

bootstrap_backup() {
	fn bootstrap_thirdstage
	req=(R os strapdir arch release squashfs_backups)
	ckreq || return 1
}

bootstrap_restore() {
	fn bootstrap_thirdstage
	req=(R os strapdir arch release squashfs_backups)
	ckreq || return 1
}
	
	
