#!/bin/sh
set -u

source /lib/functions/mounts.sh
source /lib/functions/utils.sh

print_help() {
cat >&2 << EOF
Usage: nos-feature MODE [OPTIONS]...

    remove [FILENAME]
        Removes the feature unlock file FILENAME from the read only directory.
        Returns zero on success, non-zero on failure.

Example:
nos-feature remove 130

NOS feature unlock file manager.
EOF
}

remove_feature_file() {
    local file_name="${1:-}"
    local readonly_dir="$(get_key_value ReadOnly "/home/nos/paths.ini" "/etc/NOS")"
    local full_path="${readonly_dir}/features/${file_name}"
    local ret=0

    if [ ! -f "$full_path" ]; then
        return 1
    fi

    local mount_point="$(path_for_remount "$readonly_dir" rw)"
    if [ -n "$mount_point" ]; then
        mount -o rw,remount "$mount_point"
    fi

    # Delete the file
    rm "$full_path"
    $ret=$?

    sync
    if [ -n "$mount_point" ]; then
        mount -o ro,remount "$mount_point"
    fi
    return $ret
}

main() {
    case "${1:-}" in
    remove)
        shift
        remove_feature_file "$@"
        ;;
    --help)
        print_help
        ;;
    *)
        print_help
        exit 1
        ;;
    esac
}

main "$@"
