101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
#!/bin/bash
 | 
						|
# -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | 
						|
 | 
						|
function display_help() {
 | 
						|
    uecho "$scriptname: Créer un nouveau fichier à partir d'un modèle
 | 
						|
 | 
						|
USAGE
 | 
						|
    $scriptname [options] file [template options]
 | 
						|
 | 
						|
OPTIONS
 | 
						|
Avant le nom du nouveau fichier, les options suivantes peuvent être utilisées:
 | 
						|
    -t TEMPLATE
 | 
						|
        Spécifier le modèle de fichier à utiliser. Par défaut, le modèle
 | 
						|
        à utiliser est déduit de l'extension ou du nom du fichier.
 | 
						|
    -e
 | 
						|
        Editer le fichier après l'avoir créé.
 | 
						|
 | 
						|
Après le nom du fichier, toutes les options sont spécifiques au modèle
 | 
						|
utilisé pour créer le nouveau fichier. Utiliser l'option --help pour
 | 
						|
avoir une description des options disponibles."
 | 
						|
}
 | 
						|
 | 
						|
source "$(dirname "$0")/ulib/ulib" &&
 | 
						|
urequire DEFAULTS ||
 | 
						|
exit 1
 | 
						|
 | 
						|
template=
 | 
						|
edit=
 | 
						|
overwrite=
 | 
						|
encoding=auto
 | 
						|
executable=auto
 | 
						|
opts=() # options non reconnues passées au template
 | 
						|
parse_opts + "${PRETTYOPTS[@]}" \
 | 
						|
    --help '$exit_with display_help' \
 | 
						|
    -t:,--template: template= \
 | 
						|
    -e,--edit edit=1 \
 | 
						|
    --no-edit edit= \
 | 
						|
    -f,--overwrite overwrite=1 \
 | 
						|
    -E:,--encoding: encoding= \
 | 
						|
    -x,--executable executable=1 \
 | 
						|
    -n,--no-executable executable= \
 | 
						|
    -a,-b,-c,-d,-g,-h,-i,-j,-k,-l,-m,-o,-p,-q,-r,-s,-u,-v,-w,-y,-z,-A,-B,-C,-D,-F,-G,-H,-I,-J,-K,-L,-M,-N,-O,-P,-Q,-R,-S,-T,-U,-V,-W,-X,-Y,-Z '$array_add opts "$option_"' \
 | 
						|
    @ args -- "$@" && set -- "${args[@]}" || die "$args"
 | 
						|
 | 
						|
[ -n "$template" ] || template=auto
 | 
						|
 | 
						|
TEMPLATES=()
 | 
						|
TEMPLS=()
 | 
						|
templdir="$scriptdir/lib/templates"
 | 
						|
if [ -f "$templdir/templates.conf" ]; then
 | 
						|
    source "$templdir/templates.conf"
 | 
						|
    for at in "${TEMPLATES[@]}"; do
 | 
						|
        splitpair "$at" a t
 | 
						|
        if [ "$template" == "$a" ]; then
 | 
						|
            template="$t"
 | 
						|
            break
 | 
						|
        fi
 | 
						|
    done
 | 
						|
fi
 | 
						|
array_lsfiles templs "$templdir"
 | 
						|
 | 
						|
file="$1"; shift
 | 
						|
found=1
 | 
						|
templ="$templdir/$template"
 | 
						|
if [ ! -f "$templ" ]; then
 | 
						|
    found=
 | 
						|
    for at in "${TEMPLS[@]}"; do
 | 
						|
        splitpair "$at" a t
 | 
						|
        if [ "$template" == "$a" ]; then
 | 
						|
            templ="$templdir/$t"
 | 
						|
            found=1
 | 
						|
            break
 | 
						|
        fi
 | 
						|
        [ -n "$found" ] || templ=
 | 
						|
    done
 | 
						|
    if [ -z "$found" ]; then
 | 
						|
        for templ in "${templs[@]}"; do
 | 
						|
            if [ -x "$templ" ] && "$templ" --matches-template "$template"; then
 | 
						|
                found=1
 | 
						|
                break
 | 
						|
            fi
 | 
						|
        done
 | 
						|
        [ -n "$found" ] || templ=
 | 
						|
    fi
 | 
						|
fi
 | 
						|
[ -n "$found" -a -x "$templ" ] || die "$file: Impossible de trouver le template $template${templ:+ ($(basename "$templ"))}"
 | 
						|
 | 
						|
args=()
 | 
						|
[ "$template" != "auto" ] && args=("${args[@]}" --template "$template")
 | 
						|
args=("${args[@]}" ${edit:+--edit} ${overwrite:+--overwrite})
 | 
						|
[ "$encoding" != "auto" ] && args=("${args[@]}" --encoding "$encoding")
 | 
						|
if [ "$executable" != "auto" ]; then
 | 
						|
    if [ -n "$executable" ]; then
 | 
						|
        args=("${args[@]}" --executable)
 | 
						|
    else
 | 
						|
        args=("${args[@]}" --no-executable)
 | 
						|
    fi
 | 
						|
fi
 | 
						|
array_extend args opts
 | 
						|
exec "$templ" "${args[@]}" "$file" "$@"
 |