Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # The MIT License (MIT)
- #
- # Copyright (c) 2015 Benjamin ROBIN
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in all
- # copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
- set -e
- # $1 : The system lib directory
- # $2 : The vendor lib directory
- # $3 : The basename of the file
- #
- # Print first the target and then the links
- function find_links_lib {
- local dir_sys="$1"
- local dir_vendor="$2"
- local lib_name="$3"
- local src_file=""
- local src_files=($(find "${dir_vendor}" -name "${lib_name}*" \( -type f -o -type l \)))
- local dst_files=($(readlink -q -e "${src_files[@]}" | sort -u))
- if [ ${#dst_files[@]} -ne 1 ] ; then
- echo "Unable to find unique canonical file name for: ${dir_vendor}/${lib_name}*" 1>&2
- exit 2
- fi
- echo "${dst_files[0]}"
- for src_file in "${src_files[@]}" ; do
- echo "${dir_sys}/${src_file##*/}"
- done
- }
- function find_links_xorg_nvidia {
- find_links_lib "/usr/lib/xorg/modules/extensions" "/usr/lib/nvidia/xorg/modules/extensions" "libglx.so"
- echo "/usr/lib/xorg/modules/extensions/libglx.so.1"
- }
- function find_links_xorg_mesa {
- echo "/usr/lib/xorg/modules/extensions/libglx.xorg"
- echo "/usr/lib/xorg/modules/extensions/libglx.so"
- }
- # Check links computed by find_links*
- function check_links {
- local target="${1}"
- shift
- local link_names=("${@}")
- local sys_file=""
- for sys_file in "${link_names[@]}" ; do
- if [ "$(readlink -q -e "$sys_file")" != "$target" ] ; then
- return 1
- fi
- done
- }
- # Create links computed by find_links*
- function create_links {
- local target="${1}"
- shift
- local link_names=("${@}")
- local sys_file=""
- for sys_file in "${link_names[@]}" ; do
- rm -f "$sys_file"
- ln -s "$target" "$sys_file"
- done
- }
- # $1 : The system lib directory
- function cleanup_links_lib {
- local dir_sys="$1"
- rm -f "$dir_sys/libEGL.so"*
- rm -f "$dir_sys/libGLESv1_CM.so"*
- rm -f "$dir_sys/libGLESv2.so"*
- rm -f "$dir_sys/libGL.so"*
- }
- function cleanup_links_xorg {
- rm -f /usr/lib/xorg/modules/extensions/libglx.so*
- }
- # $1 : The system directory name : lib ou lib32
- # $2 : The vendor directory name : mesa or nvidia
- function check_and_create_links_lib {
- local dir_sys="/usr/$1"
- local vendor="$2"
- local dir_vendor="$dir_sys/$vendor"
- local path_EGL=($(find_links_lib "$dir_sys" "$dir_vendor" libEGL.so))
- local path_GLESv1=($(find_links_lib "$dir_sys" "$dir_vendor" libGLESv1_CM.so))
- local path_GLESv2=($(find_links_lib "$dir_sys" "$dir_vendor" libGLESv2.so))
- local path_GL=($(find_links_lib "$dir_sys" "$dir_vendor" libGL.so))
- if check_links "${path_EGL[@]}" && \
- check_links "${path_GLESv1[@]}" && \
- check_links "${path_GLESv2[@]}" && \
- check_links "${path_GL[@]}" ; then
- echo "System $dir_sys is already using $vendor files"
- else
- cleanup_links_lib "$dir_sys"
- create_links "${path_EGL[@]}"
- create_links "${path_GLESv1[@]}"
- create_links "${path_GLESv2[@]}"
- create_links "${path_GL[@]}"
- fi
- }
- # $1 : The vendor directory name : mesa or nvidia
- function check_and_create_links_xorg {
- local vendor="$1"
- local path_Xorg=($("find_links_xorg_$vendor"))
- if check_links "${path_Xorg[@]}" ; then
- echo "Xorg is already using $vendor files"
- else
- cleanup_links_xorg
- create_links "${path_Xorg[@]}"
- fi
- }
- # $1 : The vendor directory name : mesa or nvidia
- function check_and_create_links {
- local vendor="$1"
- check_and_create_links_lib lib "$vendor"
- check_and_create_links_xorg "$vendor"
- [ -f "/usr/lib32/$vendor/libGL.so" ] && \
- check_and_create_links_lib lib32 "$vendor"
- }
- function detect_card {
- lspci | grep -Eq "VGA.*NVIDIA" && echo nvidia || echo mesa
- }
- function usage() {
- cat << EOF
- usage: $0 [options]
- This utility reconfigures the system to use mesa or nvidia lib
- OPTIONS:
- -h, --help Show this message
- -c, cleanup Remove any symbolique links inside system lib
- -m, mesa Configure to use mesa lib
- -n, nvidia Configure to use nvidia lib
- -a, auto Try to guess which card is enabled
- EOF
- }
- ###################################
- ### Argument parsing ###
- ###################################
- case "$1" in
- -c|--cleanup|cleanup)
- cleanup_links_xorg
- cleanup_links_lib /usr/lib
- cleanup_links_lib /usr/lib32
- ;;
- -m|--mesa|mesa)
- check_and_create_links mesa
- ;;
- -n|--nvidia|nvidia)
- check_and_create_links nvidia
- ;;
- -a|--auto|auto)
- check_and_create_links "$(detect_card)"
- ;;
- *)
- usage
- ;;
- esac
Advertisement
Add Comment
Please, Sign In to add comment