#!/bin/bash
# Copyright 1999-2004 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# Author:  Derek Dolney <dolney@astro.physics.upenn.edu>
# Based on gcc-config by Martin Schlemmer <azarah@gentoo.org>

source /etc/init.d/functions.sh || {
	echo "$0: Could not source /etc/init.d/functions.sh!"
	exit 1
}

# Some variables you might want to know about:
# (* = C, F77, ...)
#
# C_PROFILE and F77_PROFILE: profile requested by user on command line
#
# C_CURRENT and F77_CURRENT: full path to current profile recorded in
#                            CONFIG_FILE

PROFILE_PATH=/usr/lib/lapack
CONFIG_FILE=${PROFILE_PATH}/current

usage() {

cat << "USAGE_END"
Usage: lapack-config [Option] [LAPACK Profile]
Change the current LAPACK profile, or give info about profiles.

Options:

  -p, --print-current-profile
                         Print currently used LAPACK profiles.

  -l, --list-profiles
                         Print a list of available profiles.

      --f77libs          Print flags to link with the LAPACK library for the
                         given/current LAPACK profile.

USAGE_END

	exit 1
}

if [ "$#" -lt 1 ]
then
	usage
fi

check_root() {

        if [ "$(id -u)" -ne 0 ]
        then
                eerror "$0: Must be root."
                exit 1
        fi

}

get_current_profile() {

	if [ -f ${CONFIG_FILE} ]
	then
		source ${CONFIG_FILE}
		C_CURRENT="${PROFILE_PATH}/${C_CURRENT}"
		F77_CURRENT="${PROFILE_PATH}/${F77_CURRENT}"
	fi
}    

print_current_profile() {

	local FP="none"
#	local CP="none"

	if [ -n "${F77_CURRENT}" ]
	then
		FP="${F77_CURRENT##*/}"
		FP="${F77_CURRENT##f77-/}"
	fi

#	if [ -n "${C_CURRENT}" ]
#	then
#		CP="${C_CURRENT##*/}"
#		CP="${C_CURRENT##c-/}"
#	fi

	echo
	echo "Current profile:"
	echo "F77 LAPACK: ${FP}"
#	echo "C LAPACK: ${CP}"
}

list_profiles() {

	local i=1

#	echo
#	echo "Available C profiles:"

#	for x in ${PROFILE_PATH}/c-*
#	do
#		if [ -f "${x}" ]
#		then
#			x=${x##*/}
#			x=${x/c-/}
#			echo "[${i}] ${x##*/}"
#			i=$((i + 1))
#		fi
#	done

	echo

	i=1
	echo "Available F77 profiles:"
	for x in ${PROFILE_PATH}/f77-*
	do
		if [ -f "${x}" ]
		then
			x=${x##*/}
			x=${x/f77-/}
			echo "[${i}] ${x}"
			i=$((i + 1))
		fi
	done

}

set_c_profile() {

	check_root

	if [ -z "${C_PROFILE}" ]
	then
		usage
	else
		source ${PROFILE_PATH}/${C_PROFILE}
		setup
		C_PROFILE_CHANGED="yes"
	fi

}	

set_f77_profile() {

	check_root

	if [ -z "${F77_PROFILE}" ]
	then
		usage
	else
		source ${PROFILE_PATH}/${F77_PROFILE}
		setup
		F77_PROFILE_CHANGED="yes"
	fi

}	

list_cflags() {

	if [ -n "${C_PROFILE}" ]
	then
		source ${PROFILE_PATH}/${C_PROFILE}
		echo -n "${CFLAGS} "
	elif [ -n "${C_CURRENT}" ]
	then
		source ${C_CURRENT}
		echo -n "${CFLAGS} "
	else
		eerror "No C LAPACK profile is active."
	fi

}	

list_clibs() {

	if [ -n "${C_PROFILE}" ]
	then
		source ${PROFILE_PATH}/${C_PROFILE}
		echo -n "${CLIBS} "
	elif [ -n "${C_CURRENT}" ]
	then
		source ${C_CURRENT}
		echo -n "${CLIBS} "
	else
		eerror "No C LAPACK profile is active."
	fi
}

list_f77libs() {

	if [ -n "${F77_PROFILE}" ]
	then
		source ${PROFILE_PATH}/${F77_PROFILE}
		echo -n "${F77LIBS} "
	elif [ -n "${F77_CURRENT}" ]
	then
		source ${F77_CURRENT}
		echo -n "${F77LIBS} "
	else
		eerror "No F77 LAPACK profile is active."
	fi

}

TODO=""
C_PROFILE_CHANGED="no"
F77_PROFILE_CHANGED="no"

for x in $*
do
	case "${x}" in
		-p|--print-current-profile)
				TODO="${TODO}print_current_profile;"
			;;
		-l|--list-profiles)
				TODO="${TODO}list_profiles;"
			;;
#		-c|--set-c-profile)
#				if [ "${TODO}" != "${TODO#set_f77_profile}" ]
#				then
#					usage
#				else
#					TODO="${TODO}set_c_profile;"
#				fi
#			;;
#		-f|--set-f77-profile)
#				if [ "${TODO}" != "${TODO#set_c_profile}" ]
#				then
#					usage
#				else
#					TODO="${TODO}set_f77_profile;"
#				fi
#			;;
#		--cflags)
#				TODO="${TODO}list_cflags;"
#			;;
#		--clibs)
#				TODO="${TODO}list_clibs;"
#			;;
		--f77libs)
				TODO="${TODO}list_f77libs;"
			;;
		-*)
			eerror "$0: Invalid switch!  Run $0 without parameters for help."
			exit 1
			;;
		*)
			if [ -z "${PROFILE}" ]
			then
				for y in ${PROFILE_PATH}/*
				do
					[ "${y}" == "${CONFIG_FILE}" ] && continue
					if [ -f "${y}" ]
					then
						if [ "${x}" == "${y##*/}" ]
						then
							PROFILE="${x}"
							if [ "${PROFILE}" != "${PROFILE#c-}" ]
							then
								C_PROFILE="${PROFILE}"
							elif [ "${PROFILE}" != "${PROFILE#f77-}" ]
							then
								F77_PROFILE="${PROFILE}"
							fi
						else
							if [ "c-${x}" == "${y##*/}" ]
							then
								PROFILE="${x}"
								C_PROFILE="c-${x}"
							fi
							if [ "f77-${x}" == "${y##*/}" ]
							then
								PROFILE="${x}"
								F77_PROFILE="f77-${x}"
							fi
						fi
					fi
				done
				if [ -z "${PROFILE}" ]
				then
					eerror "$0: ${x} is not a valid profile!"
					exit 1
				fi
			else
				usage
			fi
			;;
	esac
done

get_current_profile

if [ -z "${TODO}" ]
then
	if [ -z "${PROFILE}" ]
	then
		usage
	else
#		if [ -n "${C_PROFILE}" ]
#		then
#			set_c_profile
#		fi
		if [ -n "${F77_PROFILE}" ]
		then
			set_f77_profile
	        fi
	fi
fi

eval ${TODO}
echo

NEW_CONFIG_FILE=""
if [ "${C_PROFILE_CHANGED}" == "yes" -o "${F77_PROFILE_CHANGED}" == "yes"  ]
then
#	if [ "${C_PROFILE_CHANGED}" == "yes" ]
#	then
#		NEW_CONFIG_FILE="C_CURRENT=\"${C_PROFILE}\""
#	elif [ -n "${C_CURRENT}" ]
#	then
#		NEW_CONFIG_FILE="C_CURRENT=\"${C_CURRENT##*/}\""
#	fi

	if [ "${F77_PROFILE_CHANGED}" == "yes" ]
	then
		NEW_CONFIG_FILE="F77_CURRENT=\"${F77_PROFILE}\""
	elif [ -n "${F77_CURRENT}" ]
	then
		NEW_CONFIG_FILE="F77_CURRENT=\"${F77_CURRENT##*/}\""
	fi

	echo -e "${NEW_CONFIG_FILE}" > ${CONFIG_FILE}
	exec /usr/sbin/env-update
fi

# vim:ts=8
