#!/bin/bash

#
## lrrr@armbian.com
##
## customize-image.sh script to create a minimal "network-install" armbian build
## image for orangepizero.  Script option to remove systemd and use sysvinit.
##
## The root package is converted to a "minimal" version.  All package files are
## removed from root into /usr/share/armbian-minimal-root and compressed.  If
## needed, decompress and use them.
##
## Serial console is enabled on ttyS0.  If you need remote login, be sure to 
## add to image.
## 
## Required options for ./config-default.conf to prevent external packages
##
## EXTERNAL="no"
## EXTERNAL_NEW="no"
## KERNEL_ONLY="no"
## BOARD="orangepizero"
##
## Required options for ./userpatches/lib.config to set minimal package install
##
## PACKAGE_LIST="linux-base u-boot-tools initramfs-tools"
## PACKAGE_LIST_RELEASE=""
## PACKAGE_LIST_ADDITIONAL=""
##
## To build stretch, set in ./userpatches/sources/sun8i.conf
##
## CAN_BUILD_STRETCH=yes
##

# script options
WITHOUT_SYSTEMD=yes  # yes for sysvinit, no for systemd.
SERIAL_CONSOLE=yes   # enable ttyS0 when using sysvinit

# script variables
RELEASE=$1
source /etc/armbian-image-release

# your code here
CustomizeImage() {
	case $RELEASE in
		jessie)
			;;
		stretch)
			;;
	esac

	# uncomment to chroot image
	#ChrootImage
}

# minimal root script start
MinimalRoot() {
	# convert root to "minimal" version.
	WD="/tmp/workdir"
	SHARE="${WD}/data/usr/share/armbian-minimal-root"

	DEB_BRANCH=${BRANCH//default}
	DEB_BRANCH=${DEB_BRANCH:+${DEB_BRANCH}-}
	BOARDPKG=linux-${RELEASE}-root-${DEB_BRANCH}${BOARD} 
	ARCH=$(dpkg --list | grep $BOARDPKG | awk '{print $4}')
	BOOTSCRIPT=$(realpath /boot/script.bin)

	# needed for ar
	apt-get install --yes binutils

	# copy and compress files to share
	mkdir -p $SHARE
	for FN in $(dpkg-query -L $BOARDPKG); do
	       	mkdir -p ${SHARE}$(dirname $FN)
		if [[ -f $FN ]]; then
			cp -p $FN ${SHARE}$FN
		fi
	done
	rmdir ${SHARE}.

	# copy necessary files for default boot and armbian
	mkdir -p ${WD}/data/{boot,etc}
	cp -p $BOOTSCRIPT ${WD}/data/boot/script.bin
	cp -p /etc/armbian* ${WD}/data/etc
	cd ${SHARE}/etc 
	cp -rp initramfs ${WD}/data/etc
	cp -rp kernel ${WD}/data/etc

	find $SHARE -type f -exec gzip '{}' \;

	# remove 
	apt-get remove --purge --auto-remove --yes $BOARDPKG

	# control files
	mkdir -p ${WD}/control
	cat <<-EOF > ${WD}/control/control
	Package: linux-${RELEASE}-minimal-root-${DEB_BRANCH}${BOARD}
	Version: $VERSION
	Architecture: $ARCH
	Maintainer: lrrr <lrrr@armbian.com>
	Installed-Size: 1
	Section: kernel
	Priority: optional
	Depends: bash, linux-base, u-boot-tools, initramfs-tools
	Provides: armbian-bsp
	Conflicts: armbian-bsp, linux-${RELEASE}-root-${DEB_BRANCH}${BOARD}
	Replaces: base-files, armbian-tools-$RELEASE
	Description: Minimal Armbian root for $RELEASE on $BOARD ($BRANCH branch)
	EOF

	cat <<-EOF > ${WD}/control/preinst
	#!/bin/sh
	rm -f /boot/script.bin
	exit 0
	EOF

	chmod 0755 ${WD}/control/preinst
	echo "2.0" > ${WD}/debian-binary

	# create and install deb
	tar -cpzf ${WD}/control.tar.gz -C ${WD}/control .
	tar -cpzf ${WD}/data.tar.gz -C ${WD}/data .
	cd $WD
	ar r amr.deb debian-binary control.tar.gz data.tar.gz

	dpkg -i ${WD}/amr.deb
	# encase apt is broken
	apt-get install --fix-broken --yes

	# cleanup
	apt-get remove --auto-remove --yes binutils
	cd /tmp
	rm -rf $WD
}

NetInst() {
	MinimalRoot

	# remove wireless module
	sed -i 's/xradio_wlan/#xradio_wlan/' /etc/modules

	# without-systemd
	if [[ $WITHOUT_SYSTEMD == yes ]]; then
		WithoutSystemd
	else
		systemctl --no-reload disable systemd-timesyncd.service >/dev/null 2>&1
		systemctl --no-reload enable network.service >/dev/null 2>&1
	fi

	# network interfaces
	if [[ -f /etc/network/interfaces ]]; then
		cp -p /etc/network/interfaces /etc/network/interfaces.old
	fi
	cat <<-EOF > /etc/network/interfaces
	auto lo
	iface lo inet loopback
	
	auto eth0
	iface eth0 inet dhcp
	EOF

	# restore motd
	rm -f /etc/motd
	cp -p /usr/share/base-files/motd /etc

	# clean up
	apt-get clean
}

WithoutSystemd() {
	# http://without-systemd.org

	PACKAGES="sysvinit-core"
	if [[ $RELEASE == jessie ]]; then
		PACKAGES="$PACKAGES sysvinit-utils"
	fi

	# remove systemd
	systemctl --no-reload disable firstrun.service resize2fs.service armhwinfo.service log2ram.service firstrun-config.service
	apt-get remove --purge --auto-remove --yes systemd

	# add sysvinit
	apt-get install --yes $PACKAGES
	cp -p /usr/share/sysvinit/inittab /etc/inittab
	printf "Package: *systemd*\nPin: release *\nPin-Priority: -1\n" > /etc/apt/preferences.d/systemd
	insserv

	if [[ $SERIAL_CONSOLE == yes ]]; then
		sed -i 's#\#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100#T0:23:respawn:/sbin/getty -L ttyS0 115200 vt100#' /etc/inittab
	fi
}

Summary() {
	printf "$RELEASE $BOARD network-install"
	if [[ $WITHOUT_SYSTEMD == yes ]]; then
		printf " without-systemd"
	fi
	printf "\n"
}

ChrootImage() {
	printf "Entering chroot for "
	Summary
	bash
}

case $RELEASE in
	jessie|stretch)
		if [[ $BOARD == orangepizero ]]; then
			NetInst
			Summary
		else
			echo "no good"
		fi
		;;
	*)
		echo "no good"
		;;
esac

CustomizeImage
