diff --git a/install/install_bins.sh b/install/install_bins.sh index 58aac8c..e7c4690 100755 --- a/install/install_bins.sh +++ b/install/install_bins.sh @@ -1,68 +1,84 @@ #!/bin/bash # Set SNAME, SDIR and SPATH if [ -h $0 ]; then SNAME=$(readlink $0); else SNAME=$0; fi SDIR=$(dirname $SNAME) if [[ "$SDIR" =~ ^/ ]]; then SPATH=$SDIR; else SPATH=$(realpath $(pwd)/$SDIR); fi # Set DIR_LIB DIR_LIB=$(realpath $SPATH/../lib/bash) +# DEBUG STUFF +#echo "SNAME: $SNAME" +#echo "SDIR: $SDIR" +#echo "SPATH: $SPATH" +#echo "DIR_LIB: $DIR_LIB" + # Source libs +source $DIR_LIB/tools.sh source $DIR_LIB/colors.sh source $DIR_LIB/io.sh # Set DESTINATION if [ -n "$1" ]; then DESTINATION="$(realpath $1)" else DESTINATION=$(realpath ${HOME}/bin) fi if [ ! -d $DESTINATION/ ]; then error_echo "'$DESTINATION' is not a valid directory." exit 1 fi info_echo "Using '$DESTINATION' as binary directory." +info_echo "To change binary directory, run \"$(basename $0) /path/to/your/bin_dir\"" + +confirm "Do you want to continue using \"$DESTINATION\" as binary directory?" function link_to_binaries() { for bin in $(echo $BINS); do if [ -r $DESTINATION/$bin ]; then if [ -h $DESTINATION/$bin ]; then OLD=$(realpath $DESTINATION/$bin) if [ "$OLD" == "$DIR_BIN/$bin" ]; then color_echo_n "Command '$bin' already exists... " else color_echo_n "Moving command '$bin' from '$OLD' to '$DIR_BIN/$bin'... " rm $DESTINATION/$bin ln -s $DIR_BIN/$bin $DESTINATION/$bin fi else error_echo "A similar command already exists: '$OLD'" exit 2 fi else color_echo_n "Installing command '$bin' to '$DESTINATION/'... " ln -s $DIR_BIN/$bin $DESTINATION/$bin fi [ -h $DESTINATION/$bin ] check_rc_echo $? - if [ $(which $bin 2> /dev/null | wc -l) -eq 0 ]; then + if [ $(command $bin 2> /dev/null | wc -l) -eq 0 ]; then warning_echo "Make sure '$DESTINATION' is in your \$PATH variable. (Looks like it is not the case.)"; fi done } DIR_BIN=$(realpath $SPATH/../bin) BINS='bamc' link_to_binaries DIR_BIN=$(realpath $SPATH/../local/epfl) BINS='search-epfl check-scipers' link_to_binaries +if [ "$OS" == "darwin" ]; then + if [ "$CLICOLOR" != "1" ]; then + info_echo "To have output colors in your terminal, set CLICOLOR=1 in your environment." + fi +fi + # RIP exit 0 diff --git a/lib/bash/cmdline_parser.sh b/lib/bash/cmdline_parser.sh index dd193b5..2fc79c3 100644 --- a/lib/bash/cmdline_parser.sh +++ b/lib/bash/cmdline_parser.sh @@ -1,134 +1,134 @@ #!/bin/bash # Reusable command line parser # USAGE: [in the calling script] # 1) set the global variables: $DEFAULT_ITEMS, $DEFAULT_PARAMS and $DEFAULT_ACTIONS # 2) set the global variable: $CALLBACK_PREFIX (default: cb_ ) # 3) implement each the callback: e.g. function cb_list() { ... } to implement the list action # 4) call: run $@ ACTIONS='' PARAMS='' ITEMS='' CALLBACK_PREFIX=${CALLBACK_PREFIX:-cb_} function parse_args() { while [ $# -gt 0 ]; do case $1 in "-o"|"--only") shift ITEMS=$1 shift continue ;; "-p"|"--params") shift PARAMS=$1 shift continue ;; *) if [ -z "$ACTIONS" ]; then ACTIONS=$1; else ACTIONS="$ACTIONS $1"; fi shift ;; esac done # Source local configuration (if any) function_exists 'source_local_configuration' if [ $? -eq 0 ]; then source_local_configuration; fi # Compute final ITEMS if [ -z "$ITEMS" ]; then ITEMS=$(get_default_items); fi if [ -z "$ITEMS" ]; then ITEMS='~no~items~'; fi # Compute final ACTIONS if [ -z "$ACTIONS" ]; then ACTIONS=$(get_default_actions); fi if [ -z "$ACTIONS" ]; then ACTIONS='help'; fi # Compute final PARAMS PARAMS=$(echo $PARAMS,$(get_default_params) | tr "," "\n" | sort -u | grep -v '^$' | tr "\n" "," | sed 's/,$//') debug "End of command line parsing. ACTIONS: '$ACTIONS' | ITEMS: '$ITEMS' | PARAMS: '$PARAMS'" } function get_items() { - echo "$ITEMS" | sed "s/,/\n/g" + echo "$ITEMS" | tr ',' "\n" } function get_actions() { - echo "$ACTIONS" | sed "s/ /\n/g" + echo "$ACTIONS" | tr ' ' "\n" } function get_params() { - echo "$PARAMS" | sed "s/,/\n/g" + echo "$PARAMS" | tr ',' "\n" } function get_parameter() { key=$1 # Key alone - line=$(echo $PARAMS | sed "s/,/\n/g" | grep "^$key$") + line=$(echo $PARAMS | tr ',' "\n" | grep "^$key$") if [ -n "$line" ]; then echo "set"; return; fi # Key + value - line=$(echo $PARAMS | sed "s/,/\n/g" | grep "^$key=" | cut -d '=' -f 2-) + line=$(echo $PARAMS | tr ',' "\n" | grep "^$key=" | cut -d '=' -f 2-) if [ -n "$line" ]; then echo $line; return; fi # unset echo "unset" } function is_set() { param=$1 value=$(get_parameter $param) if [ "$value" != "unset" ]; then return 1; else return 0; fi } function run() { local action local item local global_rc=0 rc parse_args $@ for action in $(get_actions); do rc=0 main_switch $action $item rc=$? if [ $rc -ne 0 ]; then echo "rc: $rc"; fi global_rc=$((global_rc+$rc)) done if [ $global_rc -ne 0 ]; then echo "global_rc: $global_rc"; fi return $global_rc } function get_default_items() { echo $DEFAULT_ITEMS } function get_default_params() { echo $DEFAULT_PARAMS } function get_default_actions() { echo $DEFAULT_ACTIONS } function check_action() { function_exists $1 return $? } function main_switch() { local action=$1 local function_to_call="${CALLBACK_PREFIX}${action}" check_action $function_to_call if [ $? -eq 0 ]; then debug "Calling action '$action' => '$function_to_call' on item(s) '$(get_items)'" $function_to_call $(get_items) return $? else error "invalid action: '$action' => '${CALLBACK_PREFIX}${action}' (not implemented ?)" return 1 fi return 0 } # EOF diff --git a/lib/bash/colors.sh b/lib/bash/colors.sh index 0488e98..5d9c007 100644 --- a/lib/bash/colors.sh +++ b/lib/bash/colors.sh @@ -1,48 +1,69 @@ #!/bin/bash if [ ! -t 1 ]; then return; fi if [ ! -t 2 ]; then return; fi check_OS -if [ $OS == 'darwin' ]; then return; fi - -# Define colors -Black="\e[0;30m" -DarkGray="\e[1;30m" -DarkBlue="\e[0;34m" -Blue="\e[1;34m" -DarkGreen="\e[0;32m" -Green="\e[1;32m" -Cyan="\e[0;36m" -LightCyan="\e[1;36m" -DarkRed="\e[0;31m" -Red="\e[1;31m" -Purple="\e[0;35m" -Pink="\e[1;35m" -Brown="\e[0;33m" -Yellow="\e[1;33m" -Gray="\e[0;37m" -White="\e[1;37m" -NoColor="\e[00m" +if [ $OS == 'darwin' ]; then + if [ "$CLICOLOR" == "1" ]; then + # Define colors + Black=$'\e[0;30m' + DarkGray=$'\e[1;30m' + DarkBlue=$'\e[0;34m' + Blue=$'\e[1;34m' + DarkGreen=$'\e[0;32m' + Green=$'\e[1;32m' + Cyan=$'\e[0;36m' + LightCyan=$'\e[1;36m' + DarkRed=$'\e[0;31m' + Red=$'\e[1;31m' + Purple=$'\e[0;35m' + Pink=$'\e[1;35m' + Brown=$'\e[0;33m' + Yellow=$'\e[1;33m' + Gray=$'\e[0;37m' + White=$'\e[1;37m' + NoColor=$'\e[00m' + fi +else + # Define colors + Black="\e[0;30m" + DarkGray="\e[1;30m" + DarkBlue="\e[0;34m" + Blue="\e[1;34m" + DarkGreen="\e[0;32m" + Green="\e[1;32m" + Cyan="\e[0;36m" + LightCyan="\e[1;36m" + DarkRed="\e[0;31m" + Red="\e[1;31m" + Purple="\e[0;35m" + Pink="\e[1;35m" + Brown="\e[0;33m" + Yellow="\e[1;33m" + Gray="\e[0;37m" + White="\e[1;37m" + NoColor="\e[00m" +fi COLORS=1; function colors() { echo -e ${Black}Black$NoColor echo -e ${DarkGray}DarkGray$NoColor echo -e ${DarkBlue}DarkBlue$NoColor echo -e ${LightBlue}Blue$NoColor echo -e ${DarkGreen}DarkGreen$NoColor echo -e ${Green}Green$NoColor echo -e ${Cyan}Cyan$NoColor echo -e ${LightCyan}LightCyan$NoColor echo -e ${DarkRed}DarkRed$NoColor echo -e ${Red}Red$NoColor echo -e ${Purple}Purple$NoColor echo -e ${Pink}Pink$NoColor echo -e ${Brown}Brown$NoColor echo -e ${Yellow}Yellow$NoColor echo -e ${Gray}Gray$NoColor echo -e ${White}White$NoColor echo -e ${NoColor}NoColor$NoColor }