183 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			183 lines
		
	
	
		
			7.5 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| ##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| ## Charger nulib et rendre disponible les modules bash, awk, php et python
 | |
| ##@cooked nocomments
 | |
| # Ce fichier doit être sourcé en premier. Si ce fichier n'est pas sourcé, alors
 | |
| # le répertoire nulib doit être disponible dans le répertoire du script qui
 | |
| # inclue ce fichier.
 | |
| # Une fois ce fichier sourcé, les autres modules peuvent être importés avec
 | |
| # require:() e.g.
 | |
| #     source /etc/nulib.sh || exit 1
 | |
| #     require: other_modules
 | |
| # ou pour une copie locale de nulib:
 | |
| #     source "$(dirname "$0")/nulib/load.sh" || exit 1
 | |
| #     require: other_modules
 | |
| 
 | |
| # vérifier version minimum de bash
 | |
| if [ "x$BASH" = "x" ]; then
 | |
|     echo "ERROR: nulib: this script requires bash"
 | |
|     exit 1
 | |
| fi
 | |
| 
 | |
| function eerror() { echo "ERROR: $*" 1>&2; }
 | |
| function die() { [ $# -gt 0 ] && eerror "$*"; exit 1; }
 | |
| function edie() { [ $# -gt 0 ] && eerror "$*"; return 1; }
 | |
| function delpath() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; ${2:-PATH}"'="${'"${2:-PATH}"'#$1:}"; '"${2:-PATH}"'="${'"${2:-PATH}"'%:$1}"; '"${2:-PATH}"'="${'"${2:-PATH}"'//:$_qdir:/:}"; [ "$'"${2:-PATH}"'" == "$1" ] && '"${2:-PATH}"'='; }
 | |
| function addpath() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; "'[ "${'"${2:-PATH}"'#$1:}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'%:$1}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'//:$_qdir:/:}" == "$'"${2:-PATH}"'" -a "$'"${2:-PATH}"'" != "$1" ] && '"${2:-PATH}"'="${'"${2:-PATH}"':+$'"${2:-PATH}"':}$1"'; }
 | |
| function inspathm() { local _qdir="${1//\//\\/}"; eval "export ${2:-PATH}; "'[ "${'"${2:-PATH}"'#$1:}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'%:$1}" == "$'"${2:-PATH}"'" -a "${'"${2:-PATH}"'//:$_qdir:/:}" == "$'"${2:-PATH}"'" -a "$'"${2:-PATH}"'" != "$1" ] && '"${2:-PATH}"'="$1${'"${2:-PATH}"':+:$'"${2:-PATH}"'}"'; }
 | |
| function inspath() { delpath "$@"; inspathm "$@"; }
 | |
| 
 | |
| if [ ${BASH_VERSINFO[0]} -ge 5 -o \( ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 \) ]; then :
 | |
| elif [ -n "$NULIB_IGNORE_BASH_VERSION" ]; then :
 | |
| else die "nulib: bash 4.1+ is required"
 | |
| fi
 | |
| 
 | |
| # Calculer emplacement de nulib
 | |
| NULIBDIR="@@dest@@"
 | |
| if [ "$NULIBDIR" = "@@""dest""@@" ]; then
 | |
|     # La valeur "@@"dest"@@" n'est remplacée que dans la copie de ce script
 | |
|     # faite dans /etc. Sinon, il faut toujours faire le calcul. Cela permet de
 | |
|     # déplacer la librairie n'importe où sur le disque, ce qui est
 | |
|     # particulièrement intéressant quand on fait du déploiement.
 | |
|     NULIBDIR="${BASH_SOURCE[0]}"
 | |
|     if [ -f "$NULIBDIR" -a "$(basename -- "$NULIBDIR")" == load.sh ]; then
 | |
|         # Fichier sourcé depuis nulib/
 | |
|         NULIB_SOURCED=1
 | |
|         NULIBDIR="$(dirname -- "$NULIBDIR")"
 | |
|     elif [ -f "$NULIBDIR" -a "$(basename -- "$NULIBDIR")" == nulib.sh ]; then
 | |
|         # Fichier sourcé depuis nulib/bash/src
 | |
|         NULIB_SOURCED=1
 | |
|         NULIBDIR="$(dirname -- "$NULIBDIR")/../.."
 | |
|     else
 | |
|         # Fichier non sourcé. Tout exprimer par rapport au script courant
 | |
|         NULIB_SOURCED=
 | |
|         NULIBDIR="$(dirname -- "$0")"
 | |
|         if [ -d "$NULIBDIR/nulib" ]; then
 | |
|             NULIBDIR="$NULIBDIR/nulib"
 | |
|         elif [ -d "$NULIBDIR/lib/nulib" ]; then
 | |
|             NULIBDIR="$NULIBDIR/lib/nulib"
 | |
|         fi
 | |
|     fi
 | |
| elif [ "${BASH_SOURCE[0]}" = /etc/nulib.sh ]; then
 | |
|     # Fichier chargé depuis /etc/nulib.sh
 | |
|     NULIB_SOURCED=1
 | |
| fi
 | |
| NULIBDIR="$(cd "$NULIBDIR" 2>/dev/null; pwd)"
 | |
| NULIBDIRS=("$NULIBDIR/bash/src")
 | |
| 
 | |
| # marqueur pour vérifier que nulib a réellement été chargé. il faut avoir $NULIBINIT == $NULIBDIR
 | |
| # utilisé par le module base qui doit pouvoir être inclus indépendamment
 | |
| NULIBINIT="$NULIBDIR"
 | |
| 
 | |
| ## Modules bash
 | |
| NULIB_LOADED_MODULES=(nulib)
 | |
| NULIB_DEFAULT_MODULES=(base pretty sysinfos)
 | |
| 
 | |
| # Si cette variable est non vide, require: recharge toujours le module, même
 | |
| # s'il a déjà été chargé. Cette valeur n'est pas transitive: il faut toujours
 | |
| # recharger explicitement tous les modules désirés
 | |
| NULIB_FORCE_RELOAD=
 | |
| 
 | |
| function nulib__define_functions() {
 | |
|     function nulib_check_loaded() {
 | |
|         local module
 | |
|         for module in "${NULIB_LOADED_MODULES[@]}"; do
 | |
|             [ "$module" == "$1" ] && return 0
 | |
|         done
 | |
|         return 1
 | |
|     }
 | |
|     function module:() {
 | |
|         NULIB_MODULE="$1"
 | |
|         if ! nulib_check_loaded "$1"; then
 | |
|             NULIB_LOADED_MODULES+=("$1")
 | |
|         fi
 | |
|     }
 | |
|     function function:() {
 | |
|         :
 | |
|     }
 | |
| }
 | |
| 
 | |
| function nulib__load:() {
 | |
|     local nl__module nl__nulibdir nl__found
 | |
|     [ $# -gt 0 ] || set DEFAULTS
 | |
| 
 | |
|     for nl__module in "$@"; do
 | |
|         nl__found=
 | |
|         for nl__nulibdir in "${NULIBDIRS[@]}"; do
 | |
|             if [ -f "$nl__nulibdir/$nl__module.sh" ]; then
 | |
|                 source "$nl__nulibdir/$nl__module.sh" || die
 | |
|                 nl__found=1
 | |
|                 break
 | |
|             fi
 | |
|         done
 | |
|         [ -n "$nl__found" ] || die "nulib: unable to find module $nl__module in (${NULIBDIRS[*]})"
 | |
|     done
 | |
| }
 | |
| function nulib__require:() {
 | |
|     local nr__module nr__nulibdir nr__found
 | |
|     [ $# -gt 0 ] || set DEFAULTS
 | |
| 
 | |
|     # sauvegarder valeurs globales
 | |
|     local nr__orig_module="$NULIB_MODULE"
 | |
|     NULIB_MODULE=
 | |
| 
 | |
|     # garder une copie de la valeur originale et casser la transitivité
 | |
|     local nr__force_reload="$NULIB_FORCE_RELOAD"
 | |
|     local NULIB_FORCE_RELOAD
 | |
| 
 | |
|     for nr__module in "$@"; do
 | |
|         nr__found=
 | |
|         for nr__nulibdir in "${NULIBDIRS[@]}"; do
 | |
|             if [ -f "$nr__nulibdir/$nr__module.sh" ]; then
 | |
|                 nr__found=1
 | |
|                 if [ -n "$nr__force_reload" ] || ! nulib_check_loaded "$nr__module"; then
 | |
|                     NULIB_LOADED_MODULES+=("$nr__module")
 | |
|                     source "$nr__nulibdir/$nr__module.sh" || die
 | |
|                 fi
 | |
|                 break
 | |
|             fi
 | |
|         done
 | |
|         if [ -z "$nr__found" -a "$nr__module" == DEFAULTS ]; then
 | |
|             for nr__module in "${NULIB_DEFAULT_MODULES[@]}"; do
 | |
|                 if [ -f "$nr__nulibdir/$nr__module.sh" ]; then
 | |
|                     nr__found=1
 | |
|                     if [ -n "$nr__force_reload" ] || ! nulib_check_loaded "$nr__module"; then
 | |
|                         NULIB_LOADED_MODULES+=("$nr__module")
 | |
|                         source "$nr__nulibdir/$nr__module.sh" || die
 | |
|                     fi
 | |
|                 else
 | |
|                     break
 | |
|                 fi
 | |
|             done
 | |
|         fi
 | |
|         [ -n "$nr__found" ] || die "nulib: unable to find module $nr__module in (${NULIBDIRS[*]})"
 | |
|     done
 | |
| 
 | |
|     # restaurer valeurs globales
 | |
|     NULIB_MODULE="$nr__orig_module"
 | |
| }
 | |
| 
 | |
| # désactiver set -x
 | |
| NULIB__DISABLE_SET_X='local NULIB__SET_X; [ -z "$NULIB_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; NULIB__SET_X=1; }'
 | |
| NULIB__ENABLE_SET_X='[ -n "$NULIB__SET_X" ] && set -x'
 | |
| # désactiver set -x de manière réentrante
 | |
| NULIB__RDISABLE_SET_X='[ -z "$NULIB_NO_DISABLE_SET_X" ] && [[ $- == *x* ]] && { set +x; local NULIB_REQUIRE_SET_X=1; }; if [ -n "$NULIB_REQUIRE_SET_X" ]; then [ -n "$NULIB_REQUIRE_SET_X_RL1" ] || local NULIB_REQUIRE_SET_X_RL1; local NULIB_REQUIRE_SET_X_RL2=$RANDOM; [ -n "$NULIB_REQUIRE_SET_X_RL1" ] || NULIB_REQUIRE_SET_X_RL1=$NULIB_REQUIRE_SET_X_RL2; fi'
 | |
| NULIB__RENABLE_SET_X='[ -n "$NULIB_REQUIRE_SET_X" -a "$NULIB_REQUIRE_SET_X_RL1" == "$NULIB_REQUIRE_SET_X_RL2" ] && set -x'
 | |
| 
 | |
| function require:() {
 | |
|     eval "$NULIB__RDISABLE_SET_X"
 | |
|     nulib__define_functions
 | |
|     nulib__require: "$@"
 | |
|     eval "$NULIB__RENABLE_SET_X"
 | |
|     return 0
 | |
| }
 | |
| 
 | |
| ## Autres modules
 | |
| [ -d "$NULIBDIR/awk/src" ] && inspath "$NULIBDIR/awk/src" AWKPATH; export AWKPATH
 | |
| [ -d "$NULIBDIR/python3/src" ] && inspath "$NULIBDIR/python3/src" PYTHONPATH; export PYTHONPATH
 | |
| 
 | |
| ## Auto import DEFAULTS
 | |
| nulib__define_functions
 | |
| if [ -n "$NULIB_SOURCED" -a -z "$NULIB_NO_IMPORT_DEFAULTS" ]; then
 | |
|     require: DEFAULTS
 | |
| fi
 |