#!/bin/bash

# Copyright 2015 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function start()
{

    # prepare /etc/exports
    seq=0
    for i in "$@"; do
        echo "$i *(rw,sync,no_root_squash,insecure,fsid=$seq)" >> /etc/exports
        seq=$(($seq + 1))
        echo "Serving $i"
    done

    # from /lib/systemd/system/proc-fs-nfsd.mount
    mount -t nfsd nfds /proc/fs/nfsd

    # from /lib/systemd/system/nfs-config.service
    /usr/lib/systemd/scripts/nfs-utils_env.sh

    # from /lib/systemd/system/nfs-mountd.service
    . /run/sysconfig/nfs-utils
    /usr/sbin/rpc.mountd $RPCMOUNTDARGS

    # from /lib/systemd/system/nfs-server.service
    . /run/sysconfig/nfs-utils
    /usr/sbin/exportfs -r
    /usr/sbin/rpc.nfsd -N 2 -N 3 -V 4 -V 4.1 $RPCNFSDARGS

    echo "NFS started"
}

function stop()
{
    echo "Stopping NFS"

    # from /lib/systemd/system/nfs-server.service
    /usr/sbin/rpc.nfsd 0
    /usr/sbin/exportfs -au
    /usr/sbin/exportfs -f

    # from /lib/systemd/system/nfs-mountd.service
    kill $( pidof rpc.mountd )
    # from /lib/systemd/system/proc-fs-nfsd.mount
    umount /proc/fs/nfsd

    echo > /etc/exports
    exit 0
}


trap stop TERM

start "$@"

# Ugly hack to do nothing and wait for SIGTERM
while true; do
    read
done
