#!/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	only add distro pgp key import (comp check required)
  		and exits

  -r|--repos	add apt repositories and exits; note that the listfile
  		'klabs.list' will remain in /etc/apt/sources.list.d
		(otherwise will be removed at end), and exists
" >&2
	[ $# != 0 ] && echo -e "\n$*\n" >&2

	exit 1
}


cleanup()
{
	$remove_list_at_end && rm -f $aptfile
}


# (MAIN)

do_check_only=false
do_only_key_import=false
do_only_addrepos=false
remove_list_at_end=true
F_EXEC=true
dummy=""

while [ $# != 0 ]
do
  case $1 in
    -n|--dry-run)	F_EXEC=false ;;
    -c|--check)		do_check_only=true ;;
    -k|--key)		do_only_key_import=true ;;
    -r|--repos)		do_only_addrepos=true; remove_list_at_end=false ;;

    --)			break ;;
    -*)			usage "unknwown parm: $1" ;;
    *)			break ;;
  esac
  shift
done
[ $# != 0 ] && usage

if $F_EXEC
then
	dummy=
	[ $(id -u) = 0 ] || {
		echo -e "\nerror: you must be root to use this command\n" >&2
		exit 1
	}
else
	dummy="echo (dummy)"
fi

# 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)

[ "X$DISTRIB_KLABS_DISTRO" = "X" ] && {
	echo -e "\n** DISTRIB '$DISTRIB_FULL_ID' NOT SUPPORTED **\n" >&2
	exit 1
}

$do_check_only && exit 0


repodir="klabs/dists/$DISTRIB_KLABS_DISTRO"
compat_info=
[ "X$DISTRIB_KLABS_DISTRO" != "X$DISTRIB_CODENAME" ] && {
	compat_info="(COMPATIBLE)"
}


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

# 1. install required packages

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


# 2. add gpg key to apt

echo
echo "  retrieving KUBiC Labs gpg key ... "
$dummy wget -O /tmp/klabs-key.gpg https://$REPOSERVER/$repodir/KEY.gpg
echo

keyring="/etc/apt/keyrings/klabs.gpg"
dir=$(dirname "$keyring")
echo -n "  importing KUBiC Labs gpg key ... "

[ -d "$dir" ] || {
	$dummy mkdir -p "$dir"
	$dummy chmod 0755 "$dir"
}
if $F_EXEC
then
	gpg --dearmor --yes -o "$keyring" </tmp/klabs-key.gpg
	echo "ok"
else
	echo
	$dummy gpg --dearmor --yes -o "$keyring" "</tmp/klabs-key.gpg"
fi
$dummy chmod a+r "$keyring"
$dummy rm /tmp/klabs-key.gpg

$do_only_key_import && exit 0


# 3. add repos
 
echo -n "  adding KUBiC Labs repos in $aptfile ... "
if $F_EXEC
then
	echo "deb [signed-by=$keyring] http://$REPOSERVER/$repodir ./" >$aptfile
	echo "ok"
else
	echo
	$dummy echo "deb [signed-by=$keyring] http://$REPOSERVER/$repodir ./ >$aptfile"
fi
$dummy apt-get update

$do_only_addrepos && exit 0


# 4. set debconf in silent mode

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: "
$dummy read pause
$dummy dpkg-reconfigure debconf


# 5. install kusa package and related

[ "X$(which kusa)" = "X" ] && {
	echo -e "\n  installing kusa packages\n"; sleep 1
	$dummy 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
