| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | set +o posix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | quiet_safe_cd() { | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   cd "$1" &>/dev/null || { | 
					
						
							|  |  |  |     echo "Error: failed to cd to $1" >&2 | 
					
						
							|  |  |  |     exit 1 | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | absdir() { | 
					
						
							|  |  |  |   quiet_safe_cd "${1%/*}/" && pwd -P | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | dirbasepath() { | 
					
						
							|  |  |  |   local dir="$1" | 
					
						
							|  |  |  |   local base="${2##*/}" | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   echo "${dir}/${base}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | realpath() { | 
					
						
							|  |  |  |   local path="$1" | 
					
						
							|  |  |  |   local dir | 
					
						
							|  |  |  |   local dest | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   dir="$(absdir "${path}")" | 
					
						
							|  |  |  |   path="$(dirbasepath "${dir}" "${path}")" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   while [[ -L "${path}" ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   do | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |     dest="$(readlink "${path}")" | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |     if [[ "${dest}" == "/"* ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |     then | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |       path="${dest}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |       path="${dir}/${dest}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |     dir="$(absdir "${path}")" | 
					
						
							|  |  |  |     path="$(dirbasepath "${dir}" "${path}")" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   echo "${path}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | executable() { | 
					
						
							|  |  |  |   local file="$1" | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   [[ -f "${file}" && -x "${file}" ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | lowercase() { | 
					
						
							|  |  |  |   echo "$1" | tr "[:upper:]" "[:lower:]" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | safe_exec() { | 
					
						
							|  |  |  |   local arg0="$1" | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |   if ! executable "${arg0}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   then | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   # prevent fork-bombs | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ "$(lowercase "${arg0}")" == "${SHIM_FILE}" || "$(realpath "${arg0}")" == "${SHIM_REAL}" ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   then | 
					
						
							|  |  |  |     return | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   if [[ "${HOMEBREW}" == "print-path" ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   then | 
					
						
							| 
									
										
										
										
											2021-03-30 01:15:05 +02:00
										 |  |  |     local dir | 
					
						
							|  |  |  |     dir="$(quiet_safe_cd "${arg0%/*}/" && pwd)" | 
					
						
							|  |  |  |     local path | 
					
						
							| 
									
										
										
										
											2021-04-18 16:44:53 +09:00
										 |  |  |     path="$(dirbasepath "${dir}" "${arg0}")" | 
					
						
							|  |  |  |     echo "${path}" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |     exit | 
					
						
							|  |  |  |   fi | 
					
						
							|  |  |  |   exec "$@" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | try_exec_non_system() { | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   local file="$1" | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  |   shift | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  |   local path | 
					
						
							|  |  |  |   while read -r path | 
					
						
							|  |  |  |   do | 
					
						
							|  |  |  |     if [[ "${path}" != "/usr/bin/${file}" ]] | 
					
						
							|  |  |  |     then | 
					
						
							|  |  |  |       safe_exec "${path}" "$@" | 
					
						
							|  |  |  |     fi | 
					
						
							|  |  |  |   done < <(type -aP "${file}") | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SHIM_FILE="${0##*/}" | 
					
						
							|  |  |  | SHIM_REAL="$(realpath "$0")" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 20:32:20 +08:00
										 |  |  | if [[ "$1" == "--homebrew="* ]] | 
					
						
							| 
									
										
										
										
											2021-03-02 17:06:05 +00:00
										 |  |  | then | 
					
						
							|  |  |  |   HOMEBREW="${1:11}" | 
					
						
							|  |  |  |   shift | 
					
						
							|  |  |  | fi |