#!/bin/sh
#----------------------------------------------------------------------------
# /etc/init.d/mountvirtfs - initial boot script
#
# Creation   :  2015-06-13 hbfl
# Last Update:  $Id$
#
# Copyright (c) 2015-@@YEAR@@ the eisfair team, team(at)eisfair(dot)org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# mount virtdevices, e.g. /proc
#----------------------------------------------------------------------------

lsb=false

if ${lsb}
then
    # read init-functions
    . /lib/lsb/init-functions
else
    # read functions
    . /etc/init.d/functions
fi

# ---------------------------------------------------------------------------
# usage
# ---------------------------------------------------------------------------
usage()
{
cat <<EOF

 Usage:
 ${0}
                -q, --quiet      suppress all normal output
                [start]
EOF
}

# ---------------------------------------------------------------------------
# start
# ---------------------------------------------------------------------------
start()
{
    # Make sure /run is available before logging any messages
    # this has only effect with tmpfs for /run
    # it is also required for udev
    if ! /usr/bin/mountpoint -q /run
    then
        /usr/bin/mount /run || failed=1
    fi

    /usr/bin/mkdir -p /run/lock/subsys
#    /run/shm
    /usr/bin/chmod 1777 /run/lock
#    /run/shm

    if ${lsb}
    then
#        log_info_msg "Mounting virtual file systems: ${INFO}/run"
        log_info_msg "Mounting virtual file systems:"
    else
        boot_mesg " * Mounting virtual file systems: ${INFO}/run /proc /sys"
        ${ECHO} -e "${NORMAL}"
    fi

    if ! /usr/bin/mountpoint -q /proc
    then
        if ${lsb}
        then
            log_info_msg2 " ${INFO}/proc"
        fi
        /usr/bin/mount -o nosuid,noexec,nodev /proc || failed=1
    fi

    if ! /usr/bin/mountpoint -q /sys
    then
        if ${lsb}
        then
            log_info_msg2 " ${INFO}/sys"
        fi
        /usr/bin/mount -o nosuid,noexec,nodev /sys || failed=1
    fi


# this has only effect with udev and devtmpfs
#    if ! /usr/bin/mountpoint -q /dev
#    then
#        if ${lsb}
#        then
#            log_info_msg2 " ${INFO}/dev"
#        fi
#        /usr/bin/mount -o mode=0755,nosuid /dev  || failed=1
#    fi
#
#    /usr/bin/ln -sfn /run/shm /dev/shm

    (exit ${failed:-0})
    evaluate_retval
    exit ${failed:-0}
}

# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
main()
{
    while [ ${#} -gt 0 ]
    do
        case "${1}" in
        -q|--quiet)
            _quiet=true
            shift
            ;;
        *)
            _action=${1}
            shift
            ;;
        esac
    done

    case "${_action}" in
    start)
        start
        ;;
    *)
        usage
        exit 1
        ;;
    esac

    exit 0
}

# ---------------------------------------------------------------------------
# call function main
# ---------------------------------------------------------------------------
main "${@}"

# ---------------------------------------------------------------------------
# end
# ---------------------------------------------------------------------------