#!/bin/sh -e
#
# The main script for ALT Linux Homeros installation
# Copyright (C) 2010-2012 Eugene Prokopiev  <enp@altlinux.org>
# Copyright (C) 2011-2013      Michael Pozhidaev <msp@altlinux.org>

THIS="${0##*/}"

BOOT_DEV=
ROOT_DEV=
HOME_DEV=
SYS_USER=
NO_LILO=0
FS_TYPE=ext4
STAGE_LOG_FILE=/tmp/.homeros-install-last-stage.log

hi_problem()
{
    echo "$1" >&2
    exit 1
}

hi_print_help()
{
cat <<EOF
$THIS:the installer for ALT Linux Homeros distribution

Usage:
    $THIS [--help] [--no-lilo] [--user USERNAME] [--home DEVICE] [--boot DEVICE] [--root DEVICE]
EOF
}

while [ -n "$1" ]; do
    if [ "$1" == '--boot' ]; then
	BOOT_DEV="$2"
	shift
	shift
    elif [ "$1" == '--root' ]; then
	ROOT_DEV="$2"
	shift
	shift
    elif [ "$1" == '--home' ]; then
	HOME_DEV="$2"
	shift
	shift
    elif [ "$1" == '--no-lilo' ]; then
	NO_LILO=1
	shift
    elif [ "$1" == '--user' ]; then
	SYS_USER="$2"
	shift
	shift
    elif [ "$1" == '--help' ]; then
	hi_print_help
	exit 0
    else
	echo "$THIS:unknown parameter: $1" >&2
	exit 1
    fi
done

[ -z "$ROOT_DEV" ] && echo "$THIS:the device for root file system is not mentioned" >&2 && exit 1
[ -z "$BOOT_DEV" ] && echo "$THIS:the device for boot loader is not mentioned" >&2 && exit 1
[ -n "$HOME_DEV" ] && echo "$THIS:/home device support is not yet implemented" >&2 && exit 1

if ! [ -e "$ROOT_DEV" ]; then 
    echo "$THIS:$ROOT_DEV is not a valid device for root file system" >&2
    exit 1
fi

if ! [ -e "$BOOT_DEV" ]; then 
    echo "$THIS:$BOOT_DEV is not a valid device for boot loader installation" >&2
    exit 1
fi

if [ -n "$SYS_USER" ]; then
    if ! /usr/sbin/useradd "$SYS_USER" &> /dev/null; then
	echo "$THIS:invalid system user name: $SYS_USER" >&2
	exit 1
    fi
    usermod -G wheel,proc,cdrom,cdwriter,audio,users,scanner "$SYS_USER"
    subst s/altlinux/"$SYS_USER"/  /etc/inittab
    echo "$SYS_USER ALL=NOPASSWD:ALL" > /etc/sudo.d/"$SYS_USER"
    chmod 400 /etc/sudo.d/"$SYS_USER"
fi

export HOMEROS_INSTALL_BOOT_DEVICE="$BOOT_DEV"
export HOMEROS_INSTALL_ROOT_DEVICE="$ROOT_DEV"
export HOMEROS_INSTALL_HOME_DEVICE="$HOME_DEV"
export HOMEROS_INSTALL_NO_LILO="$NO_LILO"
export HOMEROS_INSTALL_SYSTEM_USER="$SYS_USER"

echo "Creating $FS_TYPE file system on $ROOT_DEV"
/sbin/mkfs."$FS_TYPE" "$ROOT_DEV" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:mkfs.$FS_TYPE failed (see $STAGE_LOG_FILE for complete utility output)"
/sbin/tune2fs -i 0 "$ROOT_DEV" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:tune2fs -i 0 failed for root partition (see $STAGE_LOG_FILE for complete utility output)"
/sbin/tune2fs -c 0 "$ROOT_DEV" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:tune2fs -c 0 failed for root partition (see $STAGE_LOG_FILE for complete utility output)"

TMP_DIR="$(/bin/mktemp -d)"
/bin/mount "$ROOT_DEV" "$TMP_DIR"

echo "Filling root partition with basic content"
/usr/libexec/homeros-install/scripts/hi-fill-root "$TMP_DIR" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:root directory filling failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for complete utility output)"

echo "Performing root partition clean up"
/usr/libexec/homeros-install/scripts/hi-post "$TMP_DIR"  || hi_problem "$THIS:root directory cleaning failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for complete utility output)"

echo "Generating lilo.conf"
/usr/libexec/homeros-install/scripts/hi-gen-lilo-conf "$BOOT_DEV" "$ROOT_DEV" > "$TMP_DIR/etc/lilo.conf" 2> "$STAGE_LOG_FILE" || hi_problem "$THIS:lilo.conf generation failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for generator error output)"

echo "Generating fstab"
/usr/libexec/homeros-install/scripts/hi-gen-fstab "$ROOT_DEV" > "$TMP_DIR/etc/fstab" 2> "$STAGE_LOG_FILE" || hi_problem "$THIS:/etc/fstab generation failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for generator error output)"

echo "Installing the linux kernel"
/usr/libexec/homeros-install/scripts/hi-kernel "$TMP_DIR" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:linux kernel installation failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for complete error information)"

if [ "$NO_LILO" != 1 ]; then
    echo "Installing lilo"
    /usr/libexec/homeros-install/scripts/hi-lilo "$TMP_DIR" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:lilo installation failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for complete error description)"
fi

echo "Executing local installation scripts"
/usr/libexec/homeros-install/scripts/hi-local-scripts "$TMP_DIR" &> "$STAGE_LOG_FILE" || hi_problem "$THIS:local scripts launch failed, $TMP_DIR is still mounted (see $STAGE_LOG_FILE for complete error description)"

if [ -n "$SYS_USER" ]; then
    subst s/altlinux/"$SYS_USER"/ "$TMP_DIR"/etc/inittab
    rm -f /etc/sudo.d/"$SYS_USER"
    echo "NOTE:You should delete altlinux user account from installed system manually!"
fi

cd /
/bin/umount "$ROOT_DEV"
/bin/rmdir "$TMP_DIR"
echo 'Everything is done successfully!'

