#!/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"
LOG=/var/log/xs-setup.log

# for public demo, disable any functions that would change the machine state
if [ -f /etc/sysconfig/xs-disable-config ]; then
    echo ""
    echo "======================================================"
    echo
    echo "This public demonstration server cannot be configured remotely"
    echo
    echo "======================================================"
    exit 0
fi

#  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

echo "called xs-domain-config on" | tee -a $LOG
date  2>&1 | tee -a $LOG

#  Parse arguments
new_domain_name=$1

#### reference future config file here ####
# for domain name

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 domain name to $new_domain_name" | tee -a $LOG

#  Apply each executable in the config directory
#  JV - sort places dhclient to be run first in the list
for file in `find $DOMAIN_CONFIG_DIR -maxdepth 1 -type f -perm /u+x ! -name "*~" -print | sort` ;
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 ;
    echo "xs-domain-config ran $file" | tee -a $LOG
done
