install.sh
5de2fbd6
 #! /bin/sh
 #
 # (c) 2015, coded by Nat!, Mulle KybernetiK
 #
 
56104254
 if [ "${MULLE_BOOTSTRAP_NO_COLOR}" != "YES" ]
 then
d2100287
    # Escape sequence and resets
    C_RESET="\033[0m"
b06853c1
 
d2100287
    # Useable Foreground colours, for black/white white/black
    C_RED="\033[0;31m"     C_GREEN="\033[0;32m"
    C_BLUE="\033[0;34m"    C_MAGENTA="\033[0;35m"
    C_CYAN="\033[0;36m"
1b26e970
 
d2100287
    C_BR_RED="\033[0;91m"
    C_BOLD="\033[1m"
b06853c1
 
d2100287
    #
    # restore colors if stuff gets wonky
    #
    trap 'printf "${C_RESET}"' TERM EXIT
56104254
 fi
b06853c1
 
149c439d
 
 fail()
 {
fb26a968
    printf "${C_BR_RED}$*${C_RESET}\n" >&2
149c439d
    exit 1
 }
 
b06853c1
 #
 # https://github.com/hoelzro/useful-scripts/blob/master/decolorize.pl
 #
5de2fbd6
 
6b373056
 #
 # stolen from:
 # http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
 # ----
 #
 _prepend_path_if_relative()
 {
    case "$2" in
       /* )
          echo "$2"
          ;;
       * )
          echo "$1/$2"
          ;;
    esac
 }
 
 
 resolve_symlinks()
 {
    local dir_context path
 
    path="`readlink "$1"`"
    if [ $? -eq 0 ]
    then
       dir_context=`dirname -- "$1"`
       resolve_symlinks "`_prepend_path_if_relative "$dir_context" "$path"`"
    else
       echo "$1"
    fi
 }
 
 
 _canonicalize_dir_path()
 {
     (cd "$1" 2>/dev/null && pwd -P)
 }
 
 
 _canonicalize_file_path()
 {
     local dir file
 
     dir="` dirname "$1"`"
     file="`basename -- "$1"`"
     (cd "${dir}" 2>/dev/null && echo "`pwd -P`/${file}")
 }
 
 
 canonicalize_path()
 {
    if [ -d "$1" ]
    then
       _canonicalize_dir_path "$1"
    else
       _canonicalize_file_path "$1"
    fi
 }
 
 
 realpath()
 {
    canonicalize_path "`resolve_symlinks "$1"`"
 }
 
 
58ddf0fd
 get_windows_path()
 {
    local directory
 
    directory="$1"
    if [ -z "${directory}" ]
    then
       return 1
    fi
 
    ( cd "$directory" ; pwd -PW )
    return 0
 }
 
 
 get_sh_windows_path()
 {
    local directory
 
    directory="`which sh`"
751a6002
    directory="`dirname -- "${directory}"`"
58ddf0fd
    directory="`get_windows_path "${directory}"`"
 
    if [ -z "${directory}" ]
    then
       fail "could not find sh.exe"
    fi
    echo "${directory}/sh.exe"
 }
 
 
 sed_mangle_escape_slashes()
 {
751a6002
    sed -e 's|/|\\\\|g'
58ddf0fd
 }
 
 
5de2fbd6
 prefix=${1:-"/usr/local"}
2d8f68ea
 [ $# -eq 0 ] || shift
6b373056
 prefix="`realpath "${prefix}"`"
 
5de2fbd6
 mode=${1:-755}
2d8f68ea
 [ $# -eq 0 ] || shift
5de2fbd6
 bin=${1:-"${prefix}/bin"}
2d8f68ea
 [ $# -eq 0 ] || shift
5de2fbd6
 libexec=${1:-"${prefix}/libexec/mulle-bootstrap"}
2d8f68ea
 [ $# -eq 0 ] || shift
5de2fbd6
 
 if [ "$prefix" = "" ] || [ "$bin" = "" ] || [ "$libexec" = "" ] || [ "$mode" = "" ]
 then
8f77be37
    echo "usage: install.sh [prefix] [mode] [binpath] [libexecpath]" >&2
5de2fbd6
    exit 1
 fi
 
149c439d
 if [ ! -d "${bin}" ]
 then
    mkdir -p "${bin}" || fail "could not create ${bin}"
 fi
 if [ ! -d "${libexec}" ]
 then
    mkdir -p "${libexec}" || fail "could not create ${libexec}"
 fi
 
5de2fbd6
 
 for i in mulle*bootstrap
 do
    sed "s|/usr/local/libexec/mulle-bootstrap|${libexec}|g" < "${i}" > "${bin}/$i" || exit 1
    chmod "${mode}" "${bin}/${i}" || exit 1
1b26e970
    printf "install: ${C_MAGENTA}${C_BOLD}%s${C_RESET}\n" "$bin/$i" >&2
5de2fbd6
 done
 
3c4acfba
 case `uname` in
    MINGW*)
58ddf0fd
       for i in mulle-mingw-*sh
3c4acfba
       do
          sed "s|/usr/local/libexec/mulle-bootstrap|${libexec}|g" < "${i}" > "${bin}/$i" || exit 1
          chmod "${mode}" "${bin}/${i}" || exit 1
          printf "install: ${C_MAGENTA}${C_BOLD}%s${C_RESET}\n" "$bin/$i" >&2
       done
58ddf0fd
 
       SH_PATH="`get_sh_windows_path | sed_mangle_escape_slashes`"
       INSTALL_PATH="${bin}" # `get_windows_path "${bin}" | sed_mangle_escape_slashes`"
 
       for i in mulle-mingw-*bat
       do
 
          sed -e "s|SH_PATH|${SH_PATH}|g" -e "s|INSTALL_PATH|${INSTALL_PATH}|g" < "${i}" > "${bin}/$i" || exit 1
          chmod "${mode}" "${bin}/${i}" || exit 1
          printf "install: ${C_MAGENTA}${C_BOLD}%s${C_RESET}\n" "$bin/$i" >&2
       done
3c4acfba
    ;;
 esac
 
ed336411
 for i in src/mulle*.sh
5de2fbd6
 do
    mkdir -p "${libexec}" 2> /dev/null
    install -v -m "${mode}" "$i" "${libexec}" || exit 1
 done
 
 if [ -d "test" ]
 then
    # use attractive colors :)
3882a58c
    printf "${C_GREEN}If you are new to mulle-bootstrap I would suggest checking out\n" >&2
    printf "the ${C_YELLOW}README.md${C_GREEN} in ${C_CYAN}./test${C_GREEN} and doing the examples.\n" >&2
5de2fbd6
 fi
 
 # for people who source us
 PATH="${libexec}:$PATH"