#!/bin/bash
# vim:set sw=4 ts=4:
#
# $Id: make_fstab,v 1.5 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005      SUSE GmbH, Nuernberg, Germany
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Authors: Juergen Henge-Ernst
#          Fabian Herschel
#
##############################################################################
#
# make_fstab
# Adds entries to the fstab automatically after installation if the mountpoint
# does not exist it is created
#
# Sections: fstab
# Tags:     FSTAB_ENTRIES
#
##############################################################################
#
usage()
{
   (
      echo "$_self -h | --help | -?"
      echo "$_self -fqhn fqhn"
   ) >&2
}
#
# General Alice environment stuff
#
test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir

while [ $# -gt 0 ]
do
   case $1 in
       -fqhn | --fqhn ) export fqhn=$2; shift
            ;;
       -h | --help | -? ) usage
                          exit 1
                          ;;
   esac
   shift
done

if test -f ${alice_dir}/lib/alicerc ; then . ${alice_dir}/lib/alicerc ; fi

std_fstab=/etc/fstab
#
# Welcome to the show
#
WELCOME
#
GET_CONFIGURATION fstab $CFG_DEBUG
if [ -z "${FSTAB_ENTRIES}" ]
then
   GOOD_BYE
fi
#
# Make sure we run as "root", otherwise we cant add accounts
#
if [ "$(whoami)" != "root" ]
then
    FATAL ROOTONLY
fi
#
# Get the actual fstab and the new entries
#
FSTAB=$(cat ${std_fstab}; echo "${FSTAB_ENTRIES}")
#
# Now we have to remove the doubles
# doubles have same device AND mount point (last wins)
#
echo "$FSTAB" | awk '
        BEGIN   { lines=1; }
        $1 ~ /^#/ { complete_line[lines]=$0; lines++}
        /^$/      { complete_line[lines]=$0; lines++}
        ($1 !~ /^#/) && ( $0 !~ /^$/) {
                a=1*line_nr[$1_$2];
                if ( a == 0 ) {
                        line_nr[$1_$2]=lines;
                        complete_line[lines]=$0; lines++
                } else {
                        complete_line[a]=$0
                }
        }
        END {
                for (i=1; i<lines; i++) {
                        print complete_line[i];
                }
        }' > ${std_fstab}.alice
#
# Now BACKUP the original file and install the
# new one, if the file have changed
#
CHANGED ${std_fstab} || {
    BACKUP --error ${std_fstab}
    mv ${std_fstab}.alice ${std_fstab}
}
#
# Thank you and good Night!
#
GOOD_BYE
