From 6178a7e16b8932a86995d958368c577347ecc3d7 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sat, 10 Aug 2019 17:56:54 -0500 Subject: [PATCH 01/37] First pass at a rewrite. --- rewrite.txt | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 rewrite.txt diff --git a/rewrite.txt b/rewrite.txt new file mode 100644 index 0000000..80e0f0a --- /dev/null +++ b/rewrite.txt @@ -0,0 +1,188 @@ +#!/bin/bash - +#=============================================================================== +# +# FILE: default.txt.sh +# +# USAGE: ./default.txt.sh +# +# DESCRIPTION: +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Micheal Quinn, micheal.quinn85@gmail.com +# COMPANY: +# ORGANIZATION: +# CREATED: 08/10/2019 16:41 +# REVISION: 0.0.0 +#=============================================================================== +set -o nounset # Treat unset variables as an error + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_os +# DESCRIPTION: Attempts to determin host os using uname +# PARAMETERS: none +# RETURNS: 0 = OS Detected. Also prints detected os to stdout +# 1 = Unkown OS +# 2 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_os() { + local uname_cmd + local uname_out + + uname_cmd="$(command -v uname)" + + if [[ "${uname_cmd}" == "" ]]; then + return 2 + else + uname_out="$(uname)" + fi + + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_arch +# DESCRIPTION: Attempt to determin architecture of host +# PARAMETERS: none +# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout +# 1 = Unkown arch +# 2 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_arch() { + local uname_cmd + local uname_out + + uname_cmd="$(command -v uname)" + + if [[ "${uname_cmd}" == "" ]]; then + return 2 + else + uname_out="$(uname -m)" + fi + + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: download_file +# DESCRIPTION: Downloads a file into the specified directory. Attempts to +# use curl, then wget. If neither is found, fail. +# PARAMETERS: $1 = url of file to download +# $2 = location to download file into on host system +# RETURNS: If curl or wget found, returns the return code of curl or wget +# 2 = Could not find curl and wget +#------------------------------------------------------------------------------- +download_file() { + local url + local dir + local filename + local rcode + + url="${1}" + dir="${2}" + filename="${3}" + + if command -v curl >/dev/null 2>&1; then + curl -fsSL "${url}" -o "${dir}/${filename}" + rcode="${?}" + elif command -v wget >/dev/null 2>&1; then + wget --quiet "${url}" -O "${dir}/${filename}" + rcode="${?}" + else + echo "== Could not find curl or wget" + rcode="2" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: main +# DESCRIPTION: Put it all together in a logical way +# ...at least that is the hope... +# PARAMETERS: none +# RETURNS: 0 = All good +# 1 = Something done broke +#------------------------------------------------------------------------------- +main() { + local prefix + local tmpdir + local croc_arch + local croc_os + local croc_base_url + local croc_url + local croc_file + local croc_bin_name + local croc_version + local croc_dl_ext + + croc_bin_name="croc" + croc_version="6.1.1" + croc_dl_ext="tar.gz" + croc_base_url="https://github.com/schollz/croc/releases/download" + prefix="/usr/local/bin" + tmpdir="$(mktemp -d --suffix="_${croc_bin_name}")" + + croc_arch="$(determine_arch)" + croc_arch_rcode="${?}" + if [[ "${croc_arch_rcode}" == "0" ]]; then + echo "== Architecture detected as ${croc_arch}" + elif [[ "${croc_arch_rcode}" == "1" ]]; then + echo "== Architecture not detected" + exit 1 + else + echo "== 'uname' not found in path. Is it installed?" + fi + + croc_os="$(determine_os)" + croc_os_rcode="${?}" + if [[ "${croc_os_rcode}" == "0" ]]; then + echo "== OS detected as ${croc_os}" + elif [[ "${croc_os_rcode}" == "1" ]]; then + echo "== OS not detected" + exit 1 + else + echo "== 'uname' not found in path. Is it installed?" + fi + + case "${croc_arch}" in + "x86_64" ) croc_arch="64bit";; + "amd64" ) croc_arch="64bit";; + "aarch64" ) croc_arch="ARM64";; + "i686" ) croc_arch="32bit";; + * ) croc_arch="unknown";; + esac + + + croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" + croc_url="${croc_base_url}/v${croc_version}/${croc_file}" + + download_file "${croc_url}" "${tmpdir}" "${croc_file}" + download_file_rcode="${?}" + if [[ "${download_file_rcode}" == "0" ]]; then + echo "== Downloaded croc archive into ${tmpdir}" + elif [[ "${download_file_rcode}" == "1" ]]; then + echo "== Failed to download croc archive" + exit 1 + elif [[ "${download_file_rcode}" == "2" ]]; then + echo "== Failed to locate curl or wget" + exit 1 + else + echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" + fi +} + +main From 7a684e7266a74040fef7ac2b35603d20fe4a0737 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sat, 10 Aug 2019 17:57:37 -0500 Subject: [PATCH 02/37] moving the rewrite into the proper location --- rewrite.txt => src/install/rewrite.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename rewrite.txt => src/install/rewrite.txt (100%) diff --git a/rewrite.txt b/src/install/rewrite.txt similarity index 100% rename from rewrite.txt rename to src/install/rewrite.txt From c82dda45d946000ce676196b8a030424a7dec18a Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sat, 10 Aug 2019 18:26:06 -0500 Subject: [PATCH 03/37] Adding in extract file logic. --- src/install/rewrite.txt | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 80e0f0a..ce9ffd8 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -109,6 +109,48 @@ download_file() { return "${rcode}" } +#--- FUNCTION ---------------------------------------------------------------- +# NAME: extract_file +# DESCRIPTION: Extracts a file into a location. Attempts to determine which +# tool to use by checking file extention. +# PARAMETERS: $1 = file to extract +# $2 = location to extract file into +# $3 = extention +# RETURNS: Return code of the tool used to extract the file +# 2 = Failed to determine which tool to use +# 3 = Failed to find tool in path +#------------------------------------------------------------------------------- +extract_file() { + local file + local dir + local ext + local rcode + + file="${1}" + dir="${2}" + ext="${3}" + + case "${ext}" in + "zip" ) if command -v unzip >/dev/null 2>&1; then + unzip "${file}" -d "${dir}" + rcode="${?}" + else + rcode="3" + fi + ;; + "tar.gz" ) if command -v tar >/dev/null 2>&1; then + tar -xf "${file}" -C "${dir}" + rcode="${?}" + else + rcode="3" + fi + ;; + * ) rcode="2";; + esac + + return "${rcode}" +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: main # DESCRIPTION: Put it all together in a logical way @@ -183,6 +225,24 @@ main() { else echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" fi + + extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" + extract_file_rcode="${?}" + if [[ "${extract_file_rcode}" == "0" ]]; then + echo "== Extracted ${croc_file} to ${tmpdir}/" + elif [[ "${extract_file_rcode}" == "1" ]]; then + echo "== Failed to extract ${croc_file}" + exit 1 + elif [[ "${extract_file_rcode}" == "2" ]]; then + echo "== Failed to determine which extraction tool to use" + exit 1 + elif [[ "${extract_file_rcode}" == "3" ]]; then + echo "== Failed to find extraction tool in path" + exit 1 + else + echo "== Unknown error returned from extraction attempt" + exit 1 + fi } main From eaf448ddac7d52feadc837c951949d72ed538a80 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sat, 10 Aug 2019 21:08:04 -0500 Subject: [PATCH 04/37] Adding install function and logic --- src/install/rewrite.txt | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index ce9ffd8..343447c 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -151,6 +151,34 @@ extract_file() { return "${rcode}" } + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file +# DESCRIPTION: Installs a file into a location using 'install' +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 2 = Could not find install command +#------------------------------------------------------------------------------- +install_file() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + sudo install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + rcode="2" + fi + + return "${rcode}" +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: main # DESCRIPTION: Put it all together in a logical way @@ -243,6 +271,21 @@ main() { echo "== Unknown error returned from extraction attempt" exit 1 fi + + install_file "${tmpdir}/${croc_bin_name}" "${prefix}/" + install_file_rcode="${?}" + if [[ "${install_file_rcode}" == "0" ]]; then + echo "== Installed ${croc_bin_name} to ${prefix}/" + elif [[ "${install_file_rcode}" == "1" ]]; then + echo "== Failed to install ${croc_bin_name}" + exit 1 + elif [[ "${install_file_rcode}" == "2" ]]; then + echo "== Failed to locate 'install' command" + exit 1 + else + echo "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" + fi + } main From 9530952eb699d2c95cc1c86403c658a8971e5e21 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sat, 10 Aug 2019 21:45:53 -0500 Subject: [PATCH 05/37] Adding install function and cleanup function --- src/install/rewrite.txt | 75 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 343447c..7512e05 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -109,6 +109,31 @@ download_file() { return "${rcode}" } +#--- FUNCTION ---------------------------------------------------------------- +# NAME: checksum_check +# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure +# integrity. +# PARAMETERS: $1 = path to checksum file +# $2 = location of file to check +# $3 = working directory +# RETURNS: 0 = checkusm verified +# 1 = checksum verification failed +# 2 = failed to determine tool to use to check checksum +# 3 = failed to change into or go back from working dir +#------------------------------------------------------------------------------- +checksum_check() { + local checksum_file + local file + + checksum_file="${1}" + file="${2}" + dir="${3}" + + cd "${dir}" || return 3 + sha256sum -c "${checksum_file}" --ignore-missing >/dev/null 2>&1 + cd - >/dev/null 2>&1 || return 3 +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: extract_file # DESCRIPTION: Extracts a file into a location. Attempts to determine which @@ -179,6 +204,21 @@ install_file() { return "${rcode}" } + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: cleanup +# DESCRIPTION: Cleanup our temp files +# PARAMETERS: $1 = Path to temp dir to remove +# RETURNS: nothing +#------------------------------------------------------------------------------- +cleanup() { + local dir + + dir="$1" + + rm -rf "${dir}" +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: main # DESCRIPTION: Put it all together in a logical way @@ -195,6 +235,7 @@ main() { local croc_base_url local croc_url local croc_file + local croc_checksum_file local croc_bin_name local croc_version local croc_dl_ext @@ -238,7 +279,9 @@ main() { croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" + croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" croc_url="${croc_base_url}/v${croc_version}/${croc_file}" + croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" download_file "${croc_url}" "${tmpdir}" "${croc_file}" download_file_rcode="${?}" @@ -253,6 +296,35 @@ main() { else echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" fi + download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" + download_checksum_file_rcode="${?}" + if [[ "${download_checksum_file_rcode}" == "0" ]]; then + echo "== Downloaded croc checksums file into ${tmpdir}" + elif [[ "${download_checksum_file_rcode}" == "1" ]]; then + echo "== Failed to download croc checksums" + exit 1 + elif [[ "${download_checksum_file_rcode}" == "2" ]]; then + echo "== Failed to locate curl or wget" + exit 1 + else + echo "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" + fi + + checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" + checksum_check_rcode="${?}" + if [[ "${checksum_check_rcode}" == "0" ]]; then + echo "== Checksum of ${tmpdir}/${croc_file} verified" + elif [[ "${checksum_check_rcode}" == "1" ]]; then + echo "== Failed to verify checksum of ${tmpdir}/${croc_file}" + exit 1 + elif [[ "${checksum_check_rcode}" == "2" ]]; then + echo "== Failed to find tool to verify sha256 sums" + exit 1 + else + echo "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" + exit 1 + fi + extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" extract_file_rcode="${?}" @@ -286,6 +358,9 @@ main() { echo "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" fi + echo "== Cleaning up ${tmpdir}" + cleanup "${tmpdir}" + } main From b0ac5024a481168170e4262847424a863ffc4933 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 12:31:19 -0500 Subject: [PATCH 06/37] Adding a function to handle creation of temp dir. Polished the checksum check function to handle more cases/OSes --- src/install/rewrite.txt | 81 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 9 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 7512e05..e97303f 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -19,6 +19,31 @@ #=============================================================================== set -o nounset # Treat unset variables as an error +#--- FUNCTION ---------------------------------------------------------------- +# NAME: make_tempdir +# DESCRIPTION: Makes a temp dir using mktemp if available +# PARAMETERS: $1 = Directory template +# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout +# 1 = Failed to create temp dir +#------------------------------------------------------------------------------- +make_tempdir() { + local template + local tempdir + + template="${1}" + + if command -v mktemp >/dev/null 2>&1; then + tempdir="$(mktemp -d -t "${template}")" + tempdir_rcode="${?}" + if [[ "${tempdir_rcode}" == "0" ]]; then + echo "${tempdir}" + return 0 + else + return 1 + fi + fi +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: determine_os # DESCRIPTION: Attempts to determin host os using uname @@ -112,26 +137,53 @@ download_file() { #--- FUNCTION ---------------------------------------------------------------- # NAME: checksum_check # DESCRIPTION: Attempt to verify checksum of downloaded file to ensure -# integrity. +# integrity. Tries multiple tools before faling. # PARAMETERS: $1 = path to checksum file # $2 = location of file to check # $3 = working directory # RETURNS: 0 = checkusm verified # 1 = checksum verification failed -# 2 = failed to determine tool to use to check checksum -# 3 = failed to change into or go back from working dir +# 20 = failed to determine tool to use to check checksum +# 30 = failed to change into or go back from working dir #------------------------------------------------------------------------------- checksum_check() { local checksum_file local file + local shasum_1 + local shasum_2 checksum_file="${1}" file="${2}" dir="${3}" cd "${dir}" || return 3 - sha256sum -c "${checksum_file}" --ignore-missing >/dev/null 2>&1 - cd - >/dev/null 2>&1 || return 3 + if command -v sha256sum >/dev/null 2>&1; then + sha256sum -c "${checksum_file}" --ignore-missing >/dev/null 2>&1 + rcode="${?}" + elif command -v shasum >/dev/null 2>&1; then + ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum -s -a 256 -c "filtered_checksum.txt" + rcode="${?}" + elif command -v sha256 >/dev/null 2>&1; then + ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + ## Also sha256 -c option seems to fail, so fall back to an if statement + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_1="$(sha256 -q "${file}")" + shasum_2="$(awk '{print $1}' filtered_checksum.txt)" + if [[ "${shasum_1}" == "${shasum_2}" ]]; then + rcode="0" + else + rcode="1" + fi + else + return 20 + fi + cd - >/dev/null 2>&1 || return 30 + + return "${rcode}" } #--- FUNCTION ---------------------------------------------------------------- @@ -245,7 +297,15 @@ main() { croc_dl_ext="tar.gz" croc_base_url="https://github.com/schollz/croc/releases/download" prefix="/usr/local/bin" - tmpdir="$(mktemp -d --suffix="_${croc_bin_name}")" + + tmpdir="$(make_tempdir "${croc_bin_name}")" + tmpdir_rcode="${?}" + if [[ "${tmpdir_rcode}" == "0" ]]; then + echo "== Created temp dir at ${tmpdir}" + else + echo "== Failed to create temp dir at ${tmpdir}" + exit 1 + fi croc_arch="$(determine_arch)" croc_arch_rcode="${?}" @@ -277,7 +337,6 @@ main() { * ) croc_arch="unknown";; esac - croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" croc_url="${croc_base_url}/v${croc_version}/${croc_file}" @@ -295,6 +354,7 @@ main() { exit 1 else echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" + exit 1 fi download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" download_checksum_file_rcode="${?}" @@ -308,6 +368,7 @@ main() { exit 1 else echo "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" + exit 1 fi checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" @@ -317,15 +378,17 @@ main() { elif [[ "${checksum_check_rcode}" == "1" ]]; then echo "== Failed to verify checksum of ${tmpdir}/${croc_file}" exit 1 - elif [[ "${checksum_check_rcode}" == "2" ]]; then + elif [[ "${checksum_check_rcode}" == "20" ]]; then echo "== Failed to find tool to verify sha256 sums" exit 1 + elif [[ "${checksum_check_rcode}" == "30" ]]; then + echo "== Failed to change into working directory ${tmpdir}" + exit 1 else echo "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" exit 1 fi - extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" extract_file_rcode="${?}" if [[ "${extract_file_rcode}" == "0" ]]; then From e7d6d096a8d970cc76951f303839158f33cb8a24 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 13:01:00 -0500 Subject: [PATCH 07/37] Adding in root check to install commands with fallback to sudo. Splitting install logic out based on host OS --- src/install/rewrite.txt | 62 +++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index e97303f..97eef36 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -30,7 +30,7 @@ make_tempdir() { local template local tempdir - template="${1}" + template="${1}.XXXXXX" if command -v mktemp >/dev/null 2>&1; then tempdir="$(mktemp -d -t "${template}")" @@ -228,17 +228,17 @@ extract_file() { return "${rcode}" } - #--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file -# DESCRIPTION: Installs a file into a location using 'install' +# NAME: install_file_freebsd +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. # PARAMETERS: $1 = file to install # $2 = location to install file into # RETURNS: 0 = File Installed # 1 = File not installed # 2 = Could not find install command #------------------------------------------------------------------------------- -install_file() { +install_file_freebsd() { local file local prefix local rcode @@ -247,8 +247,47 @@ install_file() { prefix="${2}" if command -v install >/dev/null 2>&1; then - sudo install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" + if [[ "${EUID}" == "0" ]]; then + install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + fi + else + rcode="2" + fi + + return "${rcode}" +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_linux +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 2 = Could not find install command +#------------------------------------------------------------------------------- +install_file_linux() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + sudo install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + fi else rcode="2" fi @@ -407,8 +446,12 @@ main() { exit 1 fi - install_file "${tmpdir}/${croc_bin_name}" "${prefix}/" - install_file_rcode="${?}" + case "${croc_os}" in + "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + esac if [[ "${install_file_rcode}" == "0" ]]; then echo "== Installed ${croc_bin_name} to ${prefix}/" elif [[ "${install_file_rcode}" == "1" ]]; then @@ -419,6 +462,7 @@ main() { exit 1 else echo "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" + exit 1 fi echo "== Cleaning up ${tmpdir}" From 2c2920eb9c8092aa25967f94f299cbb040e9b9ea Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 15:33:42 -0500 Subject: [PATCH 08/37] Adding description information. Added print message function for fancy colors in the output. --- src/install/rewrite.txt | 127 ++++++++++++++++++++++++++-------------- 1 file changed, 84 insertions(+), 43 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 97eef36..d570cd2 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -3,22 +3,64 @@ # # FILE: default.txt.sh # -# USAGE: ./default.txt.sh +# USAGE: curl https://getcroc.schollz.com | bash +# OR +# wget -qO- https://getcroc.schollz.com | bash # -# DESCRIPTION: -# -# OPTIONS: --- -# REQUIREMENTS: --- -# BUGS: --- -# NOTES: --- +# DESCRIPTION: croc Installer Script. +# +# This script installs croc into a specified prefix. +# Default prefix = /usr/local/bin +# +# OPTIONS: -p, --prefix "${PREFIX}" +# Prefix to install croc into. Defaults to /usr/local/bin +# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run +# as root), install, mktemp, sha256sum/shasum/sha256 +# +# BUGS: ...hopefully not. Please report. +# +# NOTES: Homepage: https://schollz.com/software/croc +# Issues: https://github.com/schollz/croc/issues +# # AUTHOR: Micheal Quinn, micheal.quinn85@gmail.com -# COMPANY: -# ORGANIZATION: # CREATED: 08/10/2019 16:41 -# REVISION: 0.0.0 +# REVISION: 0.5.0 #=============================================================================== set -o nounset # Treat unset variables as an error + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_message +# DESCRIPTION: Prints a message all fancy like +# PARAMETERS: $1 = Message to print +# $2 = Severity. info, ok, error, warn +# RETURNS: Formatted Message to stdout +#------------------------------------------------------------------------------- +print_message() { + local message + local severity + local red + local green + local yellow + local nc + + message="${1}" + severity="${2}" + red='\e[0;31m' + green='\e[0;32m' + yellow='\e[1;33m' + nc='\e[0m' + + case "${severity}" in + "info" ) echo -e "${nc}${message}${nc}";; + "ok" ) echo -e "${green}${message}${nc}";; + "error" ) echo -e "${red}${message}${nc}";; + "warn" ) echo -e "${yellow}${message}${nc}";; + esac + + +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: make_tempdir # DESCRIPTION: Makes a temp dir using mktemp if available @@ -57,7 +99,6 @@ determine_os() { local uname_out uname_cmd="$(command -v uname)" - if [[ "${uname_cmd}" == "" ]]; then return 2 else @@ -127,7 +168,6 @@ download_file() { wget --quiet "${url}" -O "${dir}/${filename}" rcode="${?}" else - echo "== Could not find curl or wget" rcode="2" fi @@ -340,32 +380,32 @@ main() { tmpdir="$(make_tempdir "${croc_bin_name}")" tmpdir_rcode="${?}" if [[ "${tmpdir_rcode}" == "0" ]]; then - echo "== Created temp dir at ${tmpdir}" + print_message "== Created temp dir at ${tmpdir}" "info" else - echo "== Failed to create temp dir at ${tmpdir}" + print_message "== Failed to create temp dir at ${tmpdir}" "error" exit 1 fi croc_arch="$(determine_arch)" croc_arch_rcode="${?}" if [[ "${croc_arch_rcode}" == "0" ]]; then - echo "== Architecture detected as ${croc_arch}" + print_message "== Architecture detected as ${croc_arch}" "info" elif [[ "${croc_arch_rcode}" == "1" ]]; then - echo "== Architecture not detected" + print_message "== Architecture not detected" "error" exit 1 else - echo "== 'uname' not found in path. Is it installed?" + print_message "== 'uname' not found in path. Is it installed?" "error" fi croc_os="$(determine_os)" croc_os_rcode="${?}" if [[ "${croc_os_rcode}" == "0" ]]; then - echo "== OS detected as ${croc_os}" + print_message "== OS detected as ${croc_os}" "info" elif [[ "${croc_os_rcode}" == "1" ]]; then - echo "== OS not detected" + print_message "== OS not detected" "error" exit 1 else - echo "== 'uname' not found in path. Is it installed?" + print_message "== 'uname' not found in path. Is it installed?" "error" fi case "${croc_arch}" in @@ -384,65 +424,65 @@ main() { download_file "${croc_url}" "${tmpdir}" "${croc_file}" download_file_rcode="${?}" if [[ "${download_file_rcode}" == "0" ]]; then - echo "== Downloaded croc archive into ${tmpdir}" + print_message "== Downloaded croc archive into ${tmpdir}" "info" elif [[ "${download_file_rcode}" == "1" ]]; then - echo "== Failed to download croc archive" + print_message "== Failed to download croc archive" "error" exit 1 elif [[ "${download_file_rcode}" == "2" ]]; then - echo "== Failed to locate curl or wget" + print_message "== Failed to locate curl or wget" "error" exit 1 else - echo "== Return code of download tool returned an unexpected value of ${download_file_rcode}" + print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" exit 1 fi download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" download_checksum_file_rcode="${?}" if [[ "${download_checksum_file_rcode}" == "0" ]]; then - echo "== Downloaded croc checksums file into ${tmpdir}" + print_message "== Downloaded croc checksums file into ${tmpdir}" "info" elif [[ "${download_checksum_file_rcode}" == "1" ]]; then - echo "== Failed to download croc checksums" + print_message "== Failed to download croc checksums" "error" exit 1 elif [[ "${download_checksum_file_rcode}" == "2" ]]; then - echo "== Failed to locate curl or wget" + print_message "== Failed to locate curl or wget" "error" exit 1 else - echo "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" + print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" exit 1 fi checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" checksum_check_rcode="${?}" if [[ "${checksum_check_rcode}" == "0" ]]; then - echo "== Checksum of ${tmpdir}/${croc_file} verified" + print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" elif [[ "${checksum_check_rcode}" == "1" ]]; then - echo "== Failed to verify checksum of ${tmpdir}/${croc_file}" + print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" exit 1 elif [[ "${checksum_check_rcode}" == "20" ]]; then - echo "== Failed to find tool to verify sha256 sums" + print_message "== Failed to find tool to verify sha256 sums" "error" exit 1 elif [[ "${checksum_check_rcode}" == "30" ]]; then - echo "== Failed to change into working directory ${tmpdir}" + print_message "== Failed to change into working directory ${tmpdir}" "error" exit 1 else - echo "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" + print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" exit 1 fi extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" extract_file_rcode="${?}" if [[ "${extract_file_rcode}" == "0" ]]; then - echo "== Extracted ${croc_file} to ${tmpdir}/" + print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" elif [[ "${extract_file_rcode}" == "1" ]]; then - echo "== Failed to extract ${croc_file}" + print_message "== Failed to extract ${croc_file}" "error" exit 1 elif [[ "${extract_file_rcode}" == "2" ]]; then - echo "== Failed to determine which extraction tool to use" + print_message "== Failed to determine which extraction tool to use" "error" exit 1 elif [[ "${extract_file_rcode}" == "3" ]]; then - echo "== Failed to find extraction tool in path" + print_message "== Failed to find extraction tool in path" "error" exit 1 else - echo "== Unknown error returned from extraction attempt" + print_message "== Unknown error returned from extraction attempt" "error" exit 1 fi @@ -453,20 +493,21 @@ main() { install_file_rcode="${?}";; esac if [[ "${install_file_rcode}" == "0" ]]; then - echo "== Installed ${croc_bin_name} to ${prefix}/" + print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" elif [[ "${install_file_rcode}" == "1" ]]; then - echo "== Failed to install ${croc_bin_name}" + print_message "== Failed to install ${croc_bin_name}" "error" exit 1 elif [[ "${install_file_rcode}" == "2" ]]; then - echo "== Failed to locate 'install' command" + print_message "== Failed to locate 'install' command" "error" exit 1 else - echo "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" + print_message "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" "error" exit 1 fi - echo "== Cleaning up ${tmpdir}" + print_message "== Cleaning up ${tmpdir}" "info" cleanup "${tmpdir}" + print_message "== Installation complete" "ok" } From bf15c4c424d08d7e0f417b7cbb872bea2d09ff10 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 16:00:56 -0500 Subject: [PATCH 09/37] Adding help message formatting. Adding in sudo check. Caught and fixed some global variables that should be local. --- src/install/rewrite.txt | 91 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 82 insertions(+), 9 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index d570cd2..5237678 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -28,6 +28,35 @@ #=============================================================================== set -o nounset # Treat unset variables as an error +#------------------------------------------------------------------------------- +# DEFAULTS +#------------------------------------------------------------------------------- +PREFIX="/usr/local/bin" + +#------------------------------------------------------------------------------- +# FUNCTIONS +#------------------------------------------------------------------------------- + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_help +# DESCRIPTION: Prints out a help message +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_help() { + help_header="croc Installer Script" + help_message="Usage: + -p, --prefix + Prefix to install croc into. Directory must already exist. + Default = /usr/local/bin + + -h, --help + Prints this helpfull message and exit." + + echo "${help_header}" + echo "" + echo "${help_message}" +} #--- FUNCTION ---------------------------------------------------------------- # NAME: print_message @@ -189,6 +218,7 @@ download_file() { checksum_check() { local checksum_file local file + local rcode local shasum_1 local shasum_2 @@ -277,6 +307,7 @@ extract_file() { # RETURNS: 0 = File Installed # 1 = File not installed # 2 = Could not find install command +# 3 = Could not find sudo command #------------------------------------------------------------------------------- install_file_freebsd() { local file @@ -291,8 +322,12 @@ install_file_freebsd() { install -C -b -B '_old' -m 755 "${file}" "${prefix}" rcode="${?}" else - sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="3" + fi fi else rcode="2" @@ -325,8 +360,12 @@ install_file_linux() { install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" rcode="${?}" else - sudo install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="3" + fi fi else rcode="2" @@ -354,15 +393,18 @@ cleanup() { # NAME: main # DESCRIPTION: Put it all together in a logical way # ...at least that is the hope... -# PARAMETERS: none +# PARAMETERS: 1 = prefix # RETURNS: 0 = All good # 1 = Something done broke #------------------------------------------------------------------------------- main() { local prefix local tmpdir + local tmpdir_rcode local croc_arch + local croc_arch_rcode local croc_os + local croc_os_rcode local croc_base_url local croc_url local croc_file @@ -370,12 +412,17 @@ main() { local croc_bin_name local croc_version local croc_dl_ext + local download_file_rcode + local download_checksum_file_rcode + local checksum_check_rcode + local extract_file_rcode + local install_file_rcode croc_bin_name="croc" croc_version="6.1.1" croc_dl_ext="tar.gz" croc_base_url="https://github.com/schollz/croc/releases/download" - prefix="/usr/local/bin" + prefix="${1}" tmpdir="$(make_tempdir "${croc_bin_name}")" tmpdir_rcode="${?}" @@ -500,15 +547,41 @@ main() { elif [[ "${install_file_rcode}" == "2" ]]; then print_message "== Failed to locate 'install' command" "error" exit 1 + elif [[ "${install_file_rcode}" == "3" ]]; then + print_message "== Failed to locate 'sudo' command" "error" + exit 1 else - print_message "== Return code of 'install' returned an unexpected value of ${install_file_rcode}" "error" + print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" exit 1 fi print_message "== Cleaning up ${tmpdir}" "info" cleanup "${tmpdir}" print_message "== Installation complete" "ok" - } -main +#------------------------------------------------------------------------------- +# ARGUMENT PARSING +#------------------------------------------------------------------------------- +OPTS=$(getopt -o hp: --long help,prefix: -n 'parse-options' -- "${@}") +getopt_rcode="${?}" +if [ ${getopt_rcode} != 0 ] ; then + echo "Failed parsing options." >&2 + exit 1 +fi + +eval set -- "${OPTS}" + +while true; do + case "${1}" in + -p | --prefix ) PREFIX="${2}"; shift 2;; + -h | --help ) print_help; exit 0;; + -- ) shift; break ;; + * ) break ;; + esac +done + +#------------------------------------------------------------------------------- +# CALL MAIN +#------------------------------------------------------------------------------- +main "${PREFIX}" From d39266a36d299c88e31bfcbccee224d37868e2db Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 16:35:51 -0500 Subject: [PATCH 10/37] wrangling another batch of wild global vars that should be local vars --- src/install/rewrite.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 5237678..2b257fb 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -44,6 +44,9 @@ PREFIX="/usr/local/bin" # RETURNS: 0 #------------------------------------------------------------------------------- print_help() { + local help_header + local help_message + help_header="croc Installer Script" help_message="Usage: -p, --prefix @@ -100,6 +103,7 @@ print_message() { make_tempdir() { local template local tempdir + local tempdir_rcode template="${1}.XXXXXX" @@ -218,6 +222,7 @@ download_file() { checksum_check() { local checksum_file local file + local dir local rcode local shasum_1 local shasum_2 From 9c6f01ca91ec0cbc4da5cf419e5bd1ccc544c588 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Sun, 11 Aug 2019 17:07:43 -0500 Subject: [PATCH 11/37] swapping getopt (which apparently is not good to use) for getopts in an attempt to make it more cross-platform without having to break out the logic based on OS...again... --- src/install/rewrite.txt | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 2b257fb..4990d89 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -49,11 +49,11 @@ print_help() { help_header="croc Installer Script" help_message="Usage: - -p, --prefix + -p PREFIX Prefix to install croc into. Directory must already exist. Default = /usr/local/bin - -h, --help + -h Prints this helpfull message and exit." echo "${help_header}" @@ -568,21 +568,16 @@ main() { #------------------------------------------------------------------------------- # ARGUMENT PARSING #------------------------------------------------------------------------------- -OPTS=$(getopt -o hp: --long help,prefix: -n 'parse-options' -- "${@}") -getopt_rcode="${?}" -if [ ${getopt_rcode} != 0 ] ; then - echo "Failed parsing options." >&2 - exit 1 -fi - -eval set -- "${OPTS}" - -while true; do - case "${1}" in - -p | --prefix ) PREFIX="${2}"; shift 2;; - -h | --help ) print_help; exit 0;; - -- ) shift; break ;; - * ) break ;; +OPTS="hp:" +while getopts "${OPTS}" optchar; do + case "${optchar}" in + 'h' ) print_help + exit 0 + ;; + 'p' ) PREFIX="${OPTARG}" + ;; + '?' ) print_message "Unknown option ${OPTARG}" "warn" + ;; esac done From 34743118de6ed2c97b297c9bb15eb9f4da20fb88 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Mon, 12 Aug 2019 09:16:24 -0500 Subject: [PATCH 12/37] script header cleanup --- src/install/rewrite.txt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 4990d89..61fa4d3 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -1,7 +1,7 @@ #!/bin/bash - #=============================================================================== # -# FILE: default.txt.sh +# FILE: default.txt # # USAGE: curl https://getcroc.schollz.com | bash # OR @@ -22,7 +22,6 @@ # NOTES: Homepage: https://schollz.com/software/croc # Issues: https://github.com/schollz/croc/issues # -# AUTHOR: Micheal Quinn, micheal.quinn85@gmail.com # CREATED: 08/10/2019 16:41 # REVISION: 0.5.0 #=============================================================================== @@ -576,7 +575,7 @@ while getopts "${OPTS}" optchar; do ;; 'p' ) PREFIX="${OPTARG}" ;; - '?' ) print_message "Unknown option ${OPTARG}" "warn" + /? ) print_message "Unknown option ${OPTARG}" "warn" ;; esac done From b8d1d11b68d5f07af1a4d2895317affab995177f Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Mon, 12 Aug 2019 10:53:09 -0500 Subject: [PATCH 13/37] Changing PREFIX to INSTALL_PREFIX to avoid clashing with possibly set environment variables. Adding logic to handle if we are on termux on android (adapted from previous croc install script). Adding an info message for what prefix was set to. Adding in armv71 support (need to add more or make this a generic case to catch more arm devices). --- src/install/rewrite.txt | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 61fa4d3..76807f2 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -12,7 +12,7 @@ # This script installs croc into a specified prefix. # Default prefix = /usr/local/bin # -# OPTIONS: -p, --prefix "${PREFIX}" +# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" # Prefix to install croc into. Defaults to /usr/local/bin # REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run # as root), install, mktemp, sha256sum/shasum/sha256 @@ -30,7 +30,15 @@ set -o nounset # Treat unset variables as an error #------------------------------------------------------------------------------- # DEFAULTS #------------------------------------------------------------------------------- -PREFIX="/usr/local/bin" +PREFIX="${PREFIX:-}" +ANDROID_ROOT="${ANDROID_ROOT:-}" + +# Termux on Android has ${PREFIX} set which already ends with '/usr' +if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then + INSTALL_PREFIX="${PREFIX}/bin" +else + INSTALL_PREFIX="/usr/local/bin" +fi #------------------------------------------------------------------------------- # FUNCTIONS @@ -48,9 +56,9 @@ print_help() { help_header="croc Installer Script" help_message="Usage: - -p PREFIX + -p INSTALL_PREFIX Prefix to install croc into. Directory must already exist. - Default = /usr/local/bin + Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) -h Prints this helpfull message and exit." @@ -344,7 +352,7 @@ install_file_freebsd() { #--- FUNCTION ---------------------------------------------------------------- # NAME: install_file_linux # DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. +# 0, then attempt to use sudo (unless on android). # PARAMETERS: $1 = file to install # $2 = location to install file into # RETURNS: 0 = File Installed @@ -367,6 +375,9 @@ install_file_linux() { if command -v sudo >/dev/null 2>&1; then sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" rcode="${?}" + elif [[ "${ANDROID_ROOT}" != "" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" else rcode="3" fi @@ -427,6 +438,8 @@ main() { croc_dl_ext="tar.gz" croc_base_url="https://github.com/schollz/croc/releases/download" prefix="${1}" + + print_message "== Install prefix set to ${prefix}" "info" tmpdir="$(make_tempdir "${croc_bin_name}")" tmpdir_rcode="${?}" @@ -459,10 +472,15 @@ main() { print_message "== 'uname' not found in path. Is it installed?" "error" fi + case "${croc_os}" in + "Darwin" ) croc_os="macOS";; + esac + case "${croc_arch}" in "x86_64" ) croc_arch="64bit";; "amd64" ) croc_arch="64bit";; "aarch64" ) croc_arch="ARM64";; + "armv7l" ) croc_arch="ARM";; "i686" ) croc_arch="32bit";; * ) croc_arch="unknown";; esac @@ -573,7 +591,7 @@ while getopts "${OPTS}" optchar; do 'h' ) print_help exit 0 ;; - 'p' ) PREFIX="${OPTARG}" + 'p' ) INSTALL_PREFIX="${OPTARG}" ;; /? ) print_message "Unknown option ${OPTARG}" "warn" ;; @@ -583,4 +601,4 @@ done #------------------------------------------------------------------------------- # CALL MAIN #------------------------------------------------------------------------------- -main "${PREFIX}" +main "${INSTALL_PREFIX}" From e2e48d9e840daf66c5926830e87f9f9a54afa92b Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Mon, 12 Aug 2019 11:04:50 -0500 Subject: [PATCH 14/37] Adding in fix for macOS (Darwin) not having an install case. Uses the same logic from install_file_freebsd --- src/install/rewrite.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 76807f2..03b5886 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -560,6 +560,8 @@ main() { install_file_rcode="${?}";; "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; install_file_rcode="${?}";; + "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; esac if [[ "${install_file_rcode}" == "0" ]]; then print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" From 588dfe6c0090949220b348953ac4bfd76b08ab1b Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Mon, 12 Aug 2019 14:40:46 -0500 Subject: [PATCH 15/37] Removing cleanup logic. rm -rf in a script terrifies me. The script attempts to place it into the temp dir, so we will let the OS clean it up. --- src/install/rewrite.txt | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 03b5886..34167fc 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -389,21 +389,6 @@ install_file_linux() { return "${rcode}" } - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: cleanup -# DESCRIPTION: Cleanup our temp files -# PARAMETERS: $1 = Path to temp dir to remove -# RETURNS: nothing -#------------------------------------------------------------------------------- -cleanup() { - local dir - - dir="$1" - - rm -rf "${dir}" -} - #--- FUNCTION ---------------------------------------------------------------- # NAME: main # DESCRIPTION: Put it all together in a logical way @@ -579,9 +564,9 @@ main() { exit 1 fi - print_message "== Cleaning up ${tmpdir}" "info" - cleanup "${tmpdir}" print_message "== Installation complete" "ok" + + exit 0 } #------------------------------------------------------------------------------- From d429e09e95e735f7c402b59fc0bf15ff474e44ba Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 12 Aug 2019 21:15:06 -0500 Subject: [PATCH 16/37] quick todo file --- src/install/rewrite_todo.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/install/rewrite_todo.md diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md new file mode 100644 index 0000000..7c7a161 --- /dev/null +++ b/src/install/rewrite_todo.md @@ -0,0 +1,15 @@ +# To Do +- [ ] Test Multiple ARM Devices +- [ ] Test Multiple ARM64 Devices +- [ ] Test Windows (MobaXterm) + - [ ] Needs an override for croc_os and croc_dl_ext + - [ ] Needs new install_file_windws function +- [ ] Test Multiple MacOS Devices +- [ ] Test Multiple Linux Devices + - [ ] CentOS 6+ + - [ ] Archlinux + - [ ] Ubuntu Current LTS+ +- [ ] Test Multiple BSD Devices + - [ ] FreeBSD + - [ ] OpenBSD + - [ ] DragonFlyBSD From 081e267a19cbda52640003fcf6d61b878eeebb4f Mon Sep 17 00:00:00 2001 From: Quinn Date: Mon, 12 Aug 2019 21:15:38 -0500 Subject: [PATCH 17/37] check off bsds --- src/install/rewrite_todo.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index 7c7a161..504cf45 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -9,7 +9,7 @@ - [ ] CentOS 6+ - [ ] Archlinux - [ ] Ubuntu Current LTS+ -- [ ] Test Multiple BSD Devices - - [ ] FreeBSD - - [ ] OpenBSD - - [ ] DragonFlyBSD +- [x] Test Multiple BSD Devices + - [x] FreeBSD + - [x] OpenBSD + - [x] DragonFlyBSD From a72172d11e0ab607e6d9783e05e4624e11050784 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 13 Aug 2019 22:28:26 -0500 Subject: [PATCH 18/37] Adding update to checksum check function to always filter the checksum file based on downloaded file. Adding CYGWIN croc_os switch for windows support (needs more testing) --- src/install/rewrite.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 34167fc..da218d0 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -240,7 +240,10 @@ checksum_check() { cd "${dir}" || return 3 if command -v sha256sum >/dev/null 2>&1; then - sha256sum -c "${checksum_file}" --ignore-missing >/dev/null 2>&1 + ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + sha256sum -c "filtered_checksum.txt" >/dev/null 2>&1 rcode="${?}" elif command -v shasum >/dev/null 2>&1; then ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file @@ -459,6 +462,7 @@ main() { case "${croc_os}" in "Darwin" ) croc_os="macOS";; + "CYGWIN"* ) croc_os="Windows";; esac case "${croc_arch}" in From ec09a990afe27185853bfaa2a32832d91946e3a2 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 22:34:40 -0500 Subject: [PATCH 19/37] Adding croc_dl_ext update For when we are on a Windows/CYGWIN host --- src/install/rewrite.txt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index da218d0..86c0ef5 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -461,8 +461,9 @@ main() { fi case "${croc_os}" in - "Darwin" ) croc_os="macOS";; - "CYGWIN"* ) croc_os="Windows";; + "Darwin" ) croc_os="macOS";; + "CYGWIN"* ) croc_os="Windows"; + croc_dl_ext="zip";; esac case "${croc_arch}" in From a9caea4d64233ebe7b38c5aad4199da378f7614c Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 22:41:48 -0500 Subject: [PATCH 20/37] More WIndwos (cygwin) updates Add install_file_cygwin function to support this case --- src/install/rewrite.txt | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 86c0ef5..9fe183b 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -392,6 +392,43 @@ install_file_linux() { return "${rcode}" } +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_cygwin +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 2 = Could not find install command +#------------------------------------------------------------------------------- +install_file_linux() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -m 755 "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="3" + fi + fi + else + rcode="2" + fi + + return "${rcode}" +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: main # DESCRIPTION: Put it all together in a logical way From a31bc56c23a14528fcba532b3b3cceaae51fd3cb Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 22:45:59 -0500 Subject: [PATCH 21/37] More Cygwin updates Forgot to update the install logic case statement in main --- src/install/rewrite.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 9fe183b..e00afb1 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -589,6 +589,8 @@ main() { install_file_rcode="${?}";; "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; install_file_rcode="${?}";; + "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; esac if [[ "${install_file_rcode}" == "0" ]]; then print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" From 559807f97d773e029d170fbbf50ff65ab1f82f8f Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 22:51:06 -0500 Subject: [PATCH 22/37] Forgot to update the function name for cygwin --- src/install/rewrite.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index e00afb1..7f55c4c 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -402,7 +402,7 @@ install_file_linux() { # 1 = File not installed # 2 = Could not find install command #------------------------------------------------------------------------------- -install_file_linux() { +install_file_cygwin() { local file local prefix local rcode From 27bc2e8c8a57b9d4d74a5b03f985f97626a073da Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 23:09:13 -0500 Subject: [PATCH 23/37] Update rewrite_todo.md --- src/install/rewrite_todo.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index 504cf45..14e6169 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -2,8 +2,9 @@ - [ ] Test Multiple ARM Devices - [ ] Test Multiple ARM64 Devices - [ ] Test Windows (MobaXterm) - - [ ] Needs an override for croc_os and croc_dl_ext - - [ ] Needs new install_file_windws function + - [x] Needs an override for croc_os and croc_dl_ext + - [x] Needs new install_file_windws function + - [ ] Determine which binary to download when in cygwin - [ ] Test Multiple MacOS Devices - [ ] Test Multiple Linux Devices - [ ] CentOS 6+ From 9e9f43c352482b99e295930073f4b22b14da4fe1 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 13 Aug 2019 23:16:46 -0500 Subject: [PATCH 24/37] Update rewrite_todo.md --- src/install/rewrite_todo.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index 14e6169..14c332e 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -1,14 +1,14 @@ # To Do - [ ] Test Multiple ARM Devices - [ ] Test Multiple ARM64 Devices -- [ ] Test Windows (MobaXterm) +- [ ] Test Windows (MobaXterm/Cygwin) - [x] Needs an override for croc_os and croc_dl_ext - [x] Needs new install_file_windws function - - [ ] Determine which binary to download when in cygwin + - [x] ~~Determine which binary to download when in cygwin~~ Maybe there is a bug - [ ] Test Multiple MacOS Devices - [ ] Test Multiple Linux Devices - [ ] CentOS 6+ - - [ ] Archlinux + - [x] Archlinux - [ ] Ubuntu Current LTS+ - [x] Test Multiple BSD Devices - [x] FreeBSD From f3efbb34afe2bcdb82fdc8876c3e4a3c573753ec Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 14 Aug 2019 17:04:21 -0500 Subject: [PATCH 25/37] Update rewrite_todo.md --- src/install/rewrite_todo.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index 14c332e..ad15105 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -14,3 +14,4 @@ - [x] FreeBSD - [x] OpenBSD - [x] DragonFlyBSD +- [ ] wrong rcode https://github.com/TheQueasle/croc/blob/9e9f43c352482b99e295930073f4b22b14da4fe1/src/install/rewrite.txt#L241 From 51e77ba0c47fb4f1f88e0b28a9e0241576e6de19 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 14 Aug 2019 17:07:43 -0500 Subject: [PATCH 26/37] Update rewrite_todo.md --- src/install/rewrite_todo.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index ad15105..fdb6fb7 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -15,3 +15,4 @@ - [x] OpenBSD - [x] DragonFlyBSD - [ ] wrong rcode https://github.com/TheQueasle/croc/blob/9e9f43c352482b99e295930073f4b22b14da4fe1/src/install/rewrite.txt#L241 +- [ ] increase function rcodes to avoid clashing with tool rcodes From d3207e6190c5a36861c35ab81e0b3e23b6d3d80c Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Thu, 15 Aug 2019 20:06:04 -0500 Subject: [PATCH 27/37] First pass of upping rcodes for some of the functions that may have tool return codes clash. Also changing some logic for determine_os and determine_arch to make it a little more better --- src/install/rewrite.txt | 103 ++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 7f55c4c..6189207 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -23,7 +23,7 @@ # Issues: https://github.com/schollz/croc/issues # # CREATED: 08/10/2019 16:41 -# REVISION: 0.5.0 +# REVISION: 0.6.0 #=============================================================================== set -o nounset # Treat unset variables as an error @@ -106,6 +106,7 @@ print_message() { # PARAMETERS: $1 = Directory template # RETURNS: 0 = Created temp dir. Also prints temp file path to stdout # 1 = Failed to create temp dir +# 20 = Failed to find mktemp #------------------------------------------------------------------------------- make_tempdir() { local template @@ -123,6 +124,8 @@ make_tempdir() { else return 1 fi + else + return 20 fi } @@ -132,24 +135,21 @@ make_tempdir() { # PARAMETERS: none # RETURNS: 0 = OS Detected. Also prints detected os to stdout # 1 = Unkown OS -# 2 = 'uname' not found in path +# 20 = 'uname' not found in path #------------------------------------------------------------------------------- determine_os() { - local uname_cmd local uname_out - uname_cmd="$(command -v uname)" - if [[ "${uname_cmd}" == "" ]]; then - return 2 - else + if command -v uname >/dev/null 2>&1; then uname_out="$(uname)" - fi - - if [[ "${uname_out}" == "" ]]; then - return 1 + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi else - echo "${uname_out}" - return 0 + return 20 fi } @@ -159,25 +159,21 @@ determine_os() { # PARAMETERS: none # RETURNS: 0 = Arch Detected. Also prints detected arch to stdout # 1 = Unkown arch -# 2 = 'uname' not found in path +# 20 = 'uname' not found in path #------------------------------------------------------------------------------- determine_arch() { - local uname_cmd local uname_out - uname_cmd="$(command -v uname)" - - if [[ "${uname_cmd}" == "" ]]; then - return 2 - else + if command -v uname >/dev/null 2>&1; then uname_out="$(uname -m)" - fi - - if [[ "${uname_out}" == "" ]]; then - return 1 + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi else - echo "${uname_out}" - return 0 + return 20 fi } @@ -189,7 +185,7 @@ determine_arch() { # PARAMETERS: $1 = url of file to download # $2 = location to download file into on host system # RETURNS: If curl or wget found, returns the return code of curl or wget -# 2 = Could not find curl and wget +# 20 = Could not find curl and wget #------------------------------------------------------------------------------- download_file() { local url @@ -208,7 +204,7 @@ download_file() { wget --quiet "${url}" -O "${dir}/${filename}" rcode="${?}" else - rcode="2" + rcode="20" fi return "${rcode}" @@ -238,7 +234,7 @@ checksum_check() { file="${2}" dir="${3}" - cd "${dir}" || return 3 + cd "${dir}" || return 30 if command -v sha256sum >/dev/null 2>&1; then ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file ## to only include the file we downloaded. @@ -279,8 +275,8 @@ checksum_check() { # $2 = location to extract file into # $3 = extention # RETURNS: Return code of the tool used to extract the file -# 2 = Failed to determine which tool to use -# 3 = Failed to find tool in path +# 20 = Failed to determine which tool to use +# 30 = Failed to find tool in path #------------------------------------------------------------------------------- extract_file() { local file @@ -297,17 +293,17 @@ extract_file() { unzip "${file}" -d "${dir}" rcode="${?}" else - rcode="3" + rcode="30" fi ;; "tar.gz" ) if command -v tar >/dev/null 2>&1; then tar -xf "${file}" -C "${dir}" rcode="${?}" else - rcode="3" + rcode="30" fi ;; - * ) rcode="2";; + * ) rcode="20";; esac return "${rcode}" @@ -321,8 +317,8 @@ extract_file() { # $2 = location to install file into # RETURNS: 0 = File Installed # 1 = File not installed -# 2 = Could not find install command -# 3 = Could not find sudo command +# 20 = Could not find install command +# 21 = Could not find sudo command #------------------------------------------------------------------------------- install_file_freebsd() { local file @@ -341,11 +337,11 @@ install_file_freebsd() { sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" rcode="${?}" else - rcode="3" + rcode="21" fi fi else - rcode="2" + rcode="20" fi return "${rcode}" @@ -360,7 +356,8 @@ install_file_freebsd() { # $2 = location to install file into # RETURNS: 0 = File Installed # 1 = File not installed -# 2 = Could not find install command +# 20 = Could not find install command +# 21 = Could not find sudo command #------------------------------------------------------------------------------- install_file_linux() { local file @@ -382,11 +379,11 @@ install_file_linux() { install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" rcode="${?}" else - rcode="3" + rcode="21" fi fi else - rcode="2" + rcode="20" fi return "${rcode}" @@ -399,8 +396,8 @@ install_file_linux() { # PARAMETERS: $1 = file to install # $2 = location to install file into # RETURNS: 0 = File Installed -# 1 = File not installed -# 2 = Could not find install command +# 20 = Could not find install command +# 21 = Could not find sudo command #------------------------------------------------------------------------------- install_file_cygwin() { local file @@ -419,11 +416,11 @@ install_file_cygwin() { sudo install -m 755 "${file}" "${prefix}" rcode="${?}" else - rcode="3" + rcode="21" fi fi else - rcode="2" + rcode="20" fi return "${rcode}" @@ -470,8 +467,10 @@ main() { tmpdir_rcode="${?}" if [[ "${tmpdir_rcode}" == "0" ]]; then print_message "== Created temp dir at ${tmpdir}" "info" - else + elif [[ "${tmpdir_rcode}" == "1" ]]; then print_message "== Failed to create temp dir at ${tmpdir}" "error" + else + print_message "== 'mktemp' not found in path. Is it installed?" "error" exit 1 fi @@ -484,6 +483,7 @@ main() { exit 1 else print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 fi croc_os="$(determine_os)" @@ -495,6 +495,7 @@ main() { exit 1 else print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 fi case "${croc_os}" in @@ -538,7 +539,7 @@ main() { elif [[ "${download_checksum_file_rcode}" == "1" ]]; then print_message "== Failed to download croc checksums" "error" exit 1 - elif [[ "${download_checksum_file_rcode}" == "2" ]]; then + elif [[ "${download_checksum_file_rcode}" == "20" ]]; then print_message "== Failed to locate curl or wget" "error" exit 1 else @@ -571,10 +572,10 @@ main() { elif [[ "${extract_file_rcode}" == "1" ]]; then print_message "== Failed to extract ${croc_file}" "error" exit 1 - elif [[ "${extract_file_rcode}" == "2" ]]; then + elif [[ "${extract_file_rcode}" == "20" ]]; then print_message "== Failed to determine which extraction tool to use" "error" exit 1 - elif [[ "${extract_file_rcode}" == "3" ]]; then + elif [[ "${extract_file_rcode}" == "30" ]]; then print_message "== Failed to find extraction tool in path" "error" exit 1 else @@ -597,10 +598,10 @@ main() { elif [[ "${install_file_rcode}" == "1" ]]; then print_message "== Failed to install ${croc_bin_name}" "error" exit 1 - elif [[ "${install_file_rcode}" == "2" ]]; then + elif [[ "${install_file_rcode}" == "20" ]]; then print_message "== Failed to locate 'install' command" "error" exit 1 - elif [[ "${install_file_rcode}" == "3" ]]; then + elif [[ "${install_file_rcode}" == "21" ]]; then print_message "== Failed to locate 'sudo' command" "error" exit 1 else From d6d96e487f94316ce8dd90a21d304a962274a2a7 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Thu, 15 Aug 2019 20:48:46 -0500 Subject: [PATCH 28/37] fixing returncode for download_file --- src/install/rewrite.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt index 6189207..21fd73a 100644 --- a/src/install/rewrite.txt +++ b/src/install/rewrite.txt @@ -393,6 +393,7 @@ install_file_linux() { # NAME: install_file_cygwin # DESCRIPTION: Installs a file into a location using 'install'. If EUID not # 0, then attempt to use sudo. +# Not really 100% sure this is how to install croc in cygwin. # PARAMETERS: $1 = file to install # $2 = location to install file into # RETURNS: 0 = File Installed @@ -525,7 +526,7 @@ main() { elif [[ "${download_file_rcode}" == "1" ]]; then print_message "== Failed to download croc archive" "error" exit 1 - elif [[ "${download_file_rcode}" == "2" ]]; then + elif [[ "${download_file_rcode}" == "20" ]]; then print_message "== Failed to locate curl or wget" "error" exit 1 else From 105357bcbfeefe7092bc00b1d29aa8f790162a49 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Thu, 15 Aug 2019 20:49:30 -0500 Subject: [PATCH 29/37] Updating todo --- src/install/rewrite_todo.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index fdb6fb7..ce10544 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -7,10 +7,19 @@ - [x] ~~Determine which binary to download when in cygwin~~ Maybe there is a bug - [ ] Test Multiple MacOS Devices - [ ] Test Multiple Linux Devices - - [ ] CentOS 6+ - [x] Archlinux - - [ ] Ubuntu Current LTS+ -- [x] Test Multiple BSD Devices + - [ ] CentOS + - [x] CentOS 6 + - [x] CentOS 7 + - [ ] CentOS 8 (maybe) + - [x] Ubuntu + - [x] Ubuntu 14.04 x64 + - [x] Ubuntu 16.04 x64 + - [x] Ubuntu 18.04 x64 + - [x] Ubuntu 19.04 x64 + - [x] Ubuntu 14.04 x32 + - [x] Ubuntu 16.04 x32 +- [x] BSD - [x] FreeBSD - [x] OpenBSD - [x] DragonFlyBSD From e3e7ada3e625025520518b405cd11b089fe68d2d Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Thu, 15 Aug 2019 20:50:33 -0500 Subject: [PATCH 30/37] Updating todo --- src/install/rewrite_todo.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md index ce10544..8900a91 100644 --- a/src/install/rewrite_todo.md +++ b/src/install/rewrite_todo.md @@ -23,5 +23,4 @@ - [x] FreeBSD - [x] OpenBSD - [x] DragonFlyBSD -- [ ] wrong rcode https://github.com/TheQueasle/croc/blob/9e9f43c352482b99e295930073f4b22b14da4fe1/src/install/rewrite.txt#L241 -- [ ] increase function rcodes to avoid clashing with tool rcodes +- [x] increase function rcodes to avoid clashing with tool rcodes From 7a6c4db5f91a7fb0cd04aa022b991a5b4c9387a8 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Wed, 21 Aug 2019 22:47:51 -0500 Subject: [PATCH 31/37] Moving new file to default.txt. Removed todo (will be recreated in the pull request to come) --- src/install/default.txt | 805 +++++++++++++++++++++++++++--------- src/install/rewrite.txt | 637 ---------------------------- src/install/rewrite_todo.md | 26 -- 3 files changed, 621 insertions(+), 847 deletions(-) delete mode 100644 src/install/rewrite.txt delete mode 100644 src/install/rewrite_todo.md diff --git a/src/install/default.txt b/src/install/default.txt index d9d045b..21fd73a 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -1,200 +1,637 @@ -#!/usr/bin/env bash +#!/bin/bash - +#=============================================================================== # -# Adapted from https://github.com/caddyserver/getcaddy.com +# FILE: default.txt +# +# USAGE: curl https://getcroc.schollz.com | bash +# OR +# wget -qO- https://getcroc.schollz.com | bash +# +# DESCRIPTION: croc Installer Script. # -# croc Installer Script +# This script installs croc into a specified prefix. +# Default prefix = /usr/local/bin # -# Homepage: https://schollz.com/software/croc -# Issues: https://github.com/schollz/croc/issues -# Requires: bash, mv, rm, tr, type, curl/wget, base64, sudo (if not root) -# tar (or unzip on OSX and Windows) +# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" +# Prefix to install croc into. Defaults to /usr/local/bin +# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run +# as root), install, mktemp, sha256sum/shasum/sha256 # -# This script safely installs Caddy into your PATH (which may require -# password authorization). Use it like this: +# BUGS: ...hopefully not. Please report. # -# $ curl https://getcroc.schollz.com | bash -# or -# $ wget -qO- https://getcroc.schollz.com | bash -# -# In automated environments, you may want to run as root. -# If using curl, we recommend using the -fsSL flags. -# -# This should work on Mac, Linux, and BSD systems, and -# hopefully Windows with Cygwin. Please open an issue if -# you notice any bugs. +# NOTES: Homepage: https://schollz.com/software/croc +# Issues: https://github.com/schollz/croc/issues # +# CREATED: 08/10/2019 16:41 +# REVISION: 0.6.0 +#=============================================================================== +set -o nounset # Treat unset variables as an error -# [[ $- = *i* ]] && echo "Don't source this script!" && return 10 +#------------------------------------------------------------------------------- +# DEFAULTS +#------------------------------------------------------------------------------- +PREFIX="${PREFIX:-}" +ANDROID_ROOT="${ANDROID_ROOT:-}" -install_croc() -{ - trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; trap ERR; exit 1' ERR - install_path="/usr/local/bin" - croc_os="unsupported" - croc_arch="unknown" - croc_arm="" - croc_version="6.1.1" +# Termux on Android has ${PREFIX} set which already ends with '/usr' +if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then + INSTALL_PREFIX="${PREFIX}/bin" +else + INSTALL_PREFIX="/usr/local/bin" +fi +#------------------------------------------------------------------------------- +# FUNCTIONS +#------------------------------------------------------------------------------- - # Termux on Android has $PREFIX set which already ends with /usr - if [[ -n "$ANDROID_ROOT" && -n "$PREFIX" ]]; then - install_path="$PREFIX/bin" - fi +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_help +# DESCRIPTION: Prints out a help message +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_help() { + local help_header + local help_message - # Fall back to /usr/bin if necessary - if [[ ! -d $install_path ]]; then - install_path="/usr/bin" - fi + help_header="croc Installer Script" + help_message="Usage: + -p INSTALL_PREFIX + Prefix to install croc into. Directory must already exist. + Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) + + -h + Prints this helpfull message and exit." - # Not every platform has or needs sudo (https://termux.com/linux.html) - ((EUID)) && [[ -z "$ANDROID_ROOT" ]] && sudo_cmd="sudo" - - ######################### - # Which OS and version? # - ######################### - - croc_bin="croc" - croc_dl_ext=".tar.gz" - - # NOTE: `uname -m` is more accurate and universal than `arch` - # See https://en.wikipedia.org/wiki/Uname - unamem="$(uname -m)" - if [[ $unamem == *aarch64* ]]; then - croc_arch="ARM64" - elif [[ $unamem == *64* ]]; then - croc_arch="64bit" - elif [[ $unamem == *86* ]]; then - croc_arch="32bit" - elif [[ $unamem == *arm* ]]; then - croc_arch="ARM" - else - echo "Aborted, unsupported or unknown architecture: $unamem" - return 2 - fi - - unameu="$(tr '[:lower:]' '[:upper:]' <<<$(uname))" - if [[ $unameu == *DARWIN* ]]; then - croc_os="macOS" - vers=$(sw_vers) - version=${vers##*ProductVersion:} - IFS='.' read OSX_MAJOR OSX_MINOR _ <<<"$version" - - # # Major - # if ((OSX_MAJOR < 10)); then - # echo "Aborted, unsupported OS X version (9-)" - # return 3 - # fi - # if ((OSX_MAJOR > 10)); then - # echo "Aborted, unsupported OS X version (11+)" - # return 4 - # fi - - # # Minor - # if ((OSX_MINOR < 5)); then - # echo "Aborted, unsupported OS X version (10.5-)" - # return 5 - # fi - elif [[ $unameu == *LINUX* ]]; then - croc_os="Linux" - elif [[ $unameu == *FREEBSD* ]]; then - croc_os="FreeBSD" - elif [[ $unameu == *NETBSD* ]]; then - croc_os="NetBSD" - elif [[ $unameu == *OPENBSD* ]]; then - croc_os="OpenBSD" - elif [[ $unameu == *WIN* || $unameu == MSYS* ]]; then - # Should catch cygwin - sudo_cmd="" - croc_os="Windows" - croc_dl_ext=".zip" - croc_bin=$croc_bin.exe - else - echo "Aborted, unsupported or unknown os: $uname" - return 6 - fi - croc_file="croc_${croc_version}_${croc_os}-${croc_arch}${croc_dl_ext}" - - ######################## - # Download and extract # - ######################## - - croc_url="https://github.com/schollz/croc/releases/download/v${croc_version}/${croc_file}" - croc_checksum_url="https://github.com/schollz/croc/releases/download/v${croc_version}/croc_${croc_version}_checksums.txt" - echo "Downloading croc v${croc_version} (${croc_os} ${croc_arch})..." - - type -p gpg >/dev/null 2>&1 && gpg=1 || gpg=0 - - # Use $PREFIX for compatibility with Termux on Android - dl="$PREFIX$croc_file" - dl_checksum="$croc_file.checksum" - rm -rf -- "$dl" - rm -rf -- "$dl_checksum" - - - if type -p curl >/dev/null 2>&1; then - curl -fsSL "$croc_url" -o "$dl" - curl -fsSL "$croc_checksum_url" -o "$dl_checksum" - elif type -p wget >/dev/null 2>&1; then - wget --quiet "$croc_url" -O "$dl" - wget --quiet "$croc_checksum_url" -O "$dl_checksum" - else - echo "Aborted, could not find curl or wget" - return 7 - fi - - echo "Verifying checksum..." - if [[ $unameu == *DARWIN* ]]; then - checksum="$(shasum -a 256 ${dl})" - elif [[ $unameu == "FREEBSD" ]]; then - checksum="$(sha256 -q ${dl}) $croc_file" - else - checksum="$(sha256sum ${dl})" - fi - checksum_check="$(cat ${dl_checksum} | grep "${checksum}")" - - if [[ "${checksum}" != "${checksum_check}" ]]; then - echo "${checksum}" - echo "${checksum_check}" - echo "checksums are not valid, exiting" - return 7 - else - echo "verified checksum" - echo "Downloaded: ${checksum}" - echo "Remote: ${checksum_check}" - fi - - - echo "Extracting..." - case "$croc_file" in - *.zip) unzip -o "$dl" "$croc_bin" -d "$PREFIX/tmp/" ;; - *.tar.gz) tar -xzf "$dl" -C "$PREFIX/tmp/" "$croc_bin" ;; - esac - chmod +x "$PREFIX/tmp/$croc_bin" - - # Back up existing croc, if any found in path - if croc_path="$(type -p "$croc_bin")"; then - croc_backup="${croc_path}_old" - echo "Backing up $croc_path to $croc_backup" - echo "(Password may be required.)" - $sudo_cmd mv "$croc_path" "$croc_backup" - fi - - echo "Putting croc in $install_path (may require password)" - $sudo_cmd mv "$PREFIX/tmp/$croc_bin" "$install_path/$croc_bin" - if setcap_cmd=$(PATH+=$PATH:/sbin type -p setcap); then - $sudo_cmd $setcap_cmd cap_net_bind_service=+ep "$install_path/$croc_bin" - fi - $sudo_cmd rm -- "$dl" - $sudo_cmd rm -- "$dl_checksum" - - # check installation - $croc_bin -version - - echo "Successfully installed" - trap ERR - return 0 + echo "${help_header}" + echo "" + echo "${help_message}" } -install_croc "$@" +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_message +# DESCRIPTION: Prints a message all fancy like +# PARAMETERS: $1 = Message to print +# $2 = Severity. info, ok, error, warn +# RETURNS: Formatted Message to stdout +#------------------------------------------------------------------------------- +print_message() { + local message + local severity + local red + local green + local yellow + local nc + + message="${1}" + severity="${2}" + red='\e[0;31m' + green='\e[0;32m' + yellow='\e[1;33m' + nc='\e[0m' + + case "${severity}" in + "info" ) echo -e "${nc}${message}${nc}";; + "ok" ) echo -e "${green}${message}${nc}";; + "error" ) echo -e "${red}${message}${nc}";; + "warn" ) echo -e "${yellow}${message}${nc}";; + esac +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: make_tempdir +# DESCRIPTION: Makes a temp dir using mktemp if available +# PARAMETERS: $1 = Directory template +# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout +# 1 = Failed to create temp dir +# 20 = Failed to find mktemp +#------------------------------------------------------------------------------- +make_tempdir() { + local template + local tempdir + local tempdir_rcode + + template="${1}.XXXXXX" + + if command -v mktemp >/dev/null 2>&1; then + tempdir="$(mktemp -d -t "${template}")" + tempdir_rcode="${?}" + if [[ "${tempdir_rcode}" == "0" ]]; then + echo "${tempdir}" + return 0 + else + return 1 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_os +# DESCRIPTION: Attempts to determin host os using uname +# PARAMETERS: none +# RETURNS: 0 = OS Detected. Also prints detected os to stdout +# 1 = Unkown OS +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_os() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_arch +# DESCRIPTION: Attempt to determin architecture of host +# PARAMETERS: none +# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout +# 1 = Unkown arch +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_arch() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname -m)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: download_file +# DESCRIPTION: Downloads a file into the specified directory. Attempts to +# use curl, then wget. If neither is found, fail. +# PARAMETERS: $1 = url of file to download +# $2 = location to download file into on host system +# RETURNS: If curl or wget found, returns the return code of curl or wget +# 20 = Could not find curl and wget +#------------------------------------------------------------------------------- +download_file() { + local url + local dir + local filename + local rcode + + url="${1}" + dir="${2}" + filename="${3}" + + if command -v curl >/dev/null 2>&1; then + curl -fsSL "${url}" -o "${dir}/${filename}" + rcode="${?}" + elif command -v wget >/dev/null 2>&1; then + wget --quiet "${url}" -O "${dir}/${filename}" + rcode="${?}" + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: checksum_check +# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure +# integrity. Tries multiple tools before faling. +# PARAMETERS: $1 = path to checksum file +# $2 = location of file to check +# $3 = working directory +# RETURNS: 0 = checkusm verified +# 1 = checksum verification failed +# 20 = failed to determine tool to use to check checksum +# 30 = failed to change into or go back from working dir +#------------------------------------------------------------------------------- +checksum_check() { + local checksum_file + local file + local dir + local rcode + local shasum_1 + local shasum_2 + + checksum_file="${1}" + file="${2}" + dir="${3}" + + cd "${dir}" || return 30 + if command -v sha256sum >/dev/null 2>&1; then + ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + sha256sum -c "filtered_checksum.txt" >/dev/null 2>&1 + rcode="${?}" + elif command -v shasum >/dev/null 2>&1; then + ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum -s -a 256 -c "filtered_checksum.txt" + rcode="${?}" + elif command -v sha256 >/dev/null 2>&1; then + ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + ## Also sha256 -c option seems to fail, so fall back to an if statement + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_1="$(sha256 -q "${file}")" + shasum_2="$(awk '{print $1}' filtered_checksum.txt)" + if [[ "${shasum_1}" == "${shasum_2}" ]]; then + rcode="0" + else + rcode="1" + fi + else + return 20 + fi + cd - >/dev/null 2>&1 || return 30 + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: extract_file +# DESCRIPTION: Extracts a file into a location. Attempts to determine which +# tool to use by checking file extention. +# PARAMETERS: $1 = file to extract +# $2 = location to extract file into +# $3 = extention +# RETURNS: Return code of the tool used to extract the file +# 20 = Failed to determine which tool to use +# 30 = Failed to find tool in path +#------------------------------------------------------------------------------- +extract_file() { + local file + local dir + local ext + local rcode + + file="${1}" + dir="${2}" + ext="${3}" + + case "${ext}" in + "zip" ) if command -v unzip >/dev/null 2>&1; then + unzip "${file}" -d "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + "tar.gz" ) if command -v tar >/dev/null 2>&1; then + tar -xf "${file}" -C "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + * ) rcode="20";; + esac + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_freebsd +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_freebsd() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_linux +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo (unless on android). +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_linux() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + elif [[ "${ANDROID_ROOT}" != "" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_cygwin +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# Not really 100% sure this is how to install croc in cygwin. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_cygwin() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -m 755 "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: main +# DESCRIPTION: Put it all together in a logical way +# ...at least that is the hope... +# PARAMETERS: 1 = prefix +# RETURNS: 0 = All good +# 1 = Something done broke +#------------------------------------------------------------------------------- +main() { + local prefix + local tmpdir + local tmpdir_rcode + local croc_arch + local croc_arch_rcode + local croc_os + local croc_os_rcode + local croc_base_url + local croc_url + local croc_file + local croc_checksum_file + local croc_bin_name + local croc_version + local croc_dl_ext + local download_file_rcode + local download_checksum_file_rcode + local checksum_check_rcode + local extract_file_rcode + local install_file_rcode + + croc_bin_name="croc" + croc_version="6.1.1" + croc_dl_ext="tar.gz" + croc_base_url="https://github.com/schollz/croc/releases/download" + prefix="${1}" + + print_message "== Install prefix set to ${prefix}" "info" + + tmpdir="$(make_tempdir "${croc_bin_name}")" + tmpdir_rcode="${?}" + if [[ "${tmpdir_rcode}" == "0" ]]; then + print_message "== Created temp dir at ${tmpdir}" "info" + elif [[ "${tmpdir_rcode}" == "1" ]]; then + print_message "== Failed to create temp dir at ${tmpdir}" "error" + else + print_message "== 'mktemp' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_arch="$(determine_arch)" + croc_arch_rcode="${?}" + if [[ "${croc_arch_rcode}" == "0" ]]; then + print_message "== Architecture detected as ${croc_arch}" "info" + elif [[ "${croc_arch_rcode}" == "1" ]]; then + print_message "== Architecture not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_os="$(determine_os)" + croc_os_rcode="${?}" + if [[ "${croc_os_rcode}" == "0" ]]; then + print_message "== OS detected as ${croc_os}" "info" + elif [[ "${croc_os_rcode}" == "1" ]]; then + print_message "== OS not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + case "${croc_os}" in + "Darwin" ) croc_os="macOS";; + "CYGWIN"* ) croc_os="Windows"; + croc_dl_ext="zip";; + esac + + case "${croc_arch}" in + "x86_64" ) croc_arch="64bit";; + "amd64" ) croc_arch="64bit";; + "aarch64" ) croc_arch="ARM64";; + "armv7l" ) croc_arch="ARM";; + "i686" ) croc_arch="32bit";; + * ) croc_arch="unknown";; + esac + + croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" + croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" + croc_url="${croc_base_url}/v${croc_version}/${croc_file}" + croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" + + download_file "${croc_url}" "${tmpdir}" "${croc_file}" + download_file_rcode="${?}" + if [[ "${download_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc archive into ${tmpdir}" "info" + elif [[ "${download_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc archive" "error" + exit 1 + elif [[ "${download_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" + exit 1 + fi + download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" + download_checksum_file_rcode="${?}" + if [[ "${download_checksum_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc checksums file into ${tmpdir}" "info" + elif [[ "${download_checksum_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc checksums" "error" + exit 1 + elif [[ "${download_checksum_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" + exit 1 + fi + + checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" + checksum_check_rcode="${?}" + if [[ "${checksum_check_rcode}" == "0" ]]; then + print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" + elif [[ "${checksum_check_rcode}" == "1" ]]; then + print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "20" ]]; then + print_message "== Failed to find tool to verify sha256 sums" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "30" ]]; then + print_message "== Failed to change into working directory ${tmpdir}" "error" + exit 1 + else + print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" + exit 1 + fi + + extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" + extract_file_rcode="${?}" + if [[ "${extract_file_rcode}" == "0" ]]; then + print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" + elif [[ "${extract_file_rcode}" == "1" ]]; then + print_message "== Failed to extract ${croc_file}" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "20" ]]; then + print_message "== Failed to determine which extraction tool to use" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "30" ]]; then + print_message "== Failed to find extraction tool in path" "error" + exit 1 + else + print_message "== Unknown error returned from extraction attempt" "error" + exit 1 + fi + + case "${croc_os}" in + "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + esac + if [[ "${install_file_rcode}" == "0" ]]; then + print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" + elif [[ "${install_file_rcode}" == "1" ]]; then + print_message "== Failed to install ${croc_bin_name}" "error" + exit 1 + elif [[ "${install_file_rcode}" == "20" ]]; then + print_message "== Failed to locate 'install' command" "error" + exit 1 + elif [[ "${install_file_rcode}" == "21" ]]; then + print_message "== Failed to locate 'sudo' command" "error" + exit 1 + else + print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" + exit 1 + fi + + print_message "== Installation complete" "ok" + + exit 0 +} + +#------------------------------------------------------------------------------- +# ARGUMENT PARSING +#------------------------------------------------------------------------------- +OPTS="hp:" +while getopts "${OPTS}" optchar; do + case "${optchar}" in + 'h' ) print_help + exit 0 + ;; + 'p' ) INSTALL_PREFIX="${OPTARG}" + ;; + /? ) print_message "Unknown option ${OPTARG}" "warn" + ;; + esac +done + +#------------------------------------------------------------------------------- +# CALL MAIN +#------------------------------------------------------------------------------- +main "${INSTALL_PREFIX}" diff --git a/src/install/rewrite.txt b/src/install/rewrite.txt deleted file mode 100644 index 21fd73a..0000000 --- a/src/install/rewrite.txt +++ /dev/null @@ -1,637 +0,0 @@ -#!/bin/bash - -#=============================================================================== -# -# FILE: default.txt -# -# USAGE: curl https://getcroc.schollz.com | bash -# OR -# wget -qO- https://getcroc.schollz.com | bash -# -# DESCRIPTION: croc Installer Script. -# -# This script installs croc into a specified prefix. -# Default prefix = /usr/local/bin -# -# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" -# Prefix to install croc into. Defaults to /usr/local/bin -# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run -# as root), install, mktemp, sha256sum/shasum/sha256 -# -# BUGS: ...hopefully not. Please report. -# -# NOTES: Homepage: https://schollz.com/software/croc -# Issues: https://github.com/schollz/croc/issues -# -# CREATED: 08/10/2019 16:41 -# REVISION: 0.6.0 -#=============================================================================== -set -o nounset # Treat unset variables as an error - -#------------------------------------------------------------------------------- -# DEFAULTS -#------------------------------------------------------------------------------- -PREFIX="${PREFIX:-}" -ANDROID_ROOT="${ANDROID_ROOT:-}" - -# Termux on Android has ${PREFIX} set which already ends with '/usr' -if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then - INSTALL_PREFIX="${PREFIX}/bin" -else - INSTALL_PREFIX="/usr/local/bin" -fi - -#------------------------------------------------------------------------------- -# FUNCTIONS -#------------------------------------------------------------------------------- - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_help -# DESCRIPTION: Prints out a help message -# PARAMETERS: none -# RETURNS: 0 -#------------------------------------------------------------------------------- -print_help() { - local help_header - local help_message - - help_header="croc Installer Script" - help_message="Usage: - -p INSTALL_PREFIX - Prefix to install croc into. Directory must already exist. - Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) - - -h - Prints this helpfull message and exit." - - echo "${help_header}" - echo "" - echo "${help_message}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_message -# DESCRIPTION: Prints a message all fancy like -# PARAMETERS: $1 = Message to print -# $2 = Severity. info, ok, error, warn -# RETURNS: Formatted Message to stdout -#------------------------------------------------------------------------------- -print_message() { - local message - local severity - local red - local green - local yellow - local nc - - message="${1}" - severity="${2}" - red='\e[0;31m' - green='\e[0;32m' - yellow='\e[1;33m' - nc='\e[0m' - - case "${severity}" in - "info" ) echo -e "${nc}${message}${nc}";; - "ok" ) echo -e "${green}${message}${nc}";; - "error" ) echo -e "${red}${message}${nc}";; - "warn" ) echo -e "${yellow}${message}${nc}";; - esac - - -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: make_tempdir -# DESCRIPTION: Makes a temp dir using mktemp if available -# PARAMETERS: $1 = Directory template -# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout -# 1 = Failed to create temp dir -# 20 = Failed to find mktemp -#------------------------------------------------------------------------------- -make_tempdir() { - local template - local tempdir - local tempdir_rcode - - template="${1}.XXXXXX" - - if command -v mktemp >/dev/null 2>&1; then - tempdir="$(mktemp -d -t "${template}")" - tempdir_rcode="${?}" - if [[ "${tempdir_rcode}" == "0" ]]; then - echo "${tempdir}" - return 0 - else - return 1 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_os -# DESCRIPTION: Attempts to determin host os using uname -# PARAMETERS: none -# RETURNS: 0 = OS Detected. Also prints detected os to stdout -# 1 = Unkown OS -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_os() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_arch -# DESCRIPTION: Attempt to determin architecture of host -# PARAMETERS: none -# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout -# 1 = Unkown arch -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_arch() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname -m)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: download_file -# DESCRIPTION: Downloads a file into the specified directory. Attempts to -# use curl, then wget. If neither is found, fail. -# PARAMETERS: $1 = url of file to download -# $2 = location to download file into on host system -# RETURNS: If curl or wget found, returns the return code of curl or wget -# 20 = Could not find curl and wget -#------------------------------------------------------------------------------- -download_file() { - local url - local dir - local filename - local rcode - - url="${1}" - dir="${2}" - filename="${3}" - - if command -v curl >/dev/null 2>&1; then - curl -fsSL "${url}" -o "${dir}/${filename}" - rcode="${?}" - elif command -v wget >/dev/null 2>&1; then - wget --quiet "${url}" -O "${dir}/${filename}" - rcode="${?}" - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: checksum_check -# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure -# integrity. Tries multiple tools before faling. -# PARAMETERS: $1 = path to checksum file -# $2 = location of file to check -# $3 = working directory -# RETURNS: 0 = checkusm verified -# 1 = checksum verification failed -# 20 = failed to determine tool to use to check checksum -# 30 = failed to change into or go back from working dir -#------------------------------------------------------------------------------- -checksum_check() { - local checksum_file - local file - local dir - local rcode - local shasum_1 - local shasum_2 - - checksum_file="${1}" - file="${2}" - dir="${3}" - - cd "${dir}" || return 30 - if command -v sha256sum >/dev/null 2>&1; then - ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - sha256sum -c "filtered_checksum.txt" >/dev/null 2>&1 - rcode="${?}" - elif command -v shasum >/dev/null 2>&1; then - ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum -s -a 256 -c "filtered_checksum.txt" - rcode="${?}" - elif command -v sha256 >/dev/null 2>&1; then - ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - ## Also sha256 -c option seems to fail, so fall back to an if statement - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_1="$(sha256 -q "${file}")" - shasum_2="$(awk '{print $1}' filtered_checksum.txt)" - if [[ "${shasum_1}" == "${shasum_2}" ]]; then - rcode="0" - else - rcode="1" - fi - else - return 20 - fi - cd - >/dev/null 2>&1 || return 30 - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: extract_file -# DESCRIPTION: Extracts a file into a location. Attempts to determine which -# tool to use by checking file extention. -# PARAMETERS: $1 = file to extract -# $2 = location to extract file into -# $3 = extention -# RETURNS: Return code of the tool used to extract the file -# 20 = Failed to determine which tool to use -# 30 = Failed to find tool in path -#------------------------------------------------------------------------------- -extract_file() { - local file - local dir - local ext - local rcode - - file="${1}" - dir="${2}" - ext="${3}" - - case "${ext}" in - "zip" ) if command -v unzip >/dev/null 2>&1; then - unzip "${file}" -d "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - "tar.gz" ) if command -v tar >/dev/null 2>&1; then - tar -xf "${file}" -C "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - * ) rcode="20";; - esac - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_freebsd -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_freebsd() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_linux -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo (unless on android). -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_linux() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - elif [[ "${ANDROID_ROOT}" != "" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_cygwin -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# Not really 100% sure this is how to install croc in cygwin. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_cygwin() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -m 755 "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: main -# DESCRIPTION: Put it all together in a logical way -# ...at least that is the hope... -# PARAMETERS: 1 = prefix -# RETURNS: 0 = All good -# 1 = Something done broke -#------------------------------------------------------------------------------- -main() { - local prefix - local tmpdir - local tmpdir_rcode - local croc_arch - local croc_arch_rcode - local croc_os - local croc_os_rcode - local croc_base_url - local croc_url - local croc_file - local croc_checksum_file - local croc_bin_name - local croc_version - local croc_dl_ext - local download_file_rcode - local download_checksum_file_rcode - local checksum_check_rcode - local extract_file_rcode - local install_file_rcode - - croc_bin_name="croc" - croc_version="6.1.1" - croc_dl_ext="tar.gz" - croc_base_url="https://github.com/schollz/croc/releases/download" - prefix="${1}" - - print_message "== Install prefix set to ${prefix}" "info" - - tmpdir="$(make_tempdir "${croc_bin_name}")" - tmpdir_rcode="${?}" - if [[ "${tmpdir_rcode}" == "0" ]]; then - print_message "== Created temp dir at ${tmpdir}" "info" - elif [[ "${tmpdir_rcode}" == "1" ]]; then - print_message "== Failed to create temp dir at ${tmpdir}" "error" - else - print_message "== 'mktemp' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_arch="$(determine_arch)" - croc_arch_rcode="${?}" - if [[ "${croc_arch_rcode}" == "0" ]]; then - print_message "== Architecture detected as ${croc_arch}" "info" - elif [[ "${croc_arch_rcode}" == "1" ]]; then - print_message "== Architecture not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_os="$(determine_os)" - croc_os_rcode="${?}" - if [[ "${croc_os_rcode}" == "0" ]]; then - print_message "== OS detected as ${croc_os}" "info" - elif [[ "${croc_os_rcode}" == "1" ]]; then - print_message "== OS not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - case "${croc_os}" in - "Darwin" ) croc_os="macOS";; - "CYGWIN"* ) croc_os="Windows"; - croc_dl_ext="zip";; - esac - - case "${croc_arch}" in - "x86_64" ) croc_arch="64bit";; - "amd64" ) croc_arch="64bit";; - "aarch64" ) croc_arch="ARM64";; - "armv7l" ) croc_arch="ARM";; - "i686" ) croc_arch="32bit";; - * ) croc_arch="unknown";; - esac - - croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" - croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" - croc_url="${croc_base_url}/v${croc_version}/${croc_file}" - croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" - - download_file "${croc_url}" "${tmpdir}" "${croc_file}" - download_file_rcode="${?}" - if [[ "${download_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc archive into ${tmpdir}" "info" - elif [[ "${download_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc archive" "error" - exit 1 - elif [[ "${download_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" - exit 1 - fi - download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" - download_checksum_file_rcode="${?}" - if [[ "${download_checksum_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc checksums file into ${tmpdir}" "info" - elif [[ "${download_checksum_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc checksums" "error" - exit 1 - elif [[ "${download_checksum_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" - exit 1 - fi - - checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" - checksum_check_rcode="${?}" - if [[ "${checksum_check_rcode}" == "0" ]]; then - print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" - elif [[ "${checksum_check_rcode}" == "1" ]]; then - print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "20" ]]; then - print_message "== Failed to find tool to verify sha256 sums" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "30" ]]; then - print_message "== Failed to change into working directory ${tmpdir}" "error" - exit 1 - else - print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" - exit 1 - fi - - extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" - extract_file_rcode="${?}" - if [[ "${extract_file_rcode}" == "0" ]]; then - print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" - elif [[ "${extract_file_rcode}" == "1" ]]; then - print_message "== Failed to extract ${croc_file}" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "20" ]]; then - print_message "== Failed to determine which extraction tool to use" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "30" ]]; then - print_message "== Failed to find extraction tool in path" "error" - exit 1 - else - print_message "== Unknown error returned from extraction attempt" "error" - exit 1 - fi - - case "${croc_os}" in - "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - esac - if [[ "${install_file_rcode}" == "0" ]]; then - print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" - elif [[ "${install_file_rcode}" == "1" ]]; then - print_message "== Failed to install ${croc_bin_name}" "error" - exit 1 - elif [[ "${install_file_rcode}" == "20" ]]; then - print_message "== Failed to locate 'install' command" "error" - exit 1 - elif [[ "${install_file_rcode}" == "21" ]]; then - print_message "== Failed to locate 'sudo' command" "error" - exit 1 - else - print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" - exit 1 - fi - - print_message "== Installation complete" "ok" - - exit 0 -} - -#------------------------------------------------------------------------------- -# ARGUMENT PARSING -#------------------------------------------------------------------------------- -OPTS="hp:" -while getopts "${OPTS}" optchar; do - case "${optchar}" in - 'h' ) print_help - exit 0 - ;; - 'p' ) INSTALL_PREFIX="${OPTARG}" - ;; - /? ) print_message "Unknown option ${OPTARG}" "warn" - ;; - esac -done - -#------------------------------------------------------------------------------- -# CALL MAIN -#------------------------------------------------------------------------------- -main "${INSTALL_PREFIX}" diff --git a/src/install/rewrite_todo.md b/src/install/rewrite_todo.md deleted file mode 100644 index 8900a91..0000000 --- a/src/install/rewrite_todo.md +++ /dev/null @@ -1,26 +0,0 @@ -# To Do -- [ ] Test Multiple ARM Devices -- [ ] Test Multiple ARM64 Devices -- [ ] Test Windows (MobaXterm/Cygwin) - - [x] Needs an override for croc_os and croc_dl_ext - - [x] Needs new install_file_windws function - - [x] ~~Determine which binary to download when in cygwin~~ Maybe there is a bug -- [ ] Test Multiple MacOS Devices -- [ ] Test Multiple Linux Devices - - [x] Archlinux - - [ ] CentOS - - [x] CentOS 6 - - [x] CentOS 7 - - [ ] CentOS 8 (maybe) - - [x] Ubuntu - - [x] Ubuntu 14.04 x64 - - [x] Ubuntu 16.04 x64 - - [x] Ubuntu 18.04 x64 - - [x] Ubuntu 19.04 x64 - - [x] Ubuntu 14.04 x32 - - [x] Ubuntu 16.04 x32 -- [x] BSD - - [x] FreeBSD - - [x] OpenBSD - - [x] DragonFlyBSD -- [x] increase function rcodes to avoid clashing with tool rcodes From 07a2abd8082470e1692f6dfaf6b178a2e4a5696a Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Fri, 23 Aug 2019 11:05:57 -0500 Subject: [PATCH 32/37] Adding in a banner...because all the cool kids are doing it. --- src/install/default.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/install/default.txt b/src/install/default.txt index 21fd73a..74e3006 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -44,6 +44,31 @@ fi # FUNCTIONS #------------------------------------------------------------------------------- + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_banner +# DESCRIPTION: Prints a banner +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_banner() { + cat <<-'EOF' +================================================= + ____ + / ___|_ __ ___ ___ + | | | '__/ _ \ / __| + | |___| | | (_) | (__ + \____|_| \___/ \___| + + ___ _ _ _ + |_ _|_ __ ___| |_ __ _| | | ___ _ __ + | || '_ \/ __| __/ _` | | |/ _ \ '__| + | || | | \__ \ || (_| | | | __/ | + |___|_| |_|___/\__\__,_|_|_|\___|_| +================================================== +EOF +} + #--- FUNCTION ---------------------------------------------------------------- # NAME: print_help # DESCRIPTION: Prints out a help message @@ -462,6 +487,7 @@ main() { croc_base_url="https://github.com/schollz/croc/releases/download" prefix="${1}" + print_banner print_message "== Install prefix set to ${prefix}" "info" tmpdir="$(make_tempdir "${croc_bin_name}")" From 97f65bf51d16a2ee801192bc6f592a1db20e4fb2 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Fri, 23 Aug 2019 11:50:40 -0500 Subject: [PATCH 33/37] Adding in stdout of checksum check if the check fails --- src/install/default.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/install/default.txt b/src/install/default.txt index 74e3006..eb01df0 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -254,6 +254,7 @@ checksum_check() { local rcode local shasum_1 local shasum_2 + local shasum_c checksum_file="${1}" file="${2}" @@ -264,13 +265,13 @@ checksum_check() { ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file ## to only include the file we downloaded. grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - sha256sum -c "filtered_checksum.txt" >/dev/null 2>&1 + shasum_c="$(sha256sum -c "filtered_checksum.txt")" rcode="${?}" elif command -v shasum >/dev/null 2>&1; then ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file ## to only include the file we downloaded. grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum -s -a 256 -c "filtered_checksum.txt" + shasum_c="$(shasum -a 256 -c "filtered_checksum.txt")" rcode="${?}" elif command -v sha256 >/dev/null 2>&1; then ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file @@ -284,11 +285,15 @@ checksum_check() { else rcode="1" fi + shasum_c="Expected: ${shasum_1}, Got: ${shasum_2}" else return 20 fi cd - >/dev/null 2>&1 || return 30 + if [[ "${rcode}" -gt "0" ]]; then + echo "${shasum_c}" + fi return "${rcode}" } From 5fbb787a8408de5416b3fe452bc4a5d44623aeb8 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 10 Sep 2019 07:20:39 -0500 Subject: [PATCH 34/37] Adding in message and exit for cygwin. --- src/install/default.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/install/default.txt b/src/install/default.txt index eb01df0..d90b318 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -533,7 +533,9 @@ main() { case "${croc_os}" in "Darwin" ) croc_os="macOS";; "CYGWIN"* ) croc_os="Windows"; - croc_dl_ext="zip";; + croc_dl_ext="zip"; + print_message "== Cygwin is currently unsupported." "error"; + exit 1;; esac case "${croc_arch}" in From f01165e9f4349c1824dbab9383f1aab57d6e16dc Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 10 Sep 2019 07:29:17 -0500 Subject: [PATCH 35/37] Trying to get out of this merge confilct --- src/install/default.txt | 840 ++++++++---------------------------- src/install/new_default.txt | 670 ++++++++++++++++++++++++++++ 2 files changed, 855 insertions(+), 655 deletions(-) create mode 100644 src/install/new_default.txt diff --git a/src/install/default.txt b/src/install/default.txt index d90b318..d32cb27 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -1,670 +1,200 @@ -#!/bin/bash - -#=============================================================================== +#!/usr/bin/env bash # -# FILE: default.txt -# -# USAGE: curl https://getcroc.schollz.com | bash -# OR -# wget -qO- https://getcroc.schollz.com | bash -# -# DESCRIPTION: croc Installer Script. +# Adapted from https://github.com/caddyserver/getcaddy.com # -# This script installs croc into a specified prefix. -# Default prefix = /usr/local/bin +# croc Installer Script # -# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" -# Prefix to install croc into. Defaults to /usr/local/bin -# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run -# as root), install, mktemp, sha256sum/shasum/sha256 +# Homepage: https://schollz.com/software/croc +# Issues: https://github.com/schollz/croc/issues +# Requires: bash, mv, rm, tr, type, curl/wget, base64, sudo (if not root) +# tar (or unzip on OSX and Windows) # -# BUGS: ...hopefully not. Please report. +# This script safely installs Caddy into your PATH (which may require +# password authorization). Use it like this: # -# NOTES: Homepage: https://schollz.com/software/croc -# Issues: https://github.com/schollz/croc/issues +# $ curl https://getcroc.schollz.com | bash +# or +# $ wget -qO- https://getcroc.schollz.com | bash +# +# In automated environments, you may want to run as root. +# If using curl, we recommend using the -fsSL flags. +# +# This should work on Mac, Linux, and BSD systems, and +# hopefully Windows with Cygwin. Please open an issue if +# you notice any bugs. # -# CREATED: 08/10/2019 16:41 -# REVISION: 0.6.0 -#=============================================================================== -set -o nounset # Treat unset variables as an error -#------------------------------------------------------------------------------- -# DEFAULTS -#------------------------------------------------------------------------------- -PREFIX="${PREFIX:-}" -ANDROID_ROOT="${ANDROID_ROOT:-}" +# [[ $- = *i* ]] && echo "Don't source this script!" && return 10 -# Termux on Android has ${PREFIX} set which already ends with '/usr' -if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then - INSTALL_PREFIX="${PREFIX}/bin" -else - INSTALL_PREFIX="/usr/local/bin" -fi - -#------------------------------------------------------------------------------- -# FUNCTIONS -#------------------------------------------------------------------------------- +install_croc() +{ + trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; trap ERR; exit 1' ERR + install_path="/usr/local/bin" + croc_os="unsupported" + croc_arch="unknown" + croc_arm="" + croc_version="6.1.3" -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_banner -# DESCRIPTION: Prints a banner -# PARAMETERS: none -# RETURNS: 0 -#------------------------------------------------------------------------------- -print_banner() { - cat <<-'EOF' -================================================= - ____ - / ___|_ __ ___ ___ - | | | '__/ _ \ / __| - | |___| | | (_) | (__ - \____|_| \___/ \___| + # Termux on Android has $PREFIX set which already ends with /usr + if [[ -n "$ANDROID_ROOT" && -n "$PREFIX" ]]; then + install_path="$PREFIX/bin" + fi - ___ _ _ _ - |_ _|_ __ ___| |_ __ _| | | ___ _ __ - | || '_ \/ __| __/ _` | | |/ _ \ '__| - | || | | \__ \ || (_| | | | __/ | - |___|_| |_|___/\__\__,_|_|_|\___|_| -================================================== -EOF + # Fall back to /usr/bin if necessary + if [[ ! -d $install_path ]]; then + install_path="/usr/bin" + fi + + # Not every platform has or needs sudo (https://termux.com/linux.html) + ((EUID)) && [[ -z "$ANDROID_ROOT" ]] && sudo_cmd="sudo" + + ######################### + # Which OS and version? # + ######################### + + croc_bin="croc" + croc_dl_ext=".tar.gz" + + # NOTE: `uname -m` is more accurate and universal than `arch` + # See https://en.wikipedia.org/wiki/Uname + unamem="$(uname -m)" + if [[ $unamem == *aarch64* ]]; then + croc_arch="ARM64" + elif [[ $unamem == *64* ]]; then + croc_arch="64bit" + elif [[ $unamem == *86* ]]; then + croc_arch="32bit" + elif [[ $unamem == *arm* ]]; then + croc_arch="ARM" + else + echo "Aborted, unsupported or unknown architecture: $unamem" + return 2 + fi + + unameu="$(tr '[:lower:]' '[:upper:]' <<<$(uname))" + if [[ $unameu == *DARWIN* ]]; then + croc_os="macOS" + vers=$(sw_vers) + version=${vers##*ProductVersion:} + IFS='.' read OSX_MAJOR OSX_MINOR _ <<<"$version" + + # # Major + # if ((OSX_MAJOR < 10)); then + # echo "Aborted, unsupported OS X version (9-)" + # return 3 + # fi + # if ((OSX_MAJOR > 10)); then + # echo "Aborted, unsupported OS X version (11+)" + # return 4 + # fi + + # # Minor + # if ((OSX_MINOR < 5)); then + # echo "Aborted, unsupported OS X version (10.5-)" + # return 5 + # fi + elif [[ $unameu == *LINUX* ]]; then + croc_os="Linux" + elif [[ $unameu == *FREEBSD* ]]; then + croc_os="FreeBSD" + elif [[ $unameu == *NETBSD* ]]; then + croc_os="NetBSD" + elif [[ $unameu == *OPENBSD* ]]; then + croc_os="OpenBSD" + elif [[ $unameu == *WIN* || $unameu == MSYS* ]]; then + # Should catch cygwin + sudo_cmd="" + croc_os="Windows" + croc_dl_ext=".zip" + croc_bin=$croc_bin.exe + else + echo "Aborted, unsupported or unknown os: $uname" + return 6 + fi + croc_file="croc_${croc_version}_${croc_os}-${croc_arch}${croc_dl_ext}" + + ######################## + # Download and extract # + ######################## + + croc_url="https://github.com/schollz/croc/releases/download/v${croc_version}/${croc_file}" + croc_checksum_url="https://github.com/schollz/croc/releases/download/v${croc_version}/croc_${croc_version}_checksums.txt" + echo "Downloading croc v${croc_version} (${croc_os} ${croc_arch})..." + + type -p gpg >/dev/null 2>&1 && gpg=1 || gpg=0 + + # Use $PREFIX for compatibility with Termux on Android + dl="$PREFIX$croc_file" + dl_checksum="$croc_file.checksum" + rm -rf -- "$dl" + rm -rf -- "$dl_checksum" + + + if type -p curl >/dev/null 2>&1; then + curl -fsSL "$croc_url" -o "$dl" + curl -fsSL "$croc_checksum_url" -o "$dl_checksum" + elif type -p wget >/dev/null 2>&1; then + wget --quiet "$croc_url" -O "$dl" + wget --quiet "$croc_checksum_url" -O "$dl_checksum" + else + echo "Aborted, could not find curl or wget" + return 7 + fi + + echo "Verifying checksum..." + if [[ $unameu == *DARWIN* ]]; then + checksum="$(shasum -a 256 ${dl})" + elif [[ $unameu == "FREEBSD" ]]; then + checksum="$(sha256 -q ${dl}) $croc_file" + else + checksum="$(sha256sum ${dl})" + fi + checksum_check="$(cat ${dl_checksum} | grep "${checksum}")" + + if [[ "${checksum}" != "${checksum_check}" ]]; then + echo "${checksum}" + echo "${checksum_check}" + echo "checksums are not valid, exiting" + return 7 + else + echo "verified checksum" + echo "Downloaded: ${checksum}" + echo "Remote: ${checksum_check}" + fi + + + echo "Extracting..." + case "$croc_file" in + *.zip) unzip -o "$dl" "$croc_bin" -d "$PREFIX/tmp/" ;; + *.tar.gz) tar -xzf "$dl" -C "$PREFIX/tmp/" "$croc_bin" ;; + esac + chmod +x "$PREFIX/tmp/$croc_bin" + + # Back up existing croc, if any found in path + if croc_path="$(type -p "$croc_bin")"; then + croc_backup="${croc_path}_old" + echo "Backing up $croc_path to $croc_backup" + echo "(Password may be required.)" + $sudo_cmd mv "$croc_path" "$croc_backup" + fi + + echo "Putting croc in $install_path (may require password)" + $sudo_cmd mv "$PREFIX/tmp/$croc_bin" "$install_path/$croc_bin" + if setcap_cmd=$(PATH+=$PATH:/sbin type -p setcap); then + $sudo_cmd $setcap_cmd cap_net_bind_service=+ep "$install_path/$croc_bin" + fi + $sudo_cmd rm -- "$dl" + $sudo_cmd rm -- "$dl_checksum" + + # check installation + $croc_bin -version + + echo "Successfully installed" + trap ERR + return 0 } -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_help -# DESCRIPTION: Prints out a help message -# PARAMETERS: none -# RETURNS: 0 -#------------------------------------------------------------------------------- -print_help() { - local help_header - local help_message - - help_header="croc Installer Script" - help_message="Usage: - -p INSTALL_PREFIX - Prefix to install croc into. Directory must already exist. - Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) - - -h - Prints this helpfull message and exit." - - echo "${help_header}" - echo "" - echo "${help_message}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_message -# DESCRIPTION: Prints a message all fancy like -# PARAMETERS: $1 = Message to print -# $2 = Severity. info, ok, error, warn -# RETURNS: Formatted Message to stdout -#------------------------------------------------------------------------------- -print_message() { - local message - local severity - local red - local green - local yellow - local nc - - message="${1}" - severity="${2}" - red='\e[0;31m' - green='\e[0;32m' - yellow='\e[1;33m' - nc='\e[0m' - - case "${severity}" in - "info" ) echo -e "${nc}${message}${nc}";; - "ok" ) echo -e "${green}${message}${nc}";; - "error" ) echo -e "${red}${message}${nc}";; - "warn" ) echo -e "${yellow}${message}${nc}";; - esac +install_croc "$@" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: make_tempdir -# DESCRIPTION: Makes a temp dir using mktemp if available -# PARAMETERS: $1 = Directory template -# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout -# 1 = Failed to create temp dir -# 20 = Failed to find mktemp -#------------------------------------------------------------------------------- -make_tempdir() { - local template - local tempdir - local tempdir_rcode - - template="${1}.XXXXXX" - - if command -v mktemp >/dev/null 2>&1; then - tempdir="$(mktemp -d -t "${template}")" - tempdir_rcode="${?}" - if [[ "${tempdir_rcode}" == "0" ]]; then - echo "${tempdir}" - return 0 - else - return 1 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_os -# DESCRIPTION: Attempts to determin host os using uname -# PARAMETERS: none -# RETURNS: 0 = OS Detected. Also prints detected os to stdout -# 1 = Unkown OS -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_os() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_arch -# DESCRIPTION: Attempt to determin architecture of host -# PARAMETERS: none -# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout -# 1 = Unkown arch -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_arch() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname -m)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: download_file -# DESCRIPTION: Downloads a file into the specified directory. Attempts to -# use curl, then wget. If neither is found, fail. -# PARAMETERS: $1 = url of file to download -# $2 = location to download file into on host system -# RETURNS: If curl or wget found, returns the return code of curl or wget -# 20 = Could not find curl and wget -#------------------------------------------------------------------------------- -download_file() { - local url - local dir - local filename - local rcode - - url="${1}" - dir="${2}" - filename="${3}" - - if command -v curl >/dev/null 2>&1; then - curl -fsSL "${url}" -o "${dir}/${filename}" - rcode="${?}" - elif command -v wget >/dev/null 2>&1; then - wget --quiet "${url}" -O "${dir}/${filename}" - rcode="${?}" - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: checksum_check -# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure -# integrity. Tries multiple tools before faling. -# PARAMETERS: $1 = path to checksum file -# $2 = location of file to check -# $3 = working directory -# RETURNS: 0 = checkusm verified -# 1 = checksum verification failed -# 20 = failed to determine tool to use to check checksum -# 30 = failed to change into or go back from working dir -#------------------------------------------------------------------------------- -checksum_check() { - local checksum_file - local file - local dir - local rcode - local shasum_1 - local shasum_2 - local shasum_c - - checksum_file="${1}" - file="${2}" - dir="${3}" - - cd "${dir}" || return 30 - if command -v sha256sum >/dev/null 2>&1; then - ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_c="$(sha256sum -c "filtered_checksum.txt")" - rcode="${?}" - elif command -v shasum >/dev/null 2>&1; then - ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_c="$(shasum -a 256 -c "filtered_checksum.txt")" - rcode="${?}" - elif command -v sha256 >/dev/null 2>&1; then - ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - ## Also sha256 -c option seems to fail, so fall back to an if statement - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_1="$(sha256 -q "${file}")" - shasum_2="$(awk '{print $1}' filtered_checksum.txt)" - if [[ "${shasum_1}" == "${shasum_2}" ]]; then - rcode="0" - else - rcode="1" - fi - shasum_c="Expected: ${shasum_1}, Got: ${shasum_2}" - else - return 20 - fi - cd - >/dev/null 2>&1 || return 30 - - if [[ "${rcode}" -gt "0" ]]; then - echo "${shasum_c}" - fi - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: extract_file -# DESCRIPTION: Extracts a file into a location. Attempts to determine which -# tool to use by checking file extention. -# PARAMETERS: $1 = file to extract -# $2 = location to extract file into -# $3 = extention -# RETURNS: Return code of the tool used to extract the file -# 20 = Failed to determine which tool to use -# 30 = Failed to find tool in path -#------------------------------------------------------------------------------- -extract_file() { - local file - local dir - local ext - local rcode - - file="${1}" - dir="${2}" - ext="${3}" - - case "${ext}" in - "zip" ) if command -v unzip >/dev/null 2>&1; then - unzip "${file}" -d "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - "tar.gz" ) if command -v tar >/dev/null 2>&1; then - tar -xf "${file}" -C "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - * ) rcode="20";; - esac - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_freebsd -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_freebsd() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_linux -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo (unless on android). -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_linux() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - elif [[ "${ANDROID_ROOT}" != "" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_cygwin -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# Not really 100% sure this is how to install croc in cygwin. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_cygwin() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -m 755 "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: main -# DESCRIPTION: Put it all together in a logical way -# ...at least that is the hope... -# PARAMETERS: 1 = prefix -# RETURNS: 0 = All good -# 1 = Something done broke -#------------------------------------------------------------------------------- -main() { - local prefix - local tmpdir - local tmpdir_rcode - local croc_arch - local croc_arch_rcode - local croc_os - local croc_os_rcode - local croc_base_url - local croc_url - local croc_file - local croc_checksum_file - local croc_bin_name - local croc_version - local croc_dl_ext - local download_file_rcode - local download_checksum_file_rcode - local checksum_check_rcode - local extract_file_rcode - local install_file_rcode - - croc_bin_name="croc" - croc_version="6.1.1" - croc_dl_ext="tar.gz" - croc_base_url="https://github.com/schollz/croc/releases/download" - prefix="${1}" - - print_banner - print_message "== Install prefix set to ${prefix}" "info" - - tmpdir="$(make_tempdir "${croc_bin_name}")" - tmpdir_rcode="${?}" - if [[ "${tmpdir_rcode}" == "0" ]]; then - print_message "== Created temp dir at ${tmpdir}" "info" - elif [[ "${tmpdir_rcode}" == "1" ]]; then - print_message "== Failed to create temp dir at ${tmpdir}" "error" - else - print_message "== 'mktemp' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_arch="$(determine_arch)" - croc_arch_rcode="${?}" - if [[ "${croc_arch_rcode}" == "0" ]]; then - print_message "== Architecture detected as ${croc_arch}" "info" - elif [[ "${croc_arch_rcode}" == "1" ]]; then - print_message "== Architecture not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_os="$(determine_os)" - croc_os_rcode="${?}" - if [[ "${croc_os_rcode}" == "0" ]]; then - print_message "== OS detected as ${croc_os}" "info" - elif [[ "${croc_os_rcode}" == "1" ]]; then - print_message "== OS not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - case "${croc_os}" in - "Darwin" ) croc_os="macOS";; - "CYGWIN"* ) croc_os="Windows"; - croc_dl_ext="zip"; - print_message "== Cygwin is currently unsupported." "error"; - exit 1;; - esac - - case "${croc_arch}" in - "x86_64" ) croc_arch="64bit";; - "amd64" ) croc_arch="64bit";; - "aarch64" ) croc_arch="ARM64";; - "armv7l" ) croc_arch="ARM";; - "i686" ) croc_arch="32bit";; - * ) croc_arch="unknown";; - esac - - croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" - croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" - croc_url="${croc_base_url}/v${croc_version}/${croc_file}" - croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" - - download_file "${croc_url}" "${tmpdir}" "${croc_file}" - download_file_rcode="${?}" - if [[ "${download_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc archive into ${tmpdir}" "info" - elif [[ "${download_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc archive" "error" - exit 1 - elif [[ "${download_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" - exit 1 - fi - download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" - download_checksum_file_rcode="${?}" - if [[ "${download_checksum_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc checksums file into ${tmpdir}" "info" - elif [[ "${download_checksum_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc checksums" "error" - exit 1 - elif [[ "${download_checksum_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" - exit 1 - fi - - checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" - checksum_check_rcode="${?}" - if [[ "${checksum_check_rcode}" == "0" ]]; then - print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" - elif [[ "${checksum_check_rcode}" == "1" ]]; then - print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "20" ]]; then - print_message "== Failed to find tool to verify sha256 sums" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "30" ]]; then - print_message "== Failed to change into working directory ${tmpdir}" "error" - exit 1 - else - print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" - exit 1 - fi - - extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" - extract_file_rcode="${?}" - if [[ "${extract_file_rcode}" == "0" ]]; then - print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" - elif [[ "${extract_file_rcode}" == "1" ]]; then - print_message "== Failed to extract ${croc_file}" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "20" ]]; then - print_message "== Failed to determine which extraction tool to use" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "30" ]]; then - print_message "== Failed to find extraction tool in path" "error" - exit 1 - else - print_message "== Unknown error returned from extraction attempt" "error" - exit 1 - fi - - case "${croc_os}" in - "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - esac - if [[ "${install_file_rcode}" == "0" ]]; then - print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" - elif [[ "${install_file_rcode}" == "1" ]]; then - print_message "== Failed to install ${croc_bin_name}" "error" - exit 1 - elif [[ "${install_file_rcode}" == "20" ]]; then - print_message "== Failed to locate 'install' command" "error" - exit 1 - elif [[ "${install_file_rcode}" == "21" ]]; then - print_message "== Failed to locate 'sudo' command" "error" - exit 1 - else - print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" - exit 1 - fi - - print_message "== Installation complete" "ok" - - exit 0 -} - -#------------------------------------------------------------------------------- -# ARGUMENT PARSING -#------------------------------------------------------------------------------- -OPTS="hp:" -while getopts "${OPTS}" optchar; do - case "${optchar}" in - 'h' ) print_help - exit 0 - ;; - 'p' ) INSTALL_PREFIX="${OPTARG}" - ;; - /? ) print_message "Unknown option ${OPTARG}" "warn" - ;; - esac -done - -#------------------------------------------------------------------------------- -# CALL MAIN -#------------------------------------------------------------------------------- -main "${INSTALL_PREFIX}" diff --git a/src/install/new_default.txt b/src/install/new_default.txt new file mode 100644 index 0000000..d90b318 --- /dev/null +++ b/src/install/new_default.txt @@ -0,0 +1,670 @@ +#!/bin/bash - +#=============================================================================== +# +# FILE: default.txt +# +# USAGE: curl https://getcroc.schollz.com | bash +# OR +# wget -qO- https://getcroc.schollz.com | bash +# +# DESCRIPTION: croc Installer Script. +# +# This script installs croc into a specified prefix. +# Default prefix = /usr/local/bin +# +# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" +# Prefix to install croc into. Defaults to /usr/local/bin +# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run +# as root), install, mktemp, sha256sum/shasum/sha256 +# +# BUGS: ...hopefully not. Please report. +# +# NOTES: Homepage: https://schollz.com/software/croc +# Issues: https://github.com/schollz/croc/issues +# +# CREATED: 08/10/2019 16:41 +# REVISION: 0.6.0 +#=============================================================================== +set -o nounset # Treat unset variables as an error + +#------------------------------------------------------------------------------- +# DEFAULTS +#------------------------------------------------------------------------------- +PREFIX="${PREFIX:-}" +ANDROID_ROOT="${ANDROID_ROOT:-}" + +# Termux on Android has ${PREFIX} set which already ends with '/usr' +if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then + INSTALL_PREFIX="${PREFIX}/bin" +else + INSTALL_PREFIX="/usr/local/bin" +fi + +#------------------------------------------------------------------------------- +# FUNCTIONS +#------------------------------------------------------------------------------- + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_banner +# DESCRIPTION: Prints a banner +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_banner() { + cat <<-'EOF' +================================================= + ____ + / ___|_ __ ___ ___ + | | | '__/ _ \ / __| + | |___| | | (_) | (__ + \____|_| \___/ \___| + + ___ _ _ _ + |_ _|_ __ ___| |_ __ _| | | ___ _ __ + | || '_ \/ __| __/ _` | | |/ _ \ '__| + | || | | \__ \ || (_| | | | __/ | + |___|_| |_|___/\__\__,_|_|_|\___|_| +================================================== +EOF +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_help +# DESCRIPTION: Prints out a help message +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_help() { + local help_header + local help_message + + help_header="croc Installer Script" + help_message="Usage: + -p INSTALL_PREFIX + Prefix to install croc into. Directory must already exist. + Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) + + -h + Prints this helpfull message and exit." + + echo "${help_header}" + echo "" + echo "${help_message}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_message +# DESCRIPTION: Prints a message all fancy like +# PARAMETERS: $1 = Message to print +# $2 = Severity. info, ok, error, warn +# RETURNS: Formatted Message to stdout +#------------------------------------------------------------------------------- +print_message() { + local message + local severity + local red + local green + local yellow + local nc + + message="${1}" + severity="${2}" + red='\e[0;31m' + green='\e[0;32m' + yellow='\e[1;33m' + nc='\e[0m' + + case "${severity}" in + "info" ) echo -e "${nc}${message}${nc}";; + "ok" ) echo -e "${green}${message}${nc}";; + "error" ) echo -e "${red}${message}${nc}";; + "warn" ) echo -e "${yellow}${message}${nc}";; + esac + + +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: make_tempdir +# DESCRIPTION: Makes a temp dir using mktemp if available +# PARAMETERS: $1 = Directory template +# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout +# 1 = Failed to create temp dir +# 20 = Failed to find mktemp +#------------------------------------------------------------------------------- +make_tempdir() { + local template + local tempdir + local tempdir_rcode + + template="${1}.XXXXXX" + + if command -v mktemp >/dev/null 2>&1; then + tempdir="$(mktemp -d -t "${template}")" + tempdir_rcode="${?}" + if [[ "${tempdir_rcode}" == "0" ]]; then + echo "${tempdir}" + return 0 + else + return 1 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_os +# DESCRIPTION: Attempts to determin host os using uname +# PARAMETERS: none +# RETURNS: 0 = OS Detected. Also prints detected os to stdout +# 1 = Unkown OS +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_os() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_arch +# DESCRIPTION: Attempt to determin architecture of host +# PARAMETERS: none +# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout +# 1 = Unkown arch +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_arch() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname -m)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: download_file +# DESCRIPTION: Downloads a file into the specified directory. Attempts to +# use curl, then wget. If neither is found, fail. +# PARAMETERS: $1 = url of file to download +# $2 = location to download file into on host system +# RETURNS: If curl or wget found, returns the return code of curl or wget +# 20 = Could not find curl and wget +#------------------------------------------------------------------------------- +download_file() { + local url + local dir + local filename + local rcode + + url="${1}" + dir="${2}" + filename="${3}" + + if command -v curl >/dev/null 2>&1; then + curl -fsSL "${url}" -o "${dir}/${filename}" + rcode="${?}" + elif command -v wget >/dev/null 2>&1; then + wget --quiet "${url}" -O "${dir}/${filename}" + rcode="${?}" + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: checksum_check +# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure +# integrity. Tries multiple tools before faling. +# PARAMETERS: $1 = path to checksum file +# $2 = location of file to check +# $3 = working directory +# RETURNS: 0 = checkusm verified +# 1 = checksum verification failed +# 20 = failed to determine tool to use to check checksum +# 30 = failed to change into or go back from working dir +#------------------------------------------------------------------------------- +checksum_check() { + local checksum_file + local file + local dir + local rcode + local shasum_1 + local shasum_2 + local shasum_c + + checksum_file="${1}" + file="${2}" + dir="${3}" + + cd "${dir}" || return 30 + if command -v sha256sum >/dev/null 2>&1; then + ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_c="$(sha256sum -c "filtered_checksum.txt")" + rcode="${?}" + elif command -v shasum >/dev/null 2>&1; then + ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_c="$(shasum -a 256 -c "filtered_checksum.txt")" + rcode="${?}" + elif command -v sha256 >/dev/null 2>&1; then + ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + ## Also sha256 -c option seems to fail, so fall back to an if statement + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_1="$(sha256 -q "${file}")" + shasum_2="$(awk '{print $1}' filtered_checksum.txt)" + if [[ "${shasum_1}" == "${shasum_2}" ]]; then + rcode="0" + else + rcode="1" + fi + shasum_c="Expected: ${shasum_1}, Got: ${shasum_2}" + else + return 20 + fi + cd - >/dev/null 2>&1 || return 30 + + if [[ "${rcode}" -gt "0" ]]; then + echo "${shasum_c}" + fi + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: extract_file +# DESCRIPTION: Extracts a file into a location. Attempts to determine which +# tool to use by checking file extention. +# PARAMETERS: $1 = file to extract +# $2 = location to extract file into +# $3 = extention +# RETURNS: Return code of the tool used to extract the file +# 20 = Failed to determine which tool to use +# 30 = Failed to find tool in path +#------------------------------------------------------------------------------- +extract_file() { + local file + local dir + local ext + local rcode + + file="${1}" + dir="${2}" + ext="${3}" + + case "${ext}" in + "zip" ) if command -v unzip >/dev/null 2>&1; then + unzip "${file}" -d "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + "tar.gz" ) if command -v tar >/dev/null 2>&1; then + tar -xf "${file}" -C "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + * ) rcode="20";; + esac + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_freebsd +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_freebsd() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_linux +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo (unless on android). +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_linux() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + elif [[ "${ANDROID_ROOT}" != "" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_cygwin +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# Not really 100% sure this is how to install croc in cygwin. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_cygwin() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -m 755 "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: main +# DESCRIPTION: Put it all together in a logical way +# ...at least that is the hope... +# PARAMETERS: 1 = prefix +# RETURNS: 0 = All good +# 1 = Something done broke +#------------------------------------------------------------------------------- +main() { + local prefix + local tmpdir + local tmpdir_rcode + local croc_arch + local croc_arch_rcode + local croc_os + local croc_os_rcode + local croc_base_url + local croc_url + local croc_file + local croc_checksum_file + local croc_bin_name + local croc_version + local croc_dl_ext + local download_file_rcode + local download_checksum_file_rcode + local checksum_check_rcode + local extract_file_rcode + local install_file_rcode + + croc_bin_name="croc" + croc_version="6.1.1" + croc_dl_ext="tar.gz" + croc_base_url="https://github.com/schollz/croc/releases/download" + prefix="${1}" + + print_banner + print_message "== Install prefix set to ${prefix}" "info" + + tmpdir="$(make_tempdir "${croc_bin_name}")" + tmpdir_rcode="${?}" + if [[ "${tmpdir_rcode}" == "0" ]]; then + print_message "== Created temp dir at ${tmpdir}" "info" + elif [[ "${tmpdir_rcode}" == "1" ]]; then + print_message "== Failed to create temp dir at ${tmpdir}" "error" + else + print_message "== 'mktemp' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_arch="$(determine_arch)" + croc_arch_rcode="${?}" + if [[ "${croc_arch_rcode}" == "0" ]]; then + print_message "== Architecture detected as ${croc_arch}" "info" + elif [[ "${croc_arch_rcode}" == "1" ]]; then + print_message "== Architecture not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_os="$(determine_os)" + croc_os_rcode="${?}" + if [[ "${croc_os_rcode}" == "0" ]]; then + print_message "== OS detected as ${croc_os}" "info" + elif [[ "${croc_os_rcode}" == "1" ]]; then + print_message "== OS not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + case "${croc_os}" in + "Darwin" ) croc_os="macOS";; + "CYGWIN"* ) croc_os="Windows"; + croc_dl_ext="zip"; + print_message "== Cygwin is currently unsupported." "error"; + exit 1;; + esac + + case "${croc_arch}" in + "x86_64" ) croc_arch="64bit";; + "amd64" ) croc_arch="64bit";; + "aarch64" ) croc_arch="ARM64";; + "armv7l" ) croc_arch="ARM";; + "i686" ) croc_arch="32bit";; + * ) croc_arch="unknown";; + esac + + croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" + croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" + croc_url="${croc_base_url}/v${croc_version}/${croc_file}" + croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" + + download_file "${croc_url}" "${tmpdir}" "${croc_file}" + download_file_rcode="${?}" + if [[ "${download_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc archive into ${tmpdir}" "info" + elif [[ "${download_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc archive" "error" + exit 1 + elif [[ "${download_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" + exit 1 + fi + download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" + download_checksum_file_rcode="${?}" + if [[ "${download_checksum_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc checksums file into ${tmpdir}" "info" + elif [[ "${download_checksum_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc checksums" "error" + exit 1 + elif [[ "${download_checksum_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" + exit 1 + fi + + checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" + checksum_check_rcode="${?}" + if [[ "${checksum_check_rcode}" == "0" ]]; then + print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" + elif [[ "${checksum_check_rcode}" == "1" ]]; then + print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "20" ]]; then + print_message "== Failed to find tool to verify sha256 sums" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "30" ]]; then + print_message "== Failed to change into working directory ${tmpdir}" "error" + exit 1 + else + print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" + exit 1 + fi + + extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" + extract_file_rcode="${?}" + if [[ "${extract_file_rcode}" == "0" ]]; then + print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" + elif [[ "${extract_file_rcode}" == "1" ]]; then + print_message "== Failed to extract ${croc_file}" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "20" ]]; then + print_message "== Failed to determine which extraction tool to use" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "30" ]]; then + print_message "== Failed to find extraction tool in path" "error" + exit 1 + else + print_message "== Unknown error returned from extraction attempt" "error" + exit 1 + fi + + case "${croc_os}" in + "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + esac + if [[ "${install_file_rcode}" == "0" ]]; then + print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" + elif [[ "${install_file_rcode}" == "1" ]]; then + print_message "== Failed to install ${croc_bin_name}" "error" + exit 1 + elif [[ "${install_file_rcode}" == "20" ]]; then + print_message "== Failed to locate 'install' command" "error" + exit 1 + elif [[ "${install_file_rcode}" == "21" ]]; then + print_message "== Failed to locate 'sudo' command" "error" + exit 1 + else + print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" + exit 1 + fi + + print_message "== Installation complete" "ok" + + exit 0 +} + +#------------------------------------------------------------------------------- +# ARGUMENT PARSING +#------------------------------------------------------------------------------- +OPTS="hp:" +while getopts "${OPTS}" optchar; do + case "${optchar}" in + 'h' ) print_help + exit 0 + ;; + 'p' ) INSTALL_PREFIX="${OPTARG}" + ;; + /? ) print_message "Unknown option ${OPTARG}" "warn" + ;; + esac +done + +#------------------------------------------------------------------------------- +# CALL MAIN +#------------------------------------------------------------------------------- +main "${INSTALL_PREFIX}" From fe4e9c357a3bef8f1951ce173dc10466f47d27ae Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 10 Sep 2019 07:30:31 -0500 Subject: [PATCH 36/37] Upping version of croc in the installer script. --- src/install/new_default.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/install/new_default.txt b/src/install/new_default.txt index d90b318..464f9ab 100644 --- a/src/install/new_default.txt +++ b/src/install/new_default.txt @@ -23,7 +23,7 @@ # Issues: https://github.com/schollz/croc/issues # # CREATED: 08/10/2019 16:41 -# REVISION: 0.6.0 +# REVISION: 0.9.0 #=============================================================================== set -o nounset # Treat unset variables as an error @@ -487,7 +487,7 @@ main() { local install_file_rcode croc_bin_name="croc" - croc_version="6.1.1" + croc_version="6.1.3" croc_dl_ext="tar.gz" croc_base_url="https://github.com/schollz/croc/releases/download" prefix="${1}" From 932f037b17a46d12bf3665e829988a006f66f912 Mon Sep 17 00:00:00 2001 From: Micheal Quinn Date: Tue, 10 Sep 2019 07:31:13 -0500 Subject: [PATCH 37/37] Moving the new script back into place. I think the merge conflict is resolved now. --- src/install/default.txt | 840 ++++++++++++++++++++++++++++-------- src/install/new_default.txt | 670 ---------------------------- 2 files changed, 655 insertions(+), 855 deletions(-) delete mode 100644 src/install/new_default.txt diff --git a/src/install/default.txt b/src/install/default.txt index d32cb27..464f9ab 100644 --- a/src/install/default.txt +++ b/src/install/default.txt @@ -1,200 +1,670 @@ -#!/usr/bin/env bash +#!/bin/bash - +#=============================================================================== # -# Adapted from https://github.com/caddyserver/getcaddy.com +# FILE: default.txt +# +# USAGE: curl https://getcroc.schollz.com | bash +# OR +# wget -qO- https://getcroc.schollz.com | bash +# +# DESCRIPTION: croc Installer Script. # -# croc Installer Script +# This script installs croc into a specified prefix. +# Default prefix = /usr/local/bin # -# Homepage: https://schollz.com/software/croc -# Issues: https://github.com/schollz/croc/issues -# Requires: bash, mv, rm, tr, type, curl/wget, base64, sudo (if not root) -# tar (or unzip on OSX and Windows) +# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" +# Prefix to install croc into. Defaults to /usr/local/bin +# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run +# as root), install, mktemp, sha256sum/shasum/sha256 # -# This script safely installs Caddy into your PATH (which may require -# password authorization). Use it like this: +# BUGS: ...hopefully not. Please report. # -# $ curl https://getcroc.schollz.com | bash -# or -# $ wget -qO- https://getcroc.schollz.com | bash -# -# In automated environments, you may want to run as root. -# If using curl, we recommend using the -fsSL flags. -# -# This should work on Mac, Linux, and BSD systems, and -# hopefully Windows with Cygwin. Please open an issue if -# you notice any bugs. +# NOTES: Homepage: https://schollz.com/software/croc +# Issues: https://github.com/schollz/croc/issues # +# CREATED: 08/10/2019 16:41 +# REVISION: 0.9.0 +#=============================================================================== +set -o nounset # Treat unset variables as an error -# [[ $- = *i* ]] && echo "Don't source this script!" && return 10 +#------------------------------------------------------------------------------- +# DEFAULTS +#------------------------------------------------------------------------------- +PREFIX="${PREFIX:-}" +ANDROID_ROOT="${ANDROID_ROOT:-}" -install_croc() -{ - trap 'echo -e "Aborted, error $? in command: $BASH_COMMAND"; trap ERR; exit 1' ERR - install_path="/usr/local/bin" - croc_os="unsupported" - croc_arch="unknown" - croc_arm="" - croc_version="6.1.3" +# Termux on Android has ${PREFIX} set which already ends with '/usr' +if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then + INSTALL_PREFIX="${PREFIX}/bin" +else + INSTALL_PREFIX="/usr/local/bin" +fi + +#------------------------------------------------------------------------------- +# FUNCTIONS +#------------------------------------------------------------------------------- - # Termux on Android has $PREFIX set which already ends with /usr - if [[ -n "$ANDROID_ROOT" && -n "$PREFIX" ]]; then - install_path="$PREFIX/bin" - fi +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_banner +# DESCRIPTION: Prints a banner +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_banner() { + cat <<-'EOF' +================================================= + ____ + / ___|_ __ ___ ___ + | | | '__/ _ \ / __| + | |___| | | (_) | (__ + \____|_| \___/ \___| - # Fall back to /usr/bin if necessary - if [[ ! -d $install_path ]]; then - install_path="/usr/bin" - fi - - # Not every platform has or needs sudo (https://termux.com/linux.html) - ((EUID)) && [[ -z "$ANDROID_ROOT" ]] && sudo_cmd="sudo" - - ######################### - # Which OS and version? # - ######################### - - croc_bin="croc" - croc_dl_ext=".tar.gz" - - # NOTE: `uname -m` is more accurate and universal than `arch` - # See https://en.wikipedia.org/wiki/Uname - unamem="$(uname -m)" - if [[ $unamem == *aarch64* ]]; then - croc_arch="ARM64" - elif [[ $unamem == *64* ]]; then - croc_arch="64bit" - elif [[ $unamem == *86* ]]; then - croc_arch="32bit" - elif [[ $unamem == *arm* ]]; then - croc_arch="ARM" - else - echo "Aborted, unsupported or unknown architecture: $unamem" - return 2 - fi - - unameu="$(tr '[:lower:]' '[:upper:]' <<<$(uname))" - if [[ $unameu == *DARWIN* ]]; then - croc_os="macOS" - vers=$(sw_vers) - version=${vers##*ProductVersion:} - IFS='.' read OSX_MAJOR OSX_MINOR _ <<<"$version" - - # # Major - # if ((OSX_MAJOR < 10)); then - # echo "Aborted, unsupported OS X version (9-)" - # return 3 - # fi - # if ((OSX_MAJOR > 10)); then - # echo "Aborted, unsupported OS X version (11+)" - # return 4 - # fi - - # # Minor - # if ((OSX_MINOR < 5)); then - # echo "Aborted, unsupported OS X version (10.5-)" - # return 5 - # fi - elif [[ $unameu == *LINUX* ]]; then - croc_os="Linux" - elif [[ $unameu == *FREEBSD* ]]; then - croc_os="FreeBSD" - elif [[ $unameu == *NETBSD* ]]; then - croc_os="NetBSD" - elif [[ $unameu == *OPENBSD* ]]; then - croc_os="OpenBSD" - elif [[ $unameu == *WIN* || $unameu == MSYS* ]]; then - # Should catch cygwin - sudo_cmd="" - croc_os="Windows" - croc_dl_ext=".zip" - croc_bin=$croc_bin.exe - else - echo "Aborted, unsupported or unknown os: $uname" - return 6 - fi - croc_file="croc_${croc_version}_${croc_os}-${croc_arch}${croc_dl_ext}" - - ######################## - # Download and extract # - ######################## - - croc_url="https://github.com/schollz/croc/releases/download/v${croc_version}/${croc_file}" - croc_checksum_url="https://github.com/schollz/croc/releases/download/v${croc_version}/croc_${croc_version}_checksums.txt" - echo "Downloading croc v${croc_version} (${croc_os} ${croc_arch})..." - - type -p gpg >/dev/null 2>&1 && gpg=1 || gpg=0 - - # Use $PREFIX for compatibility with Termux on Android - dl="$PREFIX$croc_file" - dl_checksum="$croc_file.checksum" - rm -rf -- "$dl" - rm -rf -- "$dl_checksum" - - - if type -p curl >/dev/null 2>&1; then - curl -fsSL "$croc_url" -o "$dl" - curl -fsSL "$croc_checksum_url" -o "$dl_checksum" - elif type -p wget >/dev/null 2>&1; then - wget --quiet "$croc_url" -O "$dl" - wget --quiet "$croc_checksum_url" -O "$dl_checksum" - else - echo "Aborted, could not find curl or wget" - return 7 - fi - - echo "Verifying checksum..." - if [[ $unameu == *DARWIN* ]]; then - checksum="$(shasum -a 256 ${dl})" - elif [[ $unameu == "FREEBSD" ]]; then - checksum="$(sha256 -q ${dl}) $croc_file" - else - checksum="$(sha256sum ${dl})" - fi - checksum_check="$(cat ${dl_checksum} | grep "${checksum}")" - - if [[ "${checksum}" != "${checksum_check}" ]]; then - echo "${checksum}" - echo "${checksum_check}" - echo "checksums are not valid, exiting" - return 7 - else - echo "verified checksum" - echo "Downloaded: ${checksum}" - echo "Remote: ${checksum_check}" - fi - - - echo "Extracting..." - case "$croc_file" in - *.zip) unzip -o "$dl" "$croc_bin" -d "$PREFIX/tmp/" ;; - *.tar.gz) tar -xzf "$dl" -C "$PREFIX/tmp/" "$croc_bin" ;; - esac - chmod +x "$PREFIX/tmp/$croc_bin" - - # Back up existing croc, if any found in path - if croc_path="$(type -p "$croc_bin")"; then - croc_backup="${croc_path}_old" - echo "Backing up $croc_path to $croc_backup" - echo "(Password may be required.)" - $sudo_cmd mv "$croc_path" "$croc_backup" - fi - - echo "Putting croc in $install_path (may require password)" - $sudo_cmd mv "$PREFIX/tmp/$croc_bin" "$install_path/$croc_bin" - if setcap_cmd=$(PATH+=$PATH:/sbin type -p setcap); then - $sudo_cmd $setcap_cmd cap_net_bind_service=+ep "$install_path/$croc_bin" - fi - $sudo_cmd rm -- "$dl" - $sudo_cmd rm -- "$dl_checksum" - - # check installation - $croc_bin -version - - echo "Successfully installed" - trap ERR - return 0 + ___ _ _ _ + |_ _|_ __ ___| |_ __ _| | | ___ _ __ + | || '_ \/ __| __/ _` | | |/ _ \ '__| + | || | | \__ \ || (_| | | | __/ | + |___|_| |_|___/\__\__,_|_|_|\___|_| +================================================== +EOF } -install_croc "$@" +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_help +# DESCRIPTION: Prints out a help message +# PARAMETERS: none +# RETURNS: 0 +#------------------------------------------------------------------------------- +print_help() { + local help_header + local help_message + + help_header="croc Installer Script" + help_message="Usage: + -p INSTALL_PREFIX + Prefix to install croc into. Directory must already exist. + Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) + + -h + Prints this helpfull message and exit." + + echo "${help_header}" + echo "" + echo "${help_message}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: print_message +# DESCRIPTION: Prints a message all fancy like +# PARAMETERS: $1 = Message to print +# $2 = Severity. info, ok, error, warn +# RETURNS: Formatted Message to stdout +#------------------------------------------------------------------------------- +print_message() { + local message + local severity + local red + local green + local yellow + local nc + + message="${1}" + severity="${2}" + red='\e[0;31m' + green='\e[0;32m' + yellow='\e[1;33m' + nc='\e[0m' + + case "${severity}" in + "info" ) echo -e "${nc}${message}${nc}";; + "ok" ) echo -e "${green}${message}${nc}";; + "error" ) echo -e "${red}${message}${nc}";; + "warn" ) echo -e "${yellow}${message}${nc}";; + esac +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: make_tempdir +# DESCRIPTION: Makes a temp dir using mktemp if available +# PARAMETERS: $1 = Directory template +# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout +# 1 = Failed to create temp dir +# 20 = Failed to find mktemp +#------------------------------------------------------------------------------- +make_tempdir() { + local template + local tempdir + local tempdir_rcode + + template="${1}.XXXXXX" + + if command -v mktemp >/dev/null 2>&1; then + tempdir="$(mktemp -d -t "${template}")" + tempdir_rcode="${?}" + if [[ "${tempdir_rcode}" == "0" ]]; then + echo "${tempdir}" + return 0 + else + return 1 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_os +# DESCRIPTION: Attempts to determin host os using uname +# PARAMETERS: none +# RETURNS: 0 = OS Detected. Also prints detected os to stdout +# 1 = Unkown OS +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_os() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: determine_arch +# DESCRIPTION: Attempt to determin architecture of host +# PARAMETERS: none +# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout +# 1 = Unkown arch +# 20 = 'uname' not found in path +#------------------------------------------------------------------------------- +determine_arch() { + local uname_out + + if command -v uname >/dev/null 2>&1; then + uname_out="$(uname -m)" + if [[ "${uname_out}" == "" ]]; then + return 1 + else + echo "${uname_out}" + return 0 + fi + else + return 20 + fi +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: download_file +# DESCRIPTION: Downloads a file into the specified directory. Attempts to +# use curl, then wget. If neither is found, fail. +# PARAMETERS: $1 = url of file to download +# $2 = location to download file into on host system +# RETURNS: If curl or wget found, returns the return code of curl or wget +# 20 = Could not find curl and wget +#------------------------------------------------------------------------------- +download_file() { + local url + local dir + local filename + local rcode + + url="${1}" + dir="${2}" + filename="${3}" + + if command -v curl >/dev/null 2>&1; then + curl -fsSL "${url}" -o "${dir}/${filename}" + rcode="${?}" + elif command -v wget >/dev/null 2>&1; then + wget --quiet "${url}" -O "${dir}/${filename}" + rcode="${?}" + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: checksum_check +# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure +# integrity. Tries multiple tools before faling. +# PARAMETERS: $1 = path to checksum file +# $2 = location of file to check +# $3 = working directory +# RETURNS: 0 = checkusm verified +# 1 = checksum verification failed +# 20 = failed to determine tool to use to check checksum +# 30 = failed to change into or go back from working dir +#------------------------------------------------------------------------------- +checksum_check() { + local checksum_file + local file + local dir + local rcode + local shasum_1 + local shasum_2 + local shasum_c + + checksum_file="${1}" + file="${2}" + dir="${3}" + + cd "${dir}" || return 30 + if command -v sha256sum >/dev/null 2>&1; then + ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_c="$(sha256sum -c "filtered_checksum.txt")" + rcode="${?}" + elif command -v shasum >/dev/null 2>&1; then + ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_c="$(shasum -a 256 -c "filtered_checksum.txt")" + rcode="${?}" + elif command -v sha256 >/dev/null 2>&1; then + ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file + ## to only include the file we downloaded. + ## Also sha256 -c option seems to fail, so fall back to an if statement + grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt + shasum_1="$(sha256 -q "${file}")" + shasum_2="$(awk '{print $1}' filtered_checksum.txt)" + if [[ "${shasum_1}" == "${shasum_2}" ]]; then + rcode="0" + else + rcode="1" + fi + shasum_c="Expected: ${shasum_1}, Got: ${shasum_2}" + else + return 20 + fi + cd - >/dev/null 2>&1 || return 30 + + if [[ "${rcode}" -gt "0" ]]; then + echo "${shasum_c}" + fi + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: extract_file +# DESCRIPTION: Extracts a file into a location. Attempts to determine which +# tool to use by checking file extention. +# PARAMETERS: $1 = file to extract +# $2 = location to extract file into +# $3 = extention +# RETURNS: Return code of the tool used to extract the file +# 20 = Failed to determine which tool to use +# 30 = Failed to find tool in path +#------------------------------------------------------------------------------- +extract_file() { + local file + local dir + local ext + local rcode + + file="${1}" + dir="${2}" + ext="${3}" + + case "${ext}" in + "zip" ) if command -v unzip >/dev/null 2>&1; then + unzip "${file}" -d "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + "tar.gz" ) if command -v tar >/dev/null 2>&1; then + tar -xf "${file}" -C "${dir}" + rcode="${?}" + else + rcode="30" + fi + ;; + * ) rcode="20";; + esac + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_freebsd +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_freebsd() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_linux +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo (unless on android). +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 1 = File not installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_linux() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" + rcode="${?}" + elif [[ "${ANDROID_ROOT}" != "" ]]; then + install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: install_file_cygwin +# DESCRIPTION: Installs a file into a location using 'install'. If EUID not +# 0, then attempt to use sudo. +# Not really 100% sure this is how to install croc in cygwin. +# PARAMETERS: $1 = file to install +# $2 = location to install file into +# RETURNS: 0 = File Installed +# 20 = Could not find install command +# 21 = Could not find sudo command +#------------------------------------------------------------------------------- +install_file_cygwin() { + local file + local prefix + local rcode + + file="${1}" + prefix="${2}" + + if command -v install >/dev/null 2>&1; then + if [[ "${EUID}" == "0" ]]; then + install -m 755 "${prefix}" "${file}" + rcode="${?}" + else + if command -v sudo >/dev/null 2>&1; then + sudo install -m 755 "${file}" "${prefix}" + rcode="${?}" + else + rcode="21" + fi + fi + else + rcode="20" + fi + + return "${rcode}" +} + +#--- FUNCTION ---------------------------------------------------------------- +# NAME: main +# DESCRIPTION: Put it all together in a logical way +# ...at least that is the hope... +# PARAMETERS: 1 = prefix +# RETURNS: 0 = All good +# 1 = Something done broke +#------------------------------------------------------------------------------- +main() { + local prefix + local tmpdir + local tmpdir_rcode + local croc_arch + local croc_arch_rcode + local croc_os + local croc_os_rcode + local croc_base_url + local croc_url + local croc_file + local croc_checksum_file + local croc_bin_name + local croc_version + local croc_dl_ext + local download_file_rcode + local download_checksum_file_rcode + local checksum_check_rcode + local extract_file_rcode + local install_file_rcode + + croc_bin_name="croc" + croc_version="6.1.3" + croc_dl_ext="tar.gz" + croc_base_url="https://github.com/schollz/croc/releases/download" + prefix="${1}" + + print_banner + print_message "== Install prefix set to ${prefix}" "info" + + tmpdir="$(make_tempdir "${croc_bin_name}")" + tmpdir_rcode="${?}" + if [[ "${tmpdir_rcode}" == "0" ]]; then + print_message "== Created temp dir at ${tmpdir}" "info" + elif [[ "${tmpdir_rcode}" == "1" ]]; then + print_message "== Failed to create temp dir at ${tmpdir}" "error" + else + print_message "== 'mktemp' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_arch="$(determine_arch)" + croc_arch_rcode="${?}" + if [[ "${croc_arch_rcode}" == "0" ]]; then + print_message "== Architecture detected as ${croc_arch}" "info" + elif [[ "${croc_arch_rcode}" == "1" ]]; then + print_message "== Architecture not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + croc_os="$(determine_os)" + croc_os_rcode="${?}" + if [[ "${croc_os_rcode}" == "0" ]]; then + print_message "== OS detected as ${croc_os}" "info" + elif [[ "${croc_os_rcode}" == "1" ]]; then + print_message "== OS not detected" "error" + exit 1 + else + print_message "== 'uname' not found in path. Is it installed?" "error" + exit 1 + fi + + case "${croc_os}" in + "Darwin" ) croc_os="macOS";; + "CYGWIN"* ) croc_os="Windows"; + croc_dl_ext="zip"; + print_message "== Cygwin is currently unsupported." "error"; + exit 1;; + esac + + case "${croc_arch}" in + "x86_64" ) croc_arch="64bit";; + "amd64" ) croc_arch="64bit";; + "aarch64" ) croc_arch="ARM64";; + "armv7l" ) croc_arch="ARM";; + "i686" ) croc_arch="32bit";; + * ) croc_arch="unknown";; + esac + + croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" + croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" + croc_url="${croc_base_url}/v${croc_version}/${croc_file}" + croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" + + download_file "${croc_url}" "${tmpdir}" "${croc_file}" + download_file_rcode="${?}" + if [[ "${download_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc archive into ${tmpdir}" "info" + elif [[ "${download_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc archive" "error" + exit 1 + elif [[ "${download_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" + exit 1 + fi + download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" + download_checksum_file_rcode="${?}" + if [[ "${download_checksum_file_rcode}" == "0" ]]; then + print_message "== Downloaded croc checksums file into ${tmpdir}" "info" + elif [[ "${download_checksum_file_rcode}" == "1" ]]; then + print_message "== Failed to download croc checksums" "error" + exit 1 + elif [[ "${download_checksum_file_rcode}" == "20" ]]; then + print_message "== Failed to locate curl or wget" "error" + exit 1 + else + print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" + exit 1 + fi + + checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" + checksum_check_rcode="${?}" + if [[ "${checksum_check_rcode}" == "0" ]]; then + print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" + elif [[ "${checksum_check_rcode}" == "1" ]]; then + print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "20" ]]; then + print_message "== Failed to find tool to verify sha256 sums" "error" + exit 1 + elif [[ "${checksum_check_rcode}" == "30" ]]; then + print_message "== Failed to change into working directory ${tmpdir}" "error" + exit 1 + else + print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" + exit 1 + fi + + extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" + extract_file_rcode="${?}" + if [[ "${extract_file_rcode}" == "0" ]]; then + print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" + elif [[ "${extract_file_rcode}" == "1" ]]; then + print_message "== Failed to extract ${croc_file}" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "20" ]]; then + print_message "== Failed to determine which extraction tool to use" "error" + exit 1 + elif [[ "${extract_file_rcode}" == "30" ]]; then + print_message "== Failed to find extraction tool in path" "error" + exit 1 + else + print_message "== Unknown error returned from extraction attempt" "error" + exit 1 + fi + + case "${croc_os}" in + "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; + install_file_rcode="${?}";; + esac + if [[ "${install_file_rcode}" == "0" ]]; then + print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" + elif [[ "${install_file_rcode}" == "1" ]]; then + print_message "== Failed to install ${croc_bin_name}" "error" + exit 1 + elif [[ "${install_file_rcode}" == "20" ]]; then + print_message "== Failed to locate 'install' command" "error" + exit 1 + elif [[ "${install_file_rcode}" == "21" ]]; then + print_message "== Failed to locate 'sudo' command" "error" + exit 1 + else + print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" + exit 1 + fi + + print_message "== Installation complete" "ok" + + exit 0 +} + +#------------------------------------------------------------------------------- +# ARGUMENT PARSING +#------------------------------------------------------------------------------- +OPTS="hp:" +while getopts "${OPTS}" optchar; do + case "${optchar}" in + 'h' ) print_help + exit 0 + ;; + 'p' ) INSTALL_PREFIX="${OPTARG}" + ;; + /? ) print_message "Unknown option ${OPTARG}" "warn" + ;; + esac +done + +#------------------------------------------------------------------------------- +# CALL MAIN +#------------------------------------------------------------------------------- +main "${INSTALL_PREFIX}" diff --git a/src/install/new_default.txt b/src/install/new_default.txt deleted file mode 100644 index 464f9ab..0000000 --- a/src/install/new_default.txt +++ /dev/null @@ -1,670 +0,0 @@ -#!/bin/bash - -#=============================================================================== -# -# FILE: default.txt -# -# USAGE: curl https://getcroc.schollz.com | bash -# OR -# wget -qO- https://getcroc.schollz.com | bash -# -# DESCRIPTION: croc Installer Script. -# -# This script installs croc into a specified prefix. -# Default prefix = /usr/local/bin -# -# OPTIONS: -p, --prefix "${INSTALL_PREFIX}" -# Prefix to install croc into. Defaults to /usr/local/bin -# REQUIREMENTS: bash, uname, tar/unzip, curl/wget, sudo (if not run -# as root), install, mktemp, sha256sum/shasum/sha256 -# -# BUGS: ...hopefully not. Please report. -# -# NOTES: Homepage: https://schollz.com/software/croc -# Issues: https://github.com/schollz/croc/issues -# -# CREATED: 08/10/2019 16:41 -# REVISION: 0.9.0 -#=============================================================================== -set -o nounset # Treat unset variables as an error - -#------------------------------------------------------------------------------- -# DEFAULTS -#------------------------------------------------------------------------------- -PREFIX="${PREFIX:-}" -ANDROID_ROOT="${ANDROID_ROOT:-}" - -# Termux on Android has ${PREFIX} set which already ends with '/usr' -if [[ -n "${ANDROID_ROOT}" && -n "${PREFIX}" ]]; then - INSTALL_PREFIX="${PREFIX}/bin" -else - INSTALL_PREFIX="/usr/local/bin" -fi - -#------------------------------------------------------------------------------- -# FUNCTIONS -#------------------------------------------------------------------------------- - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_banner -# DESCRIPTION: Prints a banner -# PARAMETERS: none -# RETURNS: 0 -#------------------------------------------------------------------------------- -print_banner() { - cat <<-'EOF' -================================================= - ____ - / ___|_ __ ___ ___ - | | | '__/ _ \ / __| - | |___| | | (_) | (__ - \____|_| \___/ \___| - - ___ _ _ _ - |_ _|_ __ ___| |_ __ _| | | ___ _ __ - | || '_ \/ __| __/ _` | | |/ _ \ '__| - | || | | \__ \ || (_| | | | __/ | - |___|_| |_|___/\__\__,_|_|_|\___|_| -================================================== -EOF -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_help -# DESCRIPTION: Prints out a help message -# PARAMETERS: none -# RETURNS: 0 -#------------------------------------------------------------------------------- -print_help() { - local help_header - local help_message - - help_header="croc Installer Script" - help_message="Usage: - -p INSTALL_PREFIX - Prefix to install croc into. Directory must already exist. - Default = /usr/local/bin ('\${PREFIX}/bin' on Termux for Android) - - -h - Prints this helpfull message and exit." - - echo "${help_header}" - echo "" - echo "${help_message}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: print_message -# DESCRIPTION: Prints a message all fancy like -# PARAMETERS: $1 = Message to print -# $2 = Severity. info, ok, error, warn -# RETURNS: Formatted Message to stdout -#------------------------------------------------------------------------------- -print_message() { - local message - local severity - local red - local green - local yellow - local nc - - message="${1}" - severity="${2}" - red='\e[0;31m' - green='\e[0;32m' - yellow='\e[1;33m' - nc='\e[0m' - - case "${severity}" in - "info" ) echo -e "${nc}${message}${nc}";; - "ok" ) echo -e "${green}${message}${nc}";; - "error" ) echo -e "${red}${message}${nc}";; - "warn" ) echo -e "${yellow}${message}${nc}";; - esac - - -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: make_tempdir -# DESCRIPTION: Makes a temp dir using mktemp if available -# PARAMETERS: $1 = Directory template -# RETURNS: 0 = Created temp dir. Also prints temp file path to stdout -# 1 = Failed to create temp dir -# 20 = Failed to find mktemp -#------------------------------------------------------------------------------- -make_tempdir() { - local template - local tempdir - local tempdir_rcode - - template="${1}.XXXXXX" - - if command -v mktemp >/dev/null 2>&1; then - tempdir="$(mktemp -d -t "${template}")" - tempdir_rcode="${?}" - if [[ "${tempdir_rcode}" == "0" ]]; then - echo "${tempdir}" - return 0 - else - return 1 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_os -# DESCRIPTION: Attempts to determin host os using uname -# PARAMETERS: none -# RETURNS: 0 = OS Detected. Also prints detected os to stdout -# 1 = Unkown OS -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_os() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: determine_arch -# DESCRIPTION: Attempt to determin architecture of host -# PARAMETERS: none -# RETURNS: 0 = Arch Detected. Also prints detected arch to stdout -# 1 = Unkown arch -# 20 = 'uname' not found in path -#------------------------------------------------------------------------------- -determine_arch() { - local uname_out - - if command -v uname >/dev/null 2>&1; then - uname_out="$(uname -m)" - if [[ "${uname_out}" == "" ]]; then - return 1 - else - echo "${uname_out}" - return 0 - fi - else - return 20 - fi -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: download_file -# DESCRIPTION: Downloads a file into the specified directory. Attempts to -# use curl, then wget. If neither is found, fail. -# PARAMETERS: $1 = url of file to download -# $2 = location to download file into on host system -# RETURNS: If curl or wget found, returns the return code of curl or wget -# 20 = Could not find curl and wget -#------------------------------------------------------------------------------- -download_file() { - local url - local dir - local filename - local rcode - - url="${1}" - dir="${2}" - filename="${3}" - - if command -v curl >/dev/null 2>&1; then - curl -fsSL "${url}" -o "${dir}/${filename}" - rcode="${?}" - elif command -v wget >/dev/null 2>&1; then - wget --quiet "${url}" -O "${dir}/${filename}" - rcode="${?}" - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: checksum_check -# DESCRIPTION: Attempt to verify checksum of downloaded file to ensure -# integrity. Tries multiple tools before faling. -# PARAMETERS: $1 = path to checksum file -# $2 = location of file to check -# $3 = working directory -# RETURNS: 0 = checkusm verified -# 1 = checksum verification failed -# 20 = failed to determine tool to use to check checksum -# 30 = failed to change into or go back from working dir -#------------------------------------------------------------------------------- -checksum_check() { - local checksum_file - local file - local dir - local rcode - local shasum_1 - local shasum_2 - local shasum_c - - checksum_file="${1}" - file="${2}" - dir="${3}" - - cd "${dir}" || return 30 - if command -v sha256sum >/dev/null 2>&1; then - ## Not all sha256sum versions seem to have --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_c="$(sha256sum -c "filtered_checksum.txt")" - rcode="${?}" - elif command -v shasum >/dev/null 2>&1; then - ## With shasum on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_c="$(shasum -a 256 -c "filtered_checksum.txt")" - rcode="${?}" - elif command -v sha256 >/dev/null 2>&1; then - ## With sha256 on FreeBSD, we don't get to --ignore-missing, so filter the checksum file - ## to only include the file we downloaded. - ## Also sha256 -c option seems to fail, so fall back to an if statement - grep "$(basename "${file}")" "${checksum_file}" > filtered_checksum.txt - shasum_1="$(sha256 -q "${file}")" - shasum_2="$(awk '{print $1}' filtered_checksum.txt)" - if [[ "${shasum_1}" == "${shasum_2}" ]]; then - rcode="0" - else - rcode="1" - fi - shasum_c="Expected: ${shasum_1}, Got: ${shasum_2}" - else - return 20 - fi - cd - >/dev/null 2>&1 || return 30 - - if [[ "${rcode}" -gt "0" ]]; then - echo "${shasum_c}" - fi - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: extract_file -# DESCRIPTION: Extracts a file into a location. Attempts to determine which -# tool to use by checking file extention. -# PARAMETERS: $1 = file to extract -# $2 = location to extract file into -# $3 = extention -# RETURNS: Return code of the tool used to extract the file -# 20 = Failed to determine which tool to use -# 30 = Failed to find tool in path -#------------------------------------------------------------------------------- -extract_file() { - local file - local dir - local ext - local rcode - - file="${1}" - dir="${2}" - ext="${3}" - - case "${ext}" in - "zip" ) if command -v unzip >/dev/null 2>&1; then - unzip "${file}" -d "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - "tar.gz" ) if command -v tar >/dev/null 2>&1; then - tar -xf "${file}" -C "${dir}" - rcode="${?}" - else - rcode="30" - fi - ;; - * ) rcode="20";; - esac - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_freebsd -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_freebsd() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -B '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_linux -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo (unless on android). -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 1 = File not installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_linux() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -C -b -S '_old' -m 755 "${file}" "${prefix}" - rcode="${?}" - elif [[ "${ANDROID_ROOT}" != "" ]]; then - install -C -b -S '_old' -m 755 -t "${prefix}" "${file}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: install_file_cygwin -# DESCRIPTION: Installs a file into a location using 'install'. If EUID not -# 0, then attempt to use sudo. -# Not really 100% sure this is how to install croc in cygwin. -# PARAMETERS: $1 = file to install -# $2 = location to install file into -# RETURNS: 0 = File Installed -# 20 = Could not find install command -# 21 = Could not find sudo command -#------------------------------------------------------------------------------- -install_file_cygwin() { - local file - local prefix - local rcode - - file="${1}" - prefix="${2}" - - if command -v install >/dev/null 2>&1; then - if [[ "${EUID}" == "0" ]]; then - install -m 755 "${prefix}" "${file}" - rcode="${?}" - else - if command -v sudo >/dev/null 2>&1; then - sudo install -m 755 "${file}" "${prefix}" - rcode="${?}" - else - rcode="21" - fi - fi - else - rcode="20" - fi - - return "${rcode}" -} - -#--- FUNCTION ---------------------------------------------------------------- -# NAME: main -# DESCRIPTION: Put it all together in a logical way -# ...at least that is the hope... -# PARAMETERS: 1 = prefix -# RETURNS: 0 = All good -# 1 = Something done broke -#------------------------------------------------------------------------------- -main() { - local prefix - local tmpdir - local tmpdir_rcode - local croc_arch - local croc_arch_rcode - local croc_os - local croc_os_rcode - local croc_base_url - local croc_url - local croc_file - local croc_checksum_file - local croc_bin_name - local croc_version - local croc_dl_ext - local download_file_rcode - local download_checksum_file_rcode - local checksum_check_rcode - local extract_file_rcode - local install_file_rcode - - croc_bin_name="croc" - croc_version="6.1.3" - croc_dl_ext="tar.gz" - croc_base_url="https://github.com/schollz/croc/releases/download" - prefix="${1}" - - print_banner - print_message "== Install prefix set to ${prefix}" "info" - - tmpdir="$(make_tempdir "${croc_bin_name}")" - tmpdir_rcode="${?}" - if [[ "${tmpdir_rcode}" == "0" ]]; then - print_message "== Created temp dir at ${tmpdir}" "info" - elif [[ "${tmpdir_rcode}" == "1" ]]; then - print_message "== Failed to create temp dir at ${tmpdir}" "error" - else - print_message "== 'mktemp' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_arch="$(determine_arch)" - croc_arch_rcode="${?}" - if [[ "${croc_arch_rcode}" == "0" ]]; then - print_message "== Architecture detected as ${croc_arch}" "info" - elif [[ "${croc_arch_rcode}" == "1" ]]; then - print_message "== Architecture not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - croc_os="$(determine_os)" - croc_os_rcode="${?}" - if [[ "${croc_os_rcode}" == "0" ]]; then - print_message "== OS detected as ${croc_os}" "info" - elif [[ "${croc_os_rcode}" == "1" ]]; then - print_message "== OS not detected" "error" - exit 1 - else - print_message "== 'uname' not found in path. Is it installed?" "error" - exit 1 - fi - - case "${croc_os}" in - "Darwin" ) croc_os="macOS";; - "CYGWIN"* ) croc_os="Windows"; - croc_dl_ext="zip"; - print_message "== Cygwin is currently unsupported." "error"; - exit 1;; - esac - - case "${croc_arch}" in - "x86_64" ) croc_arch="64bit";; - "amd64" ) croc_arch="64bit";; - "aarch64" ) croc_arch="ARM64";; - "armv7l" ) croc_arch="ARM";; - "i686" ) croc_arch="32bit";; - * ) croc_arch="unknown";; - esac - - croc_file="${croc_bin_name}_${croc_version}_${croc_os}-${croc_arch}.${croc_dl_ext}" - croc_checksum_file="${croc_bin_name}_${croc_version}_checksums.txt" - croc_url="${croc_base_url}/v${croc_version}/${croc_file}" - croc_checksum_url="${croc_base_url}/v${croc_version}/${croc_checksum_file}" - - download_file "${croc_url}" "${tmpdir}" "${croc_file}" - download_file_rcode="${?}" - if [[ "${download_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc archive into ${tmpdir}" "info" - elif [[ "${download_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc archive" "error" - exit 1 - elif [[ "${download_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_file_rcode}" "error" - exit 1 - fi - download_file "${croc_checksum_url}" "${tmpdir}" "${croc_checksum_file}" - download_checksum_file_rcode="${?}" - if [[ "${download_checksum_file_rcode}" == "0" ]]; then - print_message "== Downloaded croc checksums file into ${tmpdir}" "info" - elif [[ "${download_checksum_file_rcode}" == "1" ]]; then - print_message "== Failed to download croc checksums" "error" - exit 1 - elif [[ "${download_checksum_file_rcode}" == "20" ]]; then - print_message "== Failed to locate curl or wget" "error" - exit 1 - else - print_message "== Return code of download tool returned an unexpected value of ${download_checksum_file_rcode}" "error" - exit 1 - fi - - checksum_check "${tmpdir}/${croc_checksum_file}" "${tmpdir}/${croc_file}" "${tmpdir}" - checksum_check_rcode="${?}" - if [[ "${checksum_check_rcode}" == "0" ]]; then - print_message "== Checksum of ${tmpdir}/${croc_file} verified" "ok" - elif [[ "${checksum_check_rcode}" == "1" ]]; then - print_message "== Failed to verify checksum of ${tmpdir}/${croc_file}" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "20" ]]; then - print_message "== Failed to find tool to verify sha256 sums" "error" - exit 1 - elif [[ "${checksum_check_rcode}" == "30" ]]; then - print_message "== Failed to change into working directory ${tmpdir}" "error" - exit 1 - else - print_message "== Unknown return code returned while checking checksum of ${tmpdir}/${croc_file}. Returned ${checksum_check_rcode}" "error" - exit 1 - fi - - extract_file "${tmpdir}/${croc_file}" "${tmpdir}/" "${croc_dl_ext}" - extract_file_rcode="${?}" - if [[ "${extract_file_rcode}" == "0" ]]; then - print_message "== Extracted ${croc_file} to ${tmpdir}/" "info" - elif [[ "${extract_file_rcode}" == "1" ]]; then - print_message "== Failed to extract ${croc_file}" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "20" ]]; then - print_message "== Failed to determine which extraction tool to use" "error" - exit 1 - elif [[ "${extract_file_rcode}" == "30" ]]; then - print_message "== Failed to find extraction tool in path" "error" - exit 1 - else - print_message "== Unknown error returned from extraction attempt" "error" - exit 1 - fi - - case "${croc_os}" in - "Linux" ) install_file_linux "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "FreeBSD" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "macOS" ) install_file_freebsd "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - "Windows" ) install_file_cygwin "${tmpdir}/${croc_bin_name}" "${prefix}/"; - install_file_rcode="${?}";; - esac - if [[ "${install_file_rcode}" == "0" ]]; then - print_message "== Installed ${croc_bin_name} to ${prefix}/" "ok" - elif [[ "${install_file_rcode}" == "1" ]]; then - print_message "== Failed to install ${croc_bin_name}" "error" - exit 1 - elif [[ "${install_file_rcode}" == "20" ]]; then - print_message "== Failed to locate 'install' command" "error" - exit 1 - elif [[ "${install_file_rcode}" == "21" ]]; then - print_message "== Failed to locate 'sudo' command" "error" - exit 1 - else - print_message "== Install attempt returned an unexpected value of ${install_file_rcode}" "error" - exit 1 - fi - - print_message "== Installation complete" "ok" - - exit 0 -} - -#------------------------------------------------------------------------------- -# ARGUMENT PARSING -#------------------------------------------------------------------------------- -OPTS="hp:" -while getopts "${OPTS}" optchar; do - case "${optchar}" in - 'h' ) print_help - exit 0 - ;; - 'p' ) INSTALL_PREFIX="${OPTARG}" - ;; - /? ) print_message "Unknown option ${OPTARG}" "warn" - ;; - esac -done - -#------------------------------------------------------------------------------- -# CALL MAIN -#------------------------------------------------------------------------------- -main "${INSTALL_PREFIX}"