#!/bin/bash

# $Id$
#
# (c) 2006 Henning Glawe <glaweh@debian.org>
# (c) 2009-2010 Thomas Lange  <lange@debian.org>

### BEGIN SUBROUTINE INFO
# Provides-Var:
# Requires-Var:    $FAI_CONFIG_SRC $FAI
# Suggests-Var:
# Short-Description: get $FAI directory from $FAI_CONFIG_SRC
### END SUBROUTINE INFO

if [ -z "$FAI_CONFIG_SRC" ]; then
	sendmon "TASKERROR get_fai_dir 21"
	echo "ERROR: No URL defined for the config space."
	echo "ERROR: Set \$FAI_CONFIG_SRC or use option -s."
	exit 21
fi

echo "FAI_CONFIG_SRC is set to $FAI_CONFIG_SRC"

# extract method to get the config dir
# matched string: "cvs[+ssh]://user@host/path/to/cvsroot module[=tag]"
#                  ^^^
#                 "nfs://host/path/to/exported/config"
#                  ^^^
# the "major" protocol name is the one up to the first "+", if it exists
method=$(expr match "$FAI_CONFIG_SRC" '\([^+]*\).*://')

if [ -z "$method" ]; then
	sendmon "TASKERROR get_fai_dir 22"
	echo "Error in get-config-dir: scheme undefined."
	exit 24
    echo
fi

# run get-config-dir-$method script if it exists
if which get-config-dir-$method &>/dev/null ; then
	get-config-dir-$method
else
	sendmon "TASKERROR get_fai_dir 22"
	echo "Error: get-config-dir-"$method "not found"
	exit 22
fi

if [ $method = "file" ]; then
    localpath=$(expr match "$FAI_CONFIG_SRC" '.*://\(/.*\)')
    export FAI=$localpath
fi

ln -sf $FAI /var/run/fai/current_config
if [ ! -d $FAI/class ]; then
	echo "WARNING: directory $FAI/class not found."
	sendmon "TASKERROR get_fai_dir 23"
	exit 23
fi
exit 0
