#!/bin/bash

# This file is part of Marionnet, a virtual network laboratory
# Copyright (C) 2007  Luca Saiu
# Copyright (C) 2007 2013  Jean-Vincent Loddo
# Copyright (C) 2007 2013  Université Paris 13

# 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.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


echo -n "Marionnet shutdown tuning... "

set -x

###########################################
#     Source-ing kernel command line      #
###########################################

# Read kernel command line variables into this shell's environment:
# Expected variables: hostname hostfs ubd0s
export $(tr </proc/cmdline ' ' '\n' | \grep "^[a-zA-Z][a-zA-Z0-9_]*[=]")


###########################################
#     Mrproper take care of /etc/hosts    #
###########################################

function remove_line_if_needed {
    local LINE="$1"
    local FILE="$2"
    local temporary_file

    if test -f "$FILE" && grep -q "^${LINE}$" "$FILE"; then
        temporary_file=$(mktemp /tmp/prepare_shutdown.XXXXXX) && \
           grep -v "^${LINE}$" "$FILE" > $temporary_file && \
           mv -f $temporary_file "$FILE"
    fi
}

# Remove the host name from /etc/hosts: the user might change it
# when the virtual machine is off:
remove_line_if_needed "127.0.0.1 $hostname" /etc/hosts

echo "done."
