#!/bin/sh
#
# This script calls a set of helper scripts to set/change
# the domain name on a server

#  Copyright 2008, One Laptop per Child
#  Author: John Watlington
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library 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 Library 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.

DOMAIN_CONFIG_DIR="/etc/sysconfig/olpc-scripts/domain_config.d/"
SYSCONFIGDIR="/etc/sysconfig"

#  Check the number of arguments and print usage if abnormal
if [ "$1" = '-h' -o $# -gt 1 ]; then
  echo "usage:  xs-domain-config [ <new_domain_name> ]"
  exit 0 ;
fi

#  Parse arguments
new_domain_name=$1
if [ -n "$new_domain_name" ];then
    echo $new_domain_name > "$SYSCONFIGDIR/xs_domain_name"
else
    if [ -e "$SYSCONFIGDIR/xs_domain_name" ];then
	new_domain_name=`cat "$SYSCONFIGDIR/xs_domain_name"`
    else
	echo "Using default domain name"
	new_domain_name="local"
    fi
fi
echo Setting the base dns name to $new_domain_name

#  Apply each executable in the config directory
for file in `find $DOMAIN_CONFIG_DIR -maxdepth 1 -type f -perm /u+x ! -name "*~" -print` ;
do
    # skip saved/backup files
    [ "${file%.cfsaved}" != "${file}" ] && continue
    [ "${file%.rpmsave}" != "${file}" ] && continue
    [ "${file%.rpmorig}" != "${file}" ] && continue
    [ "${file%.rpmnew}" != "${file}" ]  && continue
    [ "${file%.swp}" != "${file}" ]     && continue
    [ "${file%,v}" != "${file}" ]       && continue
    eval $file $new_domain_name ;
done
