#!/bin/sh
#
# $Id: alicerc2,v 1.22 2005/04/15 11:51:33 christian Exp $
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
#############################################################################
#
# Copyright (c) 2000 SuSE Linux Solutions AG, Eschborn, Germany
# All rights reserved.
#
# This file is part of ALICE, which was sponsored by
# T-Online International AG
#
# Author: Juergen Henge-Ernst
#
#############################################################################
#

#
#############################################################################
#
# Get the configurations file to a local dir
#
if [ -e /etc/rc.config.d/alice.rc.config ]
then
   . /etc/rc.config.d/alice.rc.config
fi
	
export ALICE_LOCAL_DIR=/tmp/alice

aquire_config_files()
{
    # URL-Format :  protocoll://location
    # protocoll:[nfs,file,scp,ftp,http,cvs]
    # location : depends on protocoll
    # nfs      : servername/exportpoint (don't use the : after servername, like it's done normaly for mount !!)
    # scp      : user@servername/path (don't use the : after servername, like it's done normaly for scp !!)
    #          : requires package ssh/openssh installed
    #          : not working at the moment, because no terminal can be attached at boottime
    # file     : path (don't forget the beginning /)
    # ftp/http : user:password@servername:port/path
    #          : requires package wget installed
    # cvs      : user@server/cvs_root?module=<module name>[&tag=<tag>][&proto=<protocoltype like ext, pserv ..>][&cvspas=<password taken from .cvspass>]
    #          : requires package cvs installed
    # dev      : path of device (like /dev/fd0) at that position a tar.gz must be readable
    typeset -i rc=0;

    if [ -z "ALICE_CONFIG_AGENT" ]
    then
	    ALICE_CONFIG_AGENT=ALICE
    fi

    {
	if [ -n "$ALICE_CFG_URL" ]; then
	    ADD2LOCAL_DIR=
	    if [ -e "$ALICE_LOCAL_DIR" ]; then
		if [ "$ALICE_LOCAL_DIR" != "/" ]
		then
		    rm -rf "$ALICE_LOCAL_DIR"
		fi
	    fi
	    logger -s -t ALICE "Try to get ALICE configuration data from: $ALICE_CFG_URL"

	    method=${ALICE_CFG_URL%%:*}
	    method=$(echo $method | tr '[:lower:]' '[:upper:]')
	    URL=${ALICE_CFG_URL#*:}
	    URL=${URL#//}
	    case $method in
		NFS ) # //server/path 
			url_server=${URL%%/*}
			url_path="/${URL#*/}"
			method=NFS
			mkdir $ALICE_LOCAL_DIR
			mount $url_server:$url_path $ALICE_LOCAL_DIR || rc=1
			;;
		SCP ) # //user@server/path 
			url_server=${URL%%/*}
		    url_path="/${URL#*/}"
		    method=SCP
		    /usr/bin/scp -r $url_server:$url_path $ALICE_LOCAL_DIR || rc=1
		    ;;
	    FILE ) # ///path 
		    url_path="/${URL#*/}"
		method=FILE
		ln -s $url_path $ALICE_LOCAL_DIR || rc=1
		;;
	DEVICE ) # //device-path?path=file-path
		params="${URL#*\?}&"
		dev_path="/${URL%\?*}"
		while [ -n "$params" ] 
		do
			param=${params%%&*}
			pname=${param%%=*}
			pval=${param#*=}
			eval param_${pname}=$pval
			params=${params#*&}
		done
		#echo "Have to mount $dev_path to /tmp/alice1"
		#echo "ALICE files can be found under ./$param_path"
		mkdir -p $ALICE_LOCAL_DIR
		mount $dev_path $ALICE_LOCAL_DIR || rc=1
		ADD2LOCAL_DIR="/$param_path"
		;;
		
	CVS ) # //user@server/CVS_root?modul=<modul_name>[&tag=<tag_name>]
		url_server=${URL%%/*}
		url_path="/${URL#*/}"
		cvs_root=${url_path%%\?*}
        # let us set a fall back for proto, if not set by the url
        proto=pserver
        tag=HEAD
		cvspwd=A
		# If the parameter isn't closed by a & the loop will run endless
		params="${url_path#*\?}&"
		
		while test "$params"; do
			param=${params%%&*}
			pname=${param%%=*}
			pval=${param#*=}
			eval $pname=$pval
			params=${params#*&}
		done
		if [ -n "$tag" ] ; then
			tag="-r $tag"
		fi
		mkdir -p $ALICE_LOCAL_DIR
		cd $ALICE_LOCAL_DIR
        # if the proto is ext we also set CVS_RSH to ssh
        # we do not support the rsh method!!
        case $proto in
			ext ) 
				export CVS_RSH=ssh
				;;
			pserver )
				# create the passwordfile for anonymous login
				if [ -e $HOME/.cvspass ] ; then
					mv $HOME/.cvspass $HOME/.cvspass.old
				fi
				echo ":$proto:$url_server:$cvs_root $cvspwd" >> $HOME/.cvspass
			;;
        esac
		/usr/bin/cvs -q -d ":$proto:$url_server:$cvs_root" \
			export $tag $module > /tmp/alice/cvs.log 2>&1
		if [ $? -ne 0 ]
		then
			rc=1
		fi
		ADD2LOCAL_DIR="/$module"
		;;
	DEV ) #///path to device
		url_path="/${URL#*/}"
		method=DEV
		mkdir $ALICE_LOCAL_DIR
		cd $ALICE_LOCAL_DIR
		tar -xzf $url_path || rc=1
		;;						
	FTP | HTTP | HTTPS )
		url_server=${URL%%/*}
		url_path="/${URL#*/}"
		server_part=${url_server#*@}
		server_part=${server_part%%:*}
		mkdir $ALICE_LOCAL_DIR
		cd $ALICE_LOCAL_DIR
		case "$ALICE_CONFIG_AGENT" in
			"SWDIST" )
echo "######################################################"
set -x
					lHOST=$(cat /etc/HOSTNAME)
						/usr/bin/sdacexp --alice_home="$ALICE_LOCAL_DIR" \
								--user="$lHOST" --passwd="$ALICE_CONFIG_AGENT_KEY" \
								--machine_export --system="$lHOST" \
								--url "$ALICE_CFG_URL" || rc=1
						find "$ALICE_LOCAL_DIR"
set +x
echo "######################################################"

						ADD2LOCAL_DIR=""
						;;
			"ALICE" | "" )
			 			/usr/bin/wget -np -r -l0 -nv -q "$method://$URL" || rc=1
						ADD2LOCAL_DIR=/$server_part$url_path
						;;
		esac
		;;
	esac
	export ALICE_HOME="${ALICE_LOCAL_DIR}${ADD2LOCAL_DIR}"
    fi
    if [ ! -d ${ALICE_HOME}/info ]
    then
	rc=2;
    fi
    } > /var/log/alice_get.log

    if [ $rc -ne 0 ]
    then
	MESSAGE FREETEXT "ALICE ERROR: Please see log file /var/log/alice_get.log"
    fi
    return $rc
}

cleanup_config_files ()
{
#
# remove the local store config-file, because they are no longer needed
#
if [ -n "$ALICE_CFG_URL" ]; then
	case $method in
	NFS ) 
		umount $ALICE_LOCAL_DIR
		rmdir $ALICE_LOCAL_DIR
		;;
	CVS )
		rm -rf "$ALICE_LOCAL_DIR"
		rm $HOME/.cvspass
		if [ -e $HOME/.cvspass.old ] ; then
			mv $HOME/.cvspass.old $HOME/.cvspass
		fi
		;;
	SCP | DEV | FTP | HTTP | HTTPS )
		rm -rf "$ALICE_LOCAL_DIR"
		;;
	FILE ) 
		rm -f "$ALICE_LOCAL_DIR"
		;;
	DEVICE )
		umount $ALICE_LOCAL_DIR
		rmdir $ALICE_LOCAL_DIR
        ;;
	esac
fi
}

