Create a gist now

Instantly share code, notes, and snippets.

prefix-builder.sh
#!/bin/bash
#
# Use
# eval $(prefix-builder.sh [add|rm|prepend|append] folder)
#
# or Alias such as:
# function eval_cmd() { eval $($*); }
# alias build-prefix="eval_cmd prefix-builder.sh"
# alias add-build-prefix="eval_cmd prefix-builder.sh add"
# alias rm-build-prefix="eval_cmd prefix-builder.sh rm"
#
# To get these commands:
# build-prefix [add|rm|prepend|append] folder
# add-build-prefix folder
# rm-build-prefix folder
#
# Copyright 2015, Marco Trevisan <mail@3v1n0.net>
# Released under GPLv3 terms
#
if [ "$1" != "add" ] && [ "$1" != "rm" ] && [ "$1" != "prepend" ] && [ "$1" != "append" ]; then
echo "echo You should either use 'prepend', 'append', 'add' or 'rm' operator";
exit 1;
fi
if [ -z "$2" ]; then
echo "echo You should provide a valid prefix";
exit 1;
elif [ ! -d "$2" ]; then
echo "echo $2 is not a valid folder"
exit 1;
fi
function strip_prefix()
{
local env=$1
local prefix=$2
local IFS=':'
read -a array <<< "$env";
for i in "${!array[@]}"; do
if [[ "${array[i]}" == "$prefix"* ]]; then
unset array[i];
fi
done
echo "${array[*]}"
}
function write_new_path()
{
local old_path="$1"
local new_path="$2"
local IFS=':'
read -a paths <<< "$old_path";
if [ $action == "append" ]; then
paths+=("$new_path")
else # add | prepend
paths=("$new_path" ${paths[@]})
fi
echo "${paths[*]}"
}
action=$1
PREFIX=${2%/}
arch_path=$(dpkg-architecture -qDEB_HOST_GNU_TYPE)
if [ "$action" != "rm" ]; then
echo export PATH=$(write_new_path "$PATH" "$PREFIX/sbin:$PREFIX/bin")
echo export CPATH=$(write_new_path "$CPATH" "$PREFIX/include")
# echo export MANPATH=$(write_new_path "$MANPATH" "$PREFIX/share/man")
# echo export INFOPATH=$(write_new_path "$INFOPATH" "$PREFIX/share/info")
echo export LIBRARY_PATH=$(write_new_path "$LIBRARY_PATH" "$PREFIX/lib")
echo export LD_LIBRARY_PATH=$(write_new_path "$LD_LIBRARY_PATH" "$PREFIX/lib:$PREFIX/lib/$arch_path")
echo export LD_RUN_PATH=$(write_new_path "$LD_RUN_PATH" "$PREFIX/lib")
echo export XDG_DATA_DIRS=$(write_new_path "$XDG_DATA_DIRS" "$PREFIX/share")
echo export XDG_CONFIG_DIRS=$(write_new_path "$XDG_CONFIG_DIRS" "$PREFIX/etc")
echo export PKG_CONFIG_PATH=$(write_new_path "$PKG_CONFIG_PATH" "$PREFIX/lib/pkgconfig:$PREFIX/lib/$arch_path/pkgconfig:$PREFIX/share/pkgconfig")
echo export PYTHONPATH=$(write_new_path "$PYTHONPATH" "$PREFIX/lib/python2.7/site-packages")
echo export GI_TYPELIB_PATH=$(write_new_path "$GI_TYPELIB_PATH" "$PREFIX/lib/girepository-1.0")
elif [ "$action" == "rm" ]; then
echo export PATH=$(strip_prefix "$PATH" "$PREFIX")
echo export CPATH=$(strip_prefix "$CPATH" "$PREFIX")
# echo export MANPATH=$(strip_prefix "$MANPATH" "$PREFIX")
# echo export INFOPATH=$(strip_prefix "$INFOPATH" "$PREFIX")
echo export LIBRARY_PATH=$(strip_prefix "$LIBRARY_PATH" "$PREFIX")
echo export LD_LIBRARY_PATH=$(strip_prefix "$LD_LIBRARY_PATH" "$PREFIX")
echo export LD_RUN_PATH=$(strip_prefix "$LD_RUN_PATH" "$PREFIX")
echo export XDG_DATA_DIRS=$(strip_prefix "$XDG_DATA_DIRS" "$PREFIX")
echo export XDG_CONFIG_DIRS=$(strip_prefix "$XDG_CONFIG_DIRS" "$PREFIX")
echo export PKG_CONFIG_PATH=$(strip_prefix "$PKG_CONFIG_PATH" "$PREFIX")
echo export PYTHONPATH=$(strip_prefix "$PYTHONPATH" "$PREFIX")
echo export GI_TYPELIB_PATH=$(strip_prefix "$GI_TYPELIB_PATH" "$PREFIX")
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment