# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: $

# based on kernel.eselect

DESCRIPTION="Manage the Flash browser plugin symlink"
MAINTAINER="" # XXX
#SVN_DATE='$Date: $'
#VERSION=$(svn_date_to_version "${SVN_DATE}" )
VERSION="0.1"

plugindir="nsbrowser/plugins"
flashdir="flash"
flashlink="flashplugin.so"

# return the appropriate libdir based on command-line parameters
get_libdir() {
	local abi="${1}" libdirs

	# uses the same approach as java-nsplugin.eselect: hardcode
	# /usr/lib{,32,64}, since it's not entirely clear how to map the
	# abi parameter onto list_libdirs's results

	if [[ -z "${abi}" ]] ; then
		if [[ -d "${ROOT}/usr/lib" ]] ; then
			echo "${ROOT}/usr/lib"
		else
			die "Can't find libdir!  That's bad!"
		fi

	elif [[ "${abi}" == 64bit ]] ; then
		if [[ -d "${ROOT}/usr/lib64" ]] ; then
			echo "${ROOT}/usr/lib64"
		else
			die -q "\"64bit\" doesn't appear to be valid on your system"
		fi

	elif [[ "${abi}" == 32bit ]] ; then
		if [[ -d "${ROOT}/usr/lib32" ]] ; then
			echo "${ROOT}/usr/lib32"
		else
			die -q "\"32bit\" doesn't appear to be valid on your system"
		fi

	else
		die -q "Unrecognised ABI \"${abi}\""
	fi
}

# return the human-readable name for the specified plugin
human_name() {
	local plugin="${1}"
	plugin="${plugin##*/}"
	plugin="${plugin%.so}"
	echo "${plugin#npwrapper.}"
}

# find a list of Flash plugins in the specified libdir
find_targets() {
	local libdir="${1}" oldshopts

	oldnullglob="$(shopt -p nullglob )"
	shopt -s nullglob

	for p in ${libdir}/${plugindir}/${flashdir}/*.so; do
		human_name ${p}
	done

	${oldnullglob}
}

# try to remove the Flash plugin symlink from the specified libdir
remove_symlink() {
	local libdir="${1}"
	rm "${libdir}/${plugindir}/${flashlink}"
}

# set the kernel symlink in the specified libdir
set_symlink() {
	local libdir="${1}" target="${2}"

	if is_number "${target}" ; then
		targets=( $(find_targets "${libdir}" ) )
		target=${targets[$(( ${target} - 1 ))]}
	fi

	if [[ -z ${target} ]] ; then
		die -q "Target \"${target}\" doesn't appear to be valid!"

	elif [[ -f "${libdir}/${plugindir}/${flashdir}/${target}.so" ]] ; then
		pushd "${libdir}/${plugindir}" 1>/dev/null
		ln -s "${flashdir}/${target}.so" "${flashlink}"
		popd 1>/dev/null
	elif [[ -f "${libdir}/${plugindir}/${flashdir}/npwrapper.${target}.so" ]] ; then
		pushd "${libdir}/${plugindir}" 1>/dev/null
		ln -s "${flashdir}/npwrapper.${target}.so" "${flashlink}"
		popd 1>/dev/null

	else
		die -q "Target \"${target}\" doesn't appear to be valid!"
	fi
}

# returns the human-readable name of the Flash plugin in the specified libdir
get_current() {
	local libdir="${1}"
	human_name "$(canonicalise ${libdir}/${plugindir}/${flashlink} )"
}

### show action ###

describe_show_parameters() {
	echo "[ <abi> ]"
}

describe_show_options() {
	echo "abi : \"32bit\" or \"64bit\", only valid on multilib systems."
}

describe_show() {
	echo "Show the current Flash plugin symlink"
}

do_show() {
	local libdir="$(get_libdir "${1}")" plugin

	write_list_start "Current Flash plugin:"
	if [[ -L "${libdir}/${plugindir}/${flashlink}" ]] ; then
		write_kv_list_entry "$(get_current "${libdir}" )" ""
	else
		write_kv_list_entry "(unset)" ""
	fi
}

### list action ###

describe_list_parameters() {
	echo "[ <abi> ]"
}

describe_list_options() {
	echo "abi : \"32bit\" or \"64bit\", only valid on multilib systems."
}

describe_list() {
	echo "List available Flash plugin symlink targets"
}

do_list() {
	local libdir="$(get_libdir "$1")" plugin

	plugin="$(get_current "${libdir}" )"
	targets=( $(find_targets "${libdir}" ) )

	write_list_start "Available Flash plugins:"
	if [[ -n ${targets[@]} ]] ; then
		local i
		for (( i = 0 ; i < ${#targets[@]} ; i = i + 1 )) ; do
			[[ "${targets[${i}]}" == "${plugin}" ]] && \
				targets[${i}]="${targets[${i}]} $(highlight '*' )"
		done
		write_numbered_list "${targets[@]}"
	else
		write_kv_list_entry "(none found)" ""
	fi
}

### set action ###

describe_set_parameters() {
	echo "[ <abi> ] <target>"
}

describe_set_options() {
	echo "abi    : \"32bit\" or \"64bit\", only valid on multilib systems."
	echo "target : Target name or number (from 'list' action)"
}

describe_set() {
	echo "Set a new active Flash plugin symlink target"
}

do_set() {
	local libdir plugin

	if [[ $# -lt 2 ]] ; then
		libdir="$(get_libdir "" )"
		plugin="${1}"
	else
		libdir="$(get_libdir "${1}" )"
		plugin="${2}"
	fi

	if [[ -z "${plugin}" ]] ; then
		# no parameter
		die -q "You didn't tell me what to set the symlink to"

	elif [[ -L "${libdir}/${plugindir}/${flashlink}" ]] ; then
		# existing symlink
		if ! remove_symlink "${libdir}" ; then
			die -q "Couldn't remove existing symlink"
		elif ! set_symlink "${libdir}" "${plugin}" ; then
			die -q "Couldn't set a new symlink"
		fi

	elif [[ -e "${libdir}/${plugindir}/${flashlink}" ]] ; then
		# we have something strange
		die -q "Sorry, ${libdir}/${plugindir}/${flashlink} confuses me"

	else
		set_symlink "${libdir}" "${plugin}" || die -q "Couldn't set a new symlink"
	fi
}

#
# Local variables:
# mode: shell-script
# indent-tabs-mode: t
# tab-width: 4
# End:
