#!/bin/bash
#
CMD=$(basename $0)
CMDVER="1.0"
CMDSIG="$CMD v$CMDVER (2017/03)"

set -e -u

error()
{
	echo -e "\nerror: $*\n" >&2
}

[ "$(which lsb_release)" = "" ] && {
	error "'lsb_release' comman not found, is this a Debian system?"
	exit 1
}

lsb=$(lsb_release -a)
echo
echo "$lsb"
echo

echo "$lsb" | grep -q 'Distributor ID:.*Debian$' || {
	error "this isn't a Debian system"
	exit 1
}

echo "$lsb" | grep -q 'Codename:.*jessie$' || {
	error "this isn't a 'Debian/jessie' system"
	exit 1
}


file="/etc/apt/sources.list"

grep -q devuan $file || {
	[ -f $file.orig ] && {
		error "can't save $file to $file.orig (already exists)"
		exit 1
	}

	mv $file $file.orig
	echo -e "\nreplacing APT sources with Devuan one\n"
	echo "
deb http://auto.mirror.devuan.org/merged jessie main
deb http://auto.mirror.devuan.org/merged jessie-updates main
deb http://auto.mirror.devuan.org/merged jessie-security main
" >$file
	chown --reference $file.orig $file
	chmod --reference $file.orig $file

	apt-get update
	apt-get install -y --force-yes devuan-keyring
}

echo -n "
READY TO MIGRATE THIS SYSTEM TO DEVUAN JESSIE
PRESS [Return] TO CONTINUE OR Ctrl-C TO ABORT
: "
read x

apt-get update
apt-get dist-upgrade || {
	echo
	echo "DIST-UPGRADE NOT COMPLETED"
	echo
	echo "If the problem is 'Failed to stop lib-init-rw.mount:...'"
	echo "is a known issue: you need to reboot, and relaunch"
	echo "apt-get dist-upgrade"
	echo
	exit 1
}

file="/etc/apt/apt.conf.d/50ku-apt-no-recommends"
echo
echo "disabling Suggests & Reccomends in APT"
echo "(see $file)"
echo
echo '
APT::Install-Suggests "0";
APT::Install-Recommends "0";
APT::AutoRemove::SuggestsImportant "false";
APT::AutoRemove::RecommendsImportant "false";
' >$file

exit 0
