#!/bin/bash
# -*- mode: sh; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# vim: et sts=4 sw=4

#  SPDX-License-Identifier: LGPL-2.1+
#
#  Copyright © 2020-2021 Collabora Ltd.
#  Copyright © 2020-2021 Valve Corporation.
#
#  This file is part of steamos-customizations.
#
#  steamos-customizations is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public License as
#  published by the Free Software Foundation; either version 2.1 of the License,
#  or (at your option) any later version.

set -euo pipefail

usage() {
    cat <<EOF
Usage: ${0##*/} enable|disable|status

Enable or disable read-only on the current running SteamOS.
EOF
}

needs_root() {
    if [[ "$(id -u)" -ne 0 ]]; then
        echo "$(basename "$0") needs to be run as root"
        exit 1
    fi
}

# mark root partition writable
read_write() {
    if ! status >/dev/null; then
        echo "Warning: The rootfs is already read-write!" >&2
        echo "         Nothing is performed." >&2
        return
    fi
    needs_root

    btrfs property set / ro false

    # Write token file to easily distinguish when the FS have been toggled. This
    # is a quick and dirty check (as used by steamos-systemreport) and by no
    # means an comprehensive solution.
    date > /.ROOTFS_RW
}

#
# mark root partition read-only
read_only() {
    if status >/dev/null; then
        echo "Warning: The rootfs is already read-only!" >&2
        echo "         Nothing is performed." >&2
        return
    fi
    needs_root

    sync /
    btrfs property set / ro true
}

status() {
    prop_val=$(btrfs property get / ro)
    if [[ $prop_val = "ro=true" ]]; then
        echo "enabled"
        return 0
    else
        echo "disabled"
        return 1
    fi
}


toggle() {
    if status >/dev/null; then
        read_write
    else
        read_only
    fi
    status
}

# determine file system type and set the fstype variable used above
get_fstype() {
    declare -r FSTYPE=$(findmnt -fn --output FSTYPE /)
    case "$FSTYPE" in
        btrfs)
            ;;
        *)
            echo "Unrecognized root filesystem type $FSTYPE"
            exit 1
    esac
}

get_fstype

case "${1:-}" in
    disable)
        action=read_write
        ;;
    enable)
        action=read_only
        ;;
    toggle)
        action=toggle
        ;;
    status)
        action=status
        ;;
    *)
        usage
        exit 1
esac

"$action"
