#!/bin/sh -f

. shell-config
. shell-var

po_domain="alterator-livecd"
alterator_api_version=1
source_file="${source_dir:-/image/live}"
target_dir="${target_dir:-/mnt/destination}"
preinstall_hooks_dir=/usr/lib/alterator/hooks/livecd-preinstall.d
install_pid=

slideshow_conf="/etc/alterator/slideshow.conf"
# Default slideshow timeout, sec
default_slideshow_step=30
default_slideshow_path="/usr/share/install2/slideshow"

export target_dir

. alterator-sh-functions

do_notify()
{
    alterator-mailbox-send "$1 \"$(write_string "$2")\""
    echo "message:$1 $2" >&2
    sleep 1
}

do_notify_error()
{
    do_notify error "$1"
}

do_notify_status()
{
    do_notify status "$1"
}

do_notify_stage()
{
    do_notify stage "$1"
}

##
# Calculate unpacked sqaushfs size
# 
sqfs_size() {
        local total=0 file_size=0

        local img="$1" && shift

        [ -f "$img" ] || return 1


        unsquashfs -ll "$img" 2>/dev/null | sed -e '1,3d' -e 's/[[:space:]]\+/ /g' | cut -f 3 -d ' ' | sed -e '/,/d' |
        {
            local total=0 file_size=0

            while read file_size; do
                ((total += file_size))
            done

            echo $total
        }

        return 0
}

##
# Return free space in bytes on mounted file system
#
free_space() {
        local target="$1" && shift

        [ -e "$target" ] || return 1

        df --block-size=1 "$target" | sed -e '1d' -e 's/[[:space:]]\+/ /g' | cut -d ' ' -f 4

        return 0
}

##
# unpack squashfs image
#

do_install()
{
    local img="$1";shift
    local dst="$1";shift
    local percent1=

    if ! [ -r "$img" ]; then
        do_notify_error "Can't read squashfs image $img"
        return 1
    fi

    if ! mkdir -p -- "$dst";then
        do_notify_error "Can't write to $dst directory"
        return 1
    fi

    do_notify_stage preinstall
    
    # calculate inodes per one percent
    percent1="$(unsquashfs -n -s "$img"  | grep inodes | sed -e 's/[^[:digit:]]\+//')"
    percent1="$((percent1/99))"

    # calculate size necessary for unpacked squashfs image
    local unpacked=$(sqfs_size "$img")

    if [ -z "$unpacked" ]; then
        do_notify_error "Can't calculate unpacked squashfs size"
        return 1
    fi
   
    local free=$(free_space "$dst")

    if [ $unpacked -gt $free ]; then
        do_notify_error "No free space to unpack squashfs image"
        return 1
    fi

    do_notify_stage install

    unsquashfs -force -no-progress -info -dest "$dst" "$img" 2>/dev/null |
    {
        local i=0
        local progress=0

        while read ignore; do
			[ $progress -gt 100 ] && continue
            ((i += 1))
            if [ $i -gt $percent1 ]; then
                do_notify_status "$((progress++))"
                i=0
             fi
        done
    }

    # Set right perms on root directory of installed system
    chmod 0755 "$dst"

    do_notify_stage postinstall

    for i in proc dev sys run; do
        mkdir -p -- "$dst/$i"
        mountpoint -q "$dst/$i" || mount --bind /$i "$dst/$i"
    done

    # Run preinstall scripts
    run-parts "$preinstall_hooks_dir"

    alteratord_socket_dir="/var/run/alteratord"
    # replace itself with alteratord from chroot
    [ -n "${dst:-}" ] || return
    chroot "$dst" service alteratord start
    mount -o bind "$dst/$alteratord_socket_dir" "$alteratord_socket_dir"

    # Mount root to target dir for access from chroot at finish stage
    mkdir -p "$dst/livecd-root"
    mount --rbind / "$dst/livecd-root" 2>/dev/null

    # wait until new alteratord is ready to use
    alterator-wait

    # notify interface about finish
    do_notify_stage done
    sync

    # stop old alteratord and kill itself
    sleep 1
    service alteratord stop


    return 0
}

### slideshow configuration
read_slideshow_conf()
{
    local step="$(shell_config_get "$slideshow_conf" step)"
    if [ -z "$step" ] || echo "$step" | egrep -vqs '^[[:digit:]]+$'; then
        step="$default_slideshow_step"
    fi
	write_string_param step "$step"

    local once="$(shell_config_get "$slideshow_conf" once)"
    if [ -n "$once" ] && shell_var_is_yes "$once"; then
        once='true'
    else
        once='false'
    fi
    write_bool_param once "$once"

    local url="$(shell_config_get "$slideshow_conf" url)"
    [ -n "$url" ] || url="$default_slideshow_path"
    write_string_param url "$url"
}

on_message()
{
        case "$in_action" in
                read)
                    case "$in__objects" in
                        slideshow-config)
                            read_slideshow_conf
                            ;;
                        *)
                        ;;
                    esac
                    ;;
                write)
                    if [ -n "$install_pid" ] && kill -0 "$install_pid" ;then
                        write_error "Process already running"
                        return
                    fi
                    do_install "$source_file" "$target_dir"&
                    install_pid=$!
                    ;;
        esac
}

message_loop
