#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_services,v 1.10 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
#
#############################################################################
#
# Author: Fabian Herschel
#
#############################################################################
#
# make_services
# configrues the ip services file (/etc/services)
#
# Sections: services
# Tags:     ADD_SERVICES, DEL_SERVICES, SERVICES
#
############################################################################
#
# NOTES:
#    make_services configures the IP services file /etc/services.
#    The program searches for the most significant alice configuration
#    file.
#    You can use 'SERVICES="...."' to configure /etc/services to contain
#    only these service entries
#    You can use 'ADD_SERVICES="...."' to add service entries to the actual
#    /etc/services 
#
############################################################################
#

test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir

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

   esac
   shift
done   

. $alice_dir/lib/alicerc

WELCOME
std_services_conf=/etc/services
#
############################################################################
#
# save the old configuration
#
test -f "${std_services_conf}" && BACKUP --error ${std_services_conf}
#cp ${std_inetd_conf} ${std_inetd_conf}.moved.$$ || ERROR NOSAVE inetd.conf ${std_inetd_conf}.moved.$$
#
# First priority is on a special inetd.con file
#
#config_file=$(get_config_file services.cfg) || FATAL NOCFG services.cfg
#. ${config_file}
GET_CONFIGURATION --require services $CFG_DEBUG
if [ -z "$ADD_SERVICES" -a -z "$DEL_SERVICES" -a -z "$SERVICES" ]
then
#    FATAL NOVALUE "ADD_SERVICES, DEL_SERVICES, SERVICES" $config_file
     GOOD_BYE
fi
touch ${std_services_conf}.alice 
#
# if "SERVICES" is defined neither ADD_SERVICES nor DEL_SERVICES
# could be defined
#
if [ -n "$SERVICES" ]
then
   if [ -n "$ADD_SERVICES" -o -n "$DEL_SERVICES" ]
   then
      WARNING OVERWRITES SERVICES "ADD_SERVICES, DEL_SERVICES"
   fi
   (
      echo "# "
      echo "# $std_services_conf"
      echo "# generated by ALICE"
      echo "$SERVICES" 
   )  > ${std_services_conf}.alice
else
   #
   # The systems default services file will be changed not
   # overwritten. The DEL_SERVICES is NOT implemented yet
   #
   if [ -n "$ADD_SERVICES" ]
   then
         (
           echo "# "
           echo "# $std_services_conf"
           echo "# generated by ALICE"
           cat ${std_services_conf}
           echo "$ADD_SERVICES"
         )  > ${std_services_conf}.alice   
   fi
fi
#
# Maybe a service is defined multiple, let us unify this
#
awk '
 /^#/    { print $0 }
 /^[^#]/ { 
           if ( printed[$1 "_" $2] != "x" ) {
              print $0
              printed[$1 "_" $2]="x"
           }
         }
 ' ${std_services_conf}.alice > ${std_services_conf}.alice.uni
# TODO: unify the services *and use* the unified output
#
#
#
CHANGED ${std_services_conf} || {
   BACKUP ${std_services_conf}
   mv ${std_services_conf}.alice ${std_services_conf} 
}
#
############################################################################
# Thats all
GOOD_BYE 
