# Ostree mounting			-*- shell-script -*-

PERSISTENT_UUID_EXISTS=false
DISABLE_PERSISTENT_FLAGS=0

if [ -f "/conf/conf.d/persistent_uuid.conf" ]; then
    . /conf/conf.d/persistent_uuid.conf
    PERSISTENT_UUID_EXISTS=true
fi

if [ -f "/conf/conf.d/persistent_flags.conf" ]; then
    . /conf/conf.d/persistent_flags.conf
fi

ostree_mount_root()
{
    local_mount_root

    for param in $(cat /proc/cmdline)
    do
        case "${param}" in
        root=*)
		    ROOT="${param#root=}"
		    ;;
        rootflags=*)
            ROOTFLAGS="-o ${param#rootflags=}"
            ;;
        persistent=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT="${param#persistent=}"
            fi
            ;;
        persistentflags=*)
            if ! $PERSISTENT_UUID_EXISTS; then
                PERSISTENT_FLAGS="${param#persistentflags=}"
            fi
            ;;
        disable_persistent_flags=*)
            DISABLE_PERSISTENT_FLAGS="${param#disable_persistent_flags=}"
            ;;
        esac
    done

    cmd_param="--root=$ROOT"
    if [ -n "$ROOTFLAGS" ];then
        cmd_param="$cmd_param --rootflags=$ROOTFLAGS"
    fi
    
    if [ -n "${PERSISTENT}" ]; then
        cmd_param="$cmd_param --persistent=$PERSISTENT"
        if [ -n "${PERSISTENT_FLAGS}" ] && [ $DISABLE_PERSISTENT_FLAGS -eq 0 ]; then
            cmd_param="$cmd_param --persistentflags=$PERSISTENT_FLAGS"
        fi
    fi

    cmd_param="$cmd_param $rootmnt"

    # shellcheck disable=SC2086
    /usr/bin/deepin-immutable-mount-root $cmd_param

    # Copy status.list and set permissions to read-only
    cp $rootmnt/persistent/ostree/data/status.list /run/deepin-immutable-booted-status.list
    chmod 0444 /run/deepin-immutable-booted-status.list

    printf 'sysroot-ro\x00\x00\x00\x00\x00\x00\x01\x00\x62\x0b\x14' > /run/ostree-booted
}

mountroot()
{
    ostree_mount_root
}
