#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_rcd,v 1.3 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2004 Novell, Inc.
# All rights reserved.
#
# 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: Dan Mills <thunder@ximian.com>
# 
############################################################################
# 
# make_rcd
# Configures the Red Carpet Daemon
#
# Sections: rcd
# Tags:     RCD_SET, RCD_ACTIVATE, RCD_SUBSCRIBE, RCD_UPDATE
#
############################################################################
#
# Notes:
# - Used tags
#   RCD_SET
#   Set rcd settings with rug set
#   RCD_ACTIVATE
#   Activate a key/email pair
#   RCD_SUBSCRIBE
#   Subscribe to channels
#   RCD_UPDATE
#   Update subscribed channels
# - Configuration file
#   rcd.tcf
# - Defaults
#   None.
#
# ONLY FOR alice2 (SLES8 and above)
#
############################################################################
#
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
#
############################################################################
#

GET_CONFIGURATION rcd $CFG_DEBUG

if [ -z "$RCD_SET" -a -z "$RCD_SERVICES" -a -z "$RCD_ACTIVATE" \
     -a -z "$RCD_SUBSCRIBE" -a -z "$RCD_UPDATE" ]; then
    GOOD_BYE
fi

# Check if rcd is already running
rug ping >/dev/null && running_rcd=1

# Start rcd if not running
if [ -z $running_rcd ]; then
    /etc/init.d/rcd start
    count=0
    until rug ping >/dev/null; do
	let count++
	if [ $count -ge 30 ]; then
	    break
	fi
	sleep 1
    done
    rug ping >/dev/null || FATAL CMDFAILED rcd
fi

# Configure rcd

# RCD "set" commands
if [ -n "$RCD_SET" ]; then
    echo "$RCD_SET" | while read var val; do
	# OK to run this multiple times
	rug set $var $val
    done
fi

# RCD services
if [ -n "$RCD_SERVICES" ]; then
    # First, remove all the existing services
    rug --terse service-list | (
	IFS="|"
	while read number url name; do
	    rug service-delete $url
	done
    )
    # Now add back the ones in the list
    echo "$RCD_SERVICES" | while read url; do
	rug service-add $url
    done
fi

# RCD activations
if [ -n "$RCD_ACTIVATE" ]; then
    echo "$RCD_ACTIVATE" | while read url key email; do
	# OK to run this multiple times
	# Deactivating from the client is hard, though, so we don't
	rug activate --service=$url $key $email
    done
fi

# RCD subscriptions
if [ -n "$RCD_SUBSCRIBE" ]; then
    # First, unsub from all channels
    rug --terse channels | (
	IFS="|"
	while read subscribed channel description; do
	    rug unsubscribe $channel
	done
    )
    # Now subscribe to the channels in the list
    echo "$RCD_SUBSCRIBE" | while read channel; do
	rug subscribe $channel
    done
fi

# RCD updates
if [ -n "$RCD_UPDATE" ]; then
    echo "$RCD_UPDATE" | while read channel; do
	# OK to run this multiple times
	rug update $channel
    done
fi

# Stop rcd if it wasn't running, on the assumption that it will be
# started later by the init scripts.
# We have to do this because of a bug in rcd.  if we leave it running
# and it is started again it will stop working at all (!)

if [ -z $running_rcd ]; then
    /etc/init.d/rcd stop
fi

#
############################################################################
# Thats all
GOOD_BYE 
