#!/bin/bash
#
CMD=$(basename $0)
CMDVER="__TOOLKIT_SIGNATURE__"	# follows general kusa versioning
CMDSTR="$CMD v.$CMDVER"

REPOSERVER=${REPOSERVER:-repos.kubit.ch}

aptfile="/etc/apt/sources.list.d/klabs.list"

set -e -u

usage()
{
	echo "
== $CMDSTR == kusa install on clean machines ==

usage: $CMD [options]

options:
  -c|--check	do only compatibility check, exits true/false
  -k|--key	add distro pgp key import (comp check required) and exits
  -r|--repos	add apt repositories and exits
" >&2
	exit 1
}


cleanup()
{
	rm -f $aptfile
}


# (MAIN)

do_check_only=false
do_only_key_import=false
do_only_addrepos=false

while [ $# != 0 ]
do
  case $1 in
    -c|--check)	do_check_only=true ;;
    -k|--key)	do_only_key_import=true ;;
    -r|--repos)	do_only_addrepos=true ;;
    --)		break ;;
    -*)		usage ;;
    *)		break ;;
  esac
  shift
done
[ $# != 0 ] && usage


# ku-distroenv belongs to ku-base package, that is not installed
# yet, so you need to copy it from a source machine, too
#
ku_distroenv=
for path in $(dirname "$0") . /bin /usr/sbin
do
	[ -f $path/ku-distroenv ] && {
		ku_distroenv=$path/ku-distroenv
		break
	}
done
[ "X$ku_distroenv" = "X" ] && {
	echo -e "\nERROR: 'ku-distroenv' script not found\n"
	echo "  (hint: copy it from /bin directory on a machine with kusa installed\n" >&2
	exit 1
}
eval $($ku_distroenv)

case $DISTRIB_FULL_ID in
  Ubuntu-9.04)		;;
  Ubuntu-1[024].04)	;;
  Devuan-1.0)		;;
  Devuan-2.1)		;;
  Devuan-4)		;;
  Devuan-5)		;;

  # special cases
  pve-debian-12)
  	DISTRIB_CODENAME="daedalus"	# debian bookworm, forced to daedalus repos
  	;;
  *)		echo "error: distro '$DISTRIB_FULL_ID' not supported by 'kusa'" >&2; exit 1 ;;
esac

$do_check_only && exit 0


repodir="klabs/dists/$DISTRIB_CODENAME"


echo "
----------------------------------------------------------------------
 $CMDSTR
----------------------------------------------------------------------
   DISTRO:      $DISTRIB_FULL_ID
   CODENAME:    $DISTRIB_CODENAME
   REPOSERVER:  $REPOSERVER
   REPODIR:     $repodir
----------------------------------------------------------------------
"

apt-get update

[ "X$(which gpg)" = "X" ] && {
	echo "  missing 'gpg' package, install ... "
	apt-get install gpg
}
[ "X$(which rsync)" = "X" ] && {
	echo "  missing 'rsync' package, install ... "
	apt-get install rsync
}

echo -n "  importing KUBiC Labs gpg key ... "
wget -q -O - http://$REPOSERVER/$repodir/KEY.gpg | apt-key add -

$do_only_key_import && exit 0

 
echo -n "  adding KUBiC Labs repos in $aptfile ... "
echo "deb http://$REPOSERVER/$repodir ./" >$aptfile
echo "ok"
echo "  updating apt"; sleep 1
apt-get update

cp $aptfile /tmp
echo "  note: a copy of the apt file '$aptfile' was left in /tmp"

$do_only_addrepos && exit 0



trap 'cleanup' EXIT

echo -n "
   NOW SET DEBCONF IN SILENT MODE

   In the next screen you should tell debconf to be quiet, setting
   the interface to 'Non Interactive'. This way all the packages
   will be configured using default values, it's ok, since they
   will be reconfigured by 'kusa' on the run.

   Remember, if you need to restore the default (interactive)
   interface, run 'dpkg-reconfigure debconf' again.

   Press [Enter] to continue: "
read trush
dpkg-reconfigure debconf



[ "X$(which kusa)" = "X" ] && {

	echo "  installing kusa packages"; sleep 1
	apt-get install kusa
}

cleanup

posthints="/tmp/post-install-tasks.txt"

echo "
KUSA-BOOTSTRAP POST INSTALL TASKS
------------------------------------------------------------------------------

1. edit /etc/kusa/kusa.conf to accomodate your needs; check kusa.debug too

2. as first run, launch   kusa-reconf -b  to install default kusa-db entries

3. configure first, basic, modules: net and repositories

   kusa net base-repos

4. standardize the userbase and reboot the machine:

   kusa -s base-users base-users

5. configure the remaining modules

------------------------------------------------------------------------------
 (this file is saved as $posthints)
" >$posthints
cat $posthints

exit 0
