#!/bin/sh -e
# ***** BEGIN LICENSE BLOCK *****
# * Copyright (C) 2007  Alexey Gladkov <legion@altlinux.org>
# * Copyright (C) 2007  Dmitry V. Levin <ldv@altlinux.org>
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
# ***** END LICENSE BLOCK *****

WITH_ACTION=1
. install2-init-functions

charset='utf8'
cmdline="$(cat /proc/cmdline)"

init_splash() {
    splash_nbservices=
    for d in $cmdline; do
	    [ -z "${d%%splashcount=*}" ] || continue
	    splash_nbservices="${d#splashcount=}"
	    break
    done
    export splash_nbservices
}

update_splash() {
    [ -x /sbin/splash.sh ] || return 0
    splash_progress=$(($splash_nbservices-$1)) /sbin/splash.sh ||:
}

mount_fs() {
	mount-unionfs /var
	mount-unionfs /etc
}

set_alterator_datadir()
{
    ALTERATOR_DATADIR="/usr/share/alterator:$destdir/usr/share/alterator:$datadir"
    export ALTERATOR_DATADIR
}

set_duringinstall()
{
    DURING_INSTALL=1
    export DURING_INSTALL
}

set_locale() {
    LANG=
    export LANG

    local lang=
    for d in $cmdline; do
	[ -z "${d%%lang=*}" ] || continue
	lang="${d#lang=}"
	break
    done
    [ -n "$lang" ] || return 0
    locale -a |cut -d. -f1 |fgrep -xqs "$lang" || return 0

    LANG="$lang.$charset"
}

set_autoinstall()
{
    AUTOINSTALL=
    export AUTOINSTALL

    grep -qs '\(^\|[[:space:]]\)ai\([[:space:]]\|$\)' /proc/cmdline && AUTOINSTALL=1 || :
}

set_cdromdev()
{
    CDROMDEV=
    export CDROMDEV

    [ "$METHOD" = "cdrom" ] || return 0
    CDROMDEV="$(sed -n 's,^\(/dev/[^[:space:]]\+\) '"$image_dir"' .*,\1,p' /proc/mounts)"
    [ -b "$CDROMDEV" ] || fatal "cannot find cdrom device file $CDROMDEV"
}

if_run_debug_shell()
{
	fgrep -iwqs 'instdebug' /proc/cmdline || return 0
	echo 'Executing shell for debug purposes'
	HOME=/root bash -l
}

# Mount
action 'Mounting filesystems' mount_fs
init_splash
update_splash 6

# Define HOME and TMPDIR into writable place
export HOME=/tmp
export TMPDIR=/tmp
export INSTALLER=1

service udevd start
update_splash 5

set_alterator_datadir
set_locale
update_splash 4
set_autoinstall
update_splash 3
set_duringinstall
update_splash 2
set_cdromdev
update_splash 1

initinstall
update_splash 0

if_run_debug_shell
rc=0

if [ -n "$AUTOINSTALL" -a -s '/tmp/metadata/autoinstall.scm' ]; then
	echo 'Starting autoinstaller...'
	alterator-autoinstall /tmp/metadata/autoinstall.scm || rc=$?
	printf "autoinstall done: rc=%s\n" "$rc"
else
    # Try to start Xserver with installer
    echo 'Starting Xorg server...'
    cmd="xinit /usr/sbin/alterator-install2 -- vt7 -dpms -ac -dpi 80 -logfile /tmp/x11.log"
    if ! $cmd >>/tmp/install2.log 2>&1; then
	for drv in fbdev vesa vga; do
		video_setup "$drv" /etc/X11/xorg.conf
		$cmd >>/tmp/install2.log 2>&1 && break || rc=$?
	done
    fi
fi

if_run_debug_shell
[ "$rc" -ne 0 ] || postinstall
exit $rc
