#!/usr/bin/env bash usage() { cat <&2 $(help_synopsis "${BASE_SOURCE[0]}" "[options] ") -l | --list-commands List all available commands -v Be verbose -h Show this help and exit $(help_end) EOS } LIST_COMMANDS=0 VERBOSE=0 source $(dirname ${BASH_SOURCE[0]})/nix-utils.sh # # Get the name of the script file for the command passed as argument # # Does not check whether the file exists. # script_for() { echo "$(dirname ${BASH_SOURCE[0]})/nix-script-${1}.sh" } SHIFT_ARGS=0 # # Increase the SHIFT_ARGS variable # shift_one_more() { SHIFT_ARGS=$(( SHIFT_ARGS + 1 )) } # # Shift N times the arguments, so: # # shift_n 5 a b c d e f # # will print "f" # shift_n() { for n in `seq 0 $1`; do shift; done echo $* } # # List all available commands # all_commands() { find $(dirname ${BASH_SOURCE[0]}) -type f -name "nix-script-*.sh" } # # Parse the arguments for this script # for cmd do case $cmd in "--list-commands" ) LIST_COMMANDS=1 shift_one_more ;; "-l" ) LIST_COMMANDS=1 shift_one_more ;; "-v" ) export VERBOSE=1 stdout "Verbose now" shift_one_more ;; "-h" ) usage exit 1 ;; * ) if [ ! -n $(script_for $cmd) ] then stderr "Unknown flag / command '$cmd'" exit 1 else if [ -z "$COMMAND" ] then stdout "Found command: '$cmd'" COMMAND=$cmd shift_one_more fi break fi esac done if [ $LIST_COMMANDS -eq 1 ] then stdout "Listing commands" for cmd in $(all_commands) do echo $(scriptname_to_command $cmd) done exit 0 fi [ -z "$COMMAND" ] && stderr "No command given" && exit 1 stdout "Searching for script for '$COMMAND'" SCRIPT=$(script_for $COMMAND) # # Error checking whether the script is available and executable. # [ ! -f $SCRIPT ] && stderr "Not available: $COMMAND -> $SCRIPT" && exit 1 [[ ! -x $SCRIPT ]] && stderr "Not executeable: $SCRIPT" && exit 1 stdout "Parsing args for '$COMMAND'" SCRIPT_ARGS=$(shift_n $SHIFT_ARGS $*) # # execute the script with its arguments # stdout "Calling: '$SCRIPT $SCRIPT_ARGS'" exec bash $SCRIPT $SCRIPT_ARGS