#!/bin/bash # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8 function display_help() { uecho "$scriptname: sélectionner le template approprié pour générer un fichier USAGE $scriptname [template options] Si aucun template ne correspond au nom ou à l'extension du fichier, les options sont analysées à la recherche d'un argument de la forme '-t template' ou '--template template'. Si un tel argument est trouvé, le template spécifié est utilisé." } if [ $# -eq 2 ]; then if [ "$1" == "--matches-template" ]; then exit 1 elif [ "$1" == "--matches-name" ]; then exit 1 elif [ "$1" == "--matches-ext" ]; then exit 1 fi fi #source /etc/ulib || exit 1 source "$(dirname "$0")/../ulib/ulib" || exit 1 urequire DEFAULTS [ $# -eq 1 -a "$1" == "--help" ] && exit_with display_help function __find_template() { if [ -z "$template" ]; then for nt in "${NAMES[@]}"; do splitpair "$nt" n t if [ "$name" == "$n" ]; then template="$t" break fi done fi if [ -z "$template" ]; then for et in "${EXTS[@]}"; do splitpair "$et" e t if [ "$ext" == "$e" ]; then template="$t" break fi done fi found=1 templ="$templdir/$template" if [ -z "$template" -o ! -f "$templ" ]; then found= if [ -n "$template" ] ; then for at in "${TEMPLS[@]}"; do splitpair "$at" a t if [ "$template" == "$a" ]; then templ="$templdir/$t" found=1 break fi [ -n "$found" ] || templ= done fi if [ -z "$found" -a -n "$template" ]; then for templ in "${templs[@]}"; do if [ -x "$templ" ] && "$templ" --matches-template "$template"; then found=1 break fi done [ -n "$found" ] || templ= fi if [ -z "$found" ]; then for templ in "${templs[@]}"; do if [ -x "$templ" ] && "$templ" --matches-name "$name"; then found=1 break fi done [ -n "$found" ] || templ= fi if [ -z "$found" ]; then for templ in "${templs[@]}"; do if [ -x "$templ" ] && "$templ" --matches-ext "$ext"; then found=1 break fi done [ -n "$found" ] || templ= fi fi # template trouvé? [ -n "$found" -a -x "$templ" ] } NAMES=() EXTS=() TEMPLATES=() TEMPLS=() templdir="$scriptdir" [ -f "$templdir/templates.conf" ] && source "$templdir/templates.conf" array_lsfiles templs "$templdir" file="$1"; shift name="$(basename "$file")" splitlsep "$name" . basename ext template= if ! __find_template; then # template pas trouvé par rapport au nom du fichier. analyser les arguments # manuellement à la recherche d'une option -t ou --template args=() while [ $# -gt 0 ]; do case "$1" in -t|--template) shift template="$1" shift break ;; -t*) template="${1#-t}" shift break ;; esac args=("${args[@]}" "$1") shift done [ -n "$template" ] || die "$file: impossible de trouver le template approprié (essayer avec -t)" # restaurer les arguments sans l'option analysée manuellement set -- "${args[@]}" "$@" # corriger éventuellement si un alias de template est utilisé for at in "${TEMPLATES[@]}"; do splitpair "$at" a t if [ "$template" == "$a" ]; then template="$t" break fi done # puis réessayer de trouver le template approprié __find_template || die "$file: Impossible de trouver le template $template${templ:+ ($(basename "$templ"))}" fi args=() [ -n "$template" ] && args=("${args[@]}" --template "$template") exec "$templ" "${args[@]}" "$file" "$@"