#!/bin/bash # Copyright 2011 Magellan-Linux # Distributed under the terms of the GNU General Public License v2 # $Id$ # Author: Niels Rogalla # based on opengl-update from Gentoo-Linux # hasq() { local x local me=${1} shift for x in $@ do [[ ${x} = ${me} ]] && return 0 done return 1 } check_user() { if [[ $(id -u) -ne 0 ]] then echo "${0}: Must be run as root." exit 1 fi } get_current_implem() { local implem if [[ -f ${ENV_D} ]] then source ${ENV_D} if [[ -n ${DRI_PROFILE} ]] then implem="${DRI_PROFILE}" fi unset DRI_PROFILE fi echo ${implem} } get_implementations() { local implems for dir in ${PREFIX}/lib{,32,64}/mesa/* do if [[ -d ${dir} ]] && ! hasq ${dir##*/} ${implems} then implems=${implems:+${implems} }${dir##*/} fi done echo ${implems} } print_version() { echo "dri-update ${VERSION}" } print_usage() { # Get grammar right in message local IS_ARE IMPLEM_PLURAL if [[ $(echo ${AVAIL_IMPLEMS} | wc -w) -eq 1 ]] then IS_ARE="is" IMPLEM_PLURAL="" else IS_ARE="are" IMPLEM_PLURAL="s" fi print_version cat << FOO Usage: ${0##*/} [] Set the dri implementation. Valid options: --help|-h|-?: Prints this help. --version: Shows the version of this utility. --use-old: If an implementation is already set, use that one. --prefix=: Set the source prefix (default: /usr) --dst-prefix=: Set the destination prefix (default: /usr) Usage: ${0##*/} --get-implementation Print the current implementaion This utility switches between DRI implementations. There ${IS_ARE} $(echo ${AVAIL_IMPLEMS} | wc -w) available implementation${IMPLEM_PLURAL}: ${AVAIL_IMPLEMS} Examples: ${0##*/} classic This will setup things to use the classic DRI drivers. ${0##*/} gallium This will setup things to use the gallium DRI drivers. FOO exit 1 } parse_options() { local opt while [[ ${#} -gt 0 ]] do opt=${1} shift case ${opt} in --use-old) if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else if [[ -n "${CURRENT_DRI_IMPLEM}" ]] && hasq ${CURRENT_DRI_IMPLEM} ${AVAIL_IMPLEMS} then ACTION="old-implementation" fi fi ;; --get-implementation) if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else ACTION="get-implementation" fi ;; --prefix=*) PREFIX=${opt#*=} AVAIL_IMPLEMS=$(get_implementations) ;; --dst-prefix=*) DST_PREFIX=${opt#*=} ;; --help|-h|-?) ACTION="usage" ;; --version) ACTION="version" ;; *) if hasq ${opt} ${AVAIL_IMPLEMS} then if [[ ${ACTION} != old-implementation ]] then if [[ -n ${ACTION} ]] then ACTION="error" echo "Invalid usage." else ACTION="set-implementation" NEW_DRI_IMPLEM="${opt}" fi fi else echo "Unrecognized option: ${opt}" ACTION="error" fi ;; esac done } set_new_implementation() { local DRI_IMPLEM=${1} local DRI_LOCAL check_user # Set a sane umask... bug #83115 umask 022 if ! hasq ${DRI_IMPLEM} ${AVAIL_IMPLEMS} then echo "Invalid profile selected." exit 1 fi echo "Switching to ${DRI_IMPLEM} DRI drivers" rm -f ${ENV_D} &> /dev/null LIBDIRS="lib32 lib lib64" for LIBDIR in ${LIBDIRS} do # Special case handling of lib32 because it can be a symlink to # emul libs if [[ ${LIBDIR} = lib32 ]] then [[ -d ${PREFIX}/${LIBDIR}/mesa ]] || continue else [[ -d ${PREFIX}/${LIBDIR}/mesa ]] && [[ ! -h ${PREFIX}/${LIBDIR} ]] || continue fi # Fallback on classic if we don't have this implementation for this LIBDIR. if [[ ! -d ${PREFIX}/${LIBDIR}/mesa/${DRI_IMPLEM} ]] then DRI_LOCAL="classic" else DRI_LOCAL="${DRI_IMPLEM}" fi mkdir -p ${DST_PREFIX}/${LIBDIR}/dri pushd ${DST_PREFIX}/${LIBDIR}/dri &> /dev/null # First remove old symlinks for file in *_dri.so do [[ -h ${file} ]] && rm -f ${file} done for file in ${PREFIX}/${LIBDIR}/mesa/${DRI_LOCAL}/*_dri.so do [[ -f ${file} ]] || continue [[ -f ${file##*/} ]] && rm -f ${file##*/} ln -s ${file} done popd &> /dev/null done echo "DRI_PROFILE=\"${DRI_IMPLEM}\"" >> ${ENV_D} env-rebuild return 0 } ## START PROGRAM ## ENV_D="/etc/env.d/03dri" NEW_DRI_IMPLEM="" ACTION="" PREFIX="/usr" DST_PREFIX="/usr" AVAIL_IMPLEMS=$(get_implementations) CURRENT_DRI_IMPLEM=$(get_current_implem) VERSION="@@VERSION@@" parse_options ${@} case ${ACTION} in get-implementation) if [[ -n ${CURRENT_DRI_IMPLEM} ]] then echo ${CURRENT_DRI_IMPLEM} exit 0 else exit 2 fi ;; old-implementation) set_new_implementation ${CURRENT_DRI_IMPLEM} exit $? ;; set-implementation) if [[ -n ${NEW_DRI_IMPLEM} ]] then set_new_implementation ${NEW_DRI_IMPLEM} exit $? else print_usage exit 1 fi ;; version) print_version exit 0 ;; usage) print_usage exit 0 ;; error) print_usage exit 1 ;; *) print_usage exit 1 ;; esac