#!/usr/bin/env bash # # Author: Konstantin Gredeskoul, at Homebase, # © 2018 All rights reserved, MIT License. # # LibBash Bootstrap Installer. It can be run straight off github and it downloads the # larger bootstrap script, which then does the rest of the setup. # # This library will install `./bin/bootstrap` script relative to the local folder, # as well clone this repo into `../lib-bash`, and then create a symlink from `bin/lib-bash` to # `../lib-bash/lib`. This way you can leverage all helpers defined in this project in your code. # # Run me as: # # curl -fsSL https://raw.githubusercontent.com/pioneerworks/lib-bash/master/bin/install | bash # export True=1 export False=0 export ProjectRoot=$(pwd) export LibBash__RandomQueryString="q=${RANDOM}-$(date '+%s')" export LibBash__BootstrapExec="bootstrap" export LibBash__BinPath="bin" export LibBash__RelPath="${ProjectRoot}/../lib-bash" export LibBash__BootstrapLibPath="${LibBash__RelPath}/${LibBash__BinPath}/${LibBash__BootstrapExec}" export LibBash__BootstrapLocalPath="${LibBash__BinPath}/${LibBash__BootstrapExec}" export LibBash__Branch="master" export LibBash__GithubURL="https://raw.githubusercontent.com/pioneerworks/lib-bash" export LibBash__InstallerURL="${LibBash__GithubURL}/${LibBash__Branch}/bin/install?${LibBash__RandomQueryString}" export LibBash__BootstrapURL="${LibBash__GithubURL}/${LibBash__Branch}/bin/${LibBash__BootstrapExec}?${LibBash__RandomQueryString}" unset LibBash__Downloader [[ -z ${LibBash__Downloader} && -n $(which curl) ]] && export LibBash__Downloader="curl -fsSL --connect-timeout 5 --retry-delay 10 --retry-max-time 300 --retry 15" [[ -z ${LibBash__Downloader} && -n $(which wget) ]] && export LibBash__Downloader="wget -q --connect-timeout=5 --retry-connrefused --tries=15 -O -" [[ -z "${LibBash__Downloader}" ]] && { printf "Error: can't figure out how to download things. No curl or wget found." exit 1 } debug_log() { if [[ -n ${DEBUG} || ${USER} == "kig" ]] ; then [[ -n ${LIB_BASH_QUIET} ]] || printf " ${bldwht}${bakgrn} ✔ ${clr} ${txtgrn}${1}${clr}\n" fi } debug_log "using downloader ${bldylw}${LibBash__Downloader}" lib::bash::clean-old-bootstrap() { if [[ -d ${LibBash__BinPath} ]] ; then find ${LibBash__BinPath} -mmin +1440 -type f -name ${LibBash__BootstrapExec} -print -exec rm {} \; fi } lib::bash::bootstrap() { lib::bash::clean-old-bootstrap if [[ ! -s "${LibBash__BootstrapLocalPath}" ]]; then mkdir -p $(dirname ${LibBash__BootstrapLocalPath}) > /dev/null if [[ -s ${LibBash__BootstrapLibPath} ]]; then debug_log "${LibBash__BootstrapLibPath} exists, copying to ${bldylw}${LibBash__BootstrapLocalPath}" cp -p ${LibBash__BootstrapLibPath} ${LibBash__BootstrapLocalPath} > /dev/null else debug_log "grabbing bootstrap script from:\n → ${italic}${undblu}${LibBash__BootstrapURL}" ${LibBash__Downloader} ${LibBash__BootstrapURL} > ${LibBash__BootstrapLocalPath} fi touch ${LibBash__BootstrapLocalPath} chmod 755 ${LibBash__BootstrapLocalPath} > /dev/null fi if [[ -s "${LibBash__BootstrapLocalPath}" ]]; then debug_log "running script ${bldylw}${LibBash__BootstrapLocalPath}" if [[ -s ${LibBash__BootstrapLocalPath} ]]; then source ${LibBash__BootstrapLocalPath} # only run the function if it's defined type lib::bash::bootstrap::main 2>/dev/null 1>/dev/null && lib::bash::bootstrap::main fi fi } lib::bash::bootstrap