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

    # setup hooks
    hooks_dir=/usr/share/deepin-immutable-mount-root-hooks
    mkdir -p $hooks_dir/before_mount_usr_overlay
    cat > $hooks_dir/before_mount_usr_overlay/0-default << 'EOF'
#!/bin/sh
data_deploy_dir="$1"
lower_dir="$2"
upper_dir="$3"
if [ -f "$data_deploy_dir/merge_modification_flag" ];then
    echo "merge modification flag found"
    # timeout 10min to avoid merge modification stuck
    timeout 600 /usr/bin/deepin-immutable-init merge-modification "$lower_dir" "$upper_dir" || echo "merge modification failed" >&2
    rm -f $data_deploy_dir/merge_modification_flag
fi
EOF
    chmod 0755 $hooks_dir/before_mount_usr_overlay/0-default

    mkdir -p $hooks_dir/before_mount_var_overlay
    cat > $hooks_dir/before_mount_var_overlay/0-default << 'EOF'
#!/bin/sh
data_overlay_root="$1"
if [ -f "$data_overlay_root/var_upper_confirm_rollback_flag" ];then
    echo "var_upper_confirm_rollback_flag found"
    # timeout 10min to avoid merge var upper stuck
    timeout 600 /usr/bin/deepin-immutable-init merge-var-upper "$rootmnt/var" "$data_overlay_root/var-upper"

    rm -f "$data_overlay_root/var_upper_confirm_rollback_flag"
    rm -rf "$data_overlay_root/var-upper"
    rm -rf "$data_overlay_root/var-work"
fi
EOF
    chmod 0755 $hooks_dir/before_mount_var_overlay/0-default

    # shellcheck disable=SC2086
    MOUNT_ROOT_HOOK_ENABLED=1 /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
}
