#!/bin/ksh
#
# bootmode.sh -- install fstab for next boot
#
# bootmode.sh installs /etc/fstab.rw or /etc/fstab.ro as /etc/fstab
# so that the next boot mounts filesystems appropriately.
#
# bootmode.sh may have to remount / read-write to copy the file.
# If it has to do this, it tries to restore to readonly.
#
# If no arguments are given, prints current fstab and mount state.
#
# Copyright (c) 2021 Daniel Lawrence
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice, this
#    list of conditions and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e
Progname=$0

function usage {
    echo "usage: $Progname [rw|ro]" 1>&2
    exit $1
}

# getBootMode - prints the current boot file state
# echoes rw, ro or ""
function getBootMode {
    awk '$1 == "#bootmode" {print $2}' < /etc/fstab
}

# setBootMode - copy /etc/fstab.$1 to /etc/fstab
function setBootMode {
    cp /etc/fstab.$1 /etc/fstab.new
    mv /etc/fstab.new /etc/fstab
}

# getFsState - prints current filesystem state
# echoes rw, ro
function getFsState {
    mount | awk '
    $3 == "/" {
        print (match($0, "read-only") == 0) ? "rw" : "ro"
        exit
    }
    '
}

function main {
    typeset newMode=$1
    typeset currentMode
    typeset initialState

    if test -z $newMode
    then
        echo "filesystem: `getFsState`"
        echo "fstab: `getBootMode`"
        return 0
    fi
    if test $newMode = "-?" -o $newMode = "-h" -o $newMode = "--help"
    then
        usage 0
    fi
    if test $newMode != ro -a $newMode != rw
    then
        usage 2
    fi

    currentMode=`getBootMode`
    if test "$currentMode" = "$newMode"
    then
        echo "already $currentMode"
        return 0
    fi

    initialState=`getFsState`
    if test "$initialState" = ro
    then
        echo "temporarily remounting / rw"
        mount -uw /
    fi

    setBootMode "$newMode"

    if test "$initialState" = ro
    then
        echo "remounting / ro"
        mount -ur /
    fi
}

main "$@"
