blaar.sh 7.82 KB
Newer Older
1
#!/usr/bin/env bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

# Basic Libraries And Applications for Robotics (BLAAR)
# Copyright  ETIS — ENSEA, University of Cergy-Pontoise, CNRS (2011 - 2017)
# Author: Arnaud Blanchard
#
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
# As a counterpart to the access to the source code and  rights to copy, modify and redistribute granted by the license,
# users are provided only with a limited warranty and the software's author, the holder of the economic rights,  and the successive licensors have only limited liability.
# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
# that may mean  that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.

set -o errexit  #Exit on first error
set -o nounset  #Error if a variable is used but not defined
set -o pipefail #Error if a pipe fail

20 21 22
blaar_program_name=$(basename $0)

#Create a temporary directory, essencially for logs
23
TMP_DIR="$(mktemp -d -t ${blaar_program_name}XXX)"
Arnaud Blanchard's avatar
Arnaud Blanchard committed
24
trap 'error=$?; rm -rf "$TMP_DIR"; [ $error = 0 ] || { echo; echo "BLAAR: error $error executing $blaar_program_name $*"; echo; exit $error; }' EXIT INT HUP TERM ABRT
25

26
calc(){
27
	echo $(echo "scale=6; $@"|bc -l)
28 29
}

30 31
test_run() {
  "$@" || {
Arnaud Blanchard's avatar
Arnaud Blanchard committed
32
	  error=$?
33
    echo executing: "$@"
Arnaud Blanchard's avatar
Arnaud Blanchard committed
34
    exit $error
35 36 37 38 39 40 41
  }
}

blaar_module(){

  [ $# -lt 2 ] && { echo "You need at least 2 arguments but you have $#:'$*'"; usage; exit 1; }

42
  hash $2 2>/dev/null || { echo; echo "Fail to execute '$*'" ; echo "You need to install https://framagit.org/blaar/$1"; echo; exit 2; }
43 44
  shift

Arnaud Blanchard's avatar
Arnaud Blanchard committed
45
  test_run "$@" 
46 47
}

48 49 50 51 52 53 54 55 56
blaar_init_options(){
   [ $# = 1 ] || { usage; echo "You need a program desciption line. But you do not have argument"; exit 1; }
   export blaar_options_nb=0
   export blaar_parameters_nb=0
   export blaar_program_desciption="$1"
   
}


57 58 59 60 61
blaar_add_option(){
  (( $# < 5 )) && { echo "Wrong arguments number with add_option. You use '$#' instead of 5 or 6 with '$*'"; exit 1; }
  (( $# > 6 )) && { echo "Wrong arguments number with add_option. You use '$#' instead of 5 or 6 with '$*'"; exit 1; }
  [ $3 ] && { echo "Longname not yet in implemented. Use \"\" instead of '$3'"; exit 1; }

62
  local index=$blaar_options_nb
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  blaar_options_variable[$index]="$1"
  blaar_options_letter[$index]="$2"
  blaar_options_longname[$index]="$3"
  blaar_options_parameter[$index]="$4"
  blaar_options_help[$index]="$5"
  
  export ${blaar_options_variable[$index]}=""
  
  if [ $# = 6 ]; then
    blaar_options_default[$index]="$6"
  else
	blaar_options_default[$index]=""
  fi
  blaar_options_nb=$(( $blaar_options_nb + 1 ))
}

80 81 82 83 84 85 86 87

blaar_add_parameter(){
  (( $# < 4 )) && { echo "Wrong arguments number with add_parameter. You use '$#' instead of 4 or 5 with '$*'"; exit 1; }
  (( $# > 5 )) && { echo "Wrong arguments number with add_parameter. You use '$#' instead of 4 or 5 with '$*'"; exit 1; }

  local index=$blaar_parameters_nb

  blaar_parameters_variable[$index]="$1"
88
  blaar_parameters_name[$index]="$2"
89 90 91 92 93 94 95 96 97 98 99 100 101
  blaar_parameters_required[$index]="$3"
  blaar_parameters_help[$index]="$4"  
  
  export ${blaar_parameters_variable[$index]}=""
  
  if [ $# = 5 ]; then
    blaar_parameters_default[$index]="$5"
  else
	blaar_parameters_default[$index]=""
  fi
  blaar_parameters_nb=$(( $blaar_parameters_nb + 1 ))
}

102 103 104
blaar_display_help(){
	echo
	[ $# = 0 ] || { echo "You should not have argument but you have '$*'"; exit 1; }
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	printf "usage: $(basename $0)"
    for ((i = 0; i < $blaar_options_nb; i++))
    do
        printf " [-${blaar_options_letter[$i]} ${blaar_options_parameter[$i]}]"
    done

    for ((i = 0; i < $blaar_parameters_nb; i++))
    do
        if (( ${blaar_parameters_required[$i]} > 0 ))
        then
        for ((j = 0; j<${blaar_parameters_required[$i]}; j++ ))
        do
            printf " ${blaar_parameters_name[$i]}"
        done
        elif (( ${blaar_parameters_required[$i]} == 0 ))
        then
            printf " [ ${blaar_parameters_name[$i]} ]"
        else
            printf " [ ${blaar_parameters_name[$i]} ...]"
        fi
    done
    echo
127 128
	echo
	echo $blaar_program_desciption
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    echo
    if (( $blaar_parameters_nb != 0))
    then
        echo "positional arguments:"
        for ((i = 0; i < $blaar_parameters_nb; i++))
        do
            if (( ${blaar_parameters_required[$i]} > 0 ))
            then
                for ((j = 0; j<${blaar_parameters_required[$i]}; j++ ))
                do
                    echo "  ${blaar_parameters_name[$i]}          ${blaar_parameters_help[$i]} "
            done
            elif (( ${blaar_parameters_required[$i]} == 0 ))
            then
                echo "  [ ${blaar_parameters_name[$i]} ]          ${blaar_parameters_help[$i]}"
            else
                echo "  [ ${blaar_parameters_name[$i]} ...]          ${blaar_parameters_help[$i]}"
            fi
        done
    fi
    echo
    if (( $blaar_options_nb != 0 ))
    then
        echo "optional arguments:"

        for ((i = 0; i < $blaar_options_nb; i++))
        do
156
		if [ ${blaar_options_default[$i]} ]; then
157 158 159 160 161 162 163
                default="(default: ${blaar_options_default[$i]})"
            else
                default=""
            fi
            echo " -${blaar_options_letter[$i]} ${blaar_options_parameter[$i]}          ${blaar_options_help[$i]} $default"
        done
    fi
164 165
}

166

167
blaar_parse_args(){
168 169 170 171 172
  #We say we need all default velue to remove them after when we read values
  for ((i = 0; i < $blaar_options_nb; i++))
  do
      default_needed_indexes[$i]=$i
  done
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  blaar_add_option help "h" "" "" "display this help"
  optstring=""
  for ((i = 0; i < $blaar_options_nb; i++))
  do
    optstring="$optstring"${blaar_options_letter[$i]}
    [ "${blaar_options_parameter[$i]}" ] && optstring="$optstring:"
  done
  args=$(getopt $optstring "$@")
  set -- $args
  while (( $# > 0 ))
  do
    for ((i = 0; i < $blaar_options_nb; i++))
    do
	  [ $1 = "-h" ] && { blaar_display_help; exit 0; }
      if [ $1 = "-${blaar_options_letter[$i]}" ]; then
         if [ ${blaar_options_parameter[$i]} ]; then
             export ${blaar_options_variable[$i]}=$2
			 unset default_needed_indexes[$i]
192
             shift
193 194 195
         else
           ${blaar_options_variable[$i]}="1"
         fi
196
		 shift;
197 198
         break
      fi
199
      [[ $i = $blaar_options_nb ]] && { blaar_display_help; echo "Unexpected option '$1'"; return 1; }
200
    done
201 202 203 204 205
  	if [ $1 = "--" ];then
		shift 
		break
	fi
	
206
  done
207 208 209

#We test than 'default_needed_indexes' exists (not null) :- is to avoid error if default_needed_indexes is unbound .
if [ -n "${default_needed_indexes:-}" ]; then
210 211 212 213 214
  #We set default values
  for i in ${default_needed_indexes[@]}
  do
		export ${blaar_options_variable[$i]}="${blaar_options_default[$i]}"
  done
215 216 217 218 219
  
  for ((i = 0; i < $blaar_parameters_nb; i++))
  do
	  if (( ${blaar_parameters_required[$i]} > $# ))
	  then  
220
		  [ -t 0 ] &&echo "${blaar_parameters_name[$i]}     ${blaar_parameters_help[$i]} ?"
221 222 223 224 225 226 227
		  read ${blaar_parameters_variable[$i]}
		  export ${blaar_parameters_variable[$i]}
	  else
	  	export ${blaar_parameters_variable[$i]}=$1
  	  	shift
	fi
  done
228
fi
229
  
230 231
  return 0
}
232 233