diff --git a/install.sh b/install.sh index 715f156..0253da3 100755 --- a/install.sh +++ b/install.sh @@ -6,70 +6,90 @@ # default installation directory INSTALL_DIR_DEFAULT="/usr/local/bin" +# URI of "kiwi" launcher script +KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi" +############# +# FUNCTIONS # +############# -############ -# CLI ARGS # -############ +# prompt yes/no question (default yes) +yes_no() { + # prompt and read from terminal + printf "%s [Y|n] " "${1}" + read -r answer /dev/stderr + exit 1 +} ######## # MAIN # ######## -# prompt for installation directory +# check if already installed +install_kiwi="$(command -v kiwi)" + +if [ -x "${install_kiwi}" ]; then + # kiwi is installed: Choose that directory + install_dir="$(dirname "${install_kiwi}")" + + if ! yes_no "kiwi-config found in '${install_dir}'. Overwrite?"; then + die "Uninstall existing '${install_kiwi}' first" + fi + +elif [ ${#} -gt 0 ]; then + # install dir candidate given as CLI argument + install_dir="${1}" + shift 1 +fi + +# check dir given by argument while [ ! -d "${install_dir}" ]; do + # prompt user for installation directory printf "Select installation directory [Default: '%s']: " "${INSTALL_DIR_DEFAULT}" read -r install_dir /dev/stderr - exit 1 + # fail if install dir can't be created + if yes_no "Install directory doesn't exist. Try creating?"; then + if ! mkdir -p "${install_dir}" >/dev/null 2>/dev/null; then + die "Couldn't create install directory" fi fi fi done -if [ ! -d "${install_dir}" ]; then - echo "wtf?" - exit 1 -fi - # start actual installation printf "Installing into '%s' ... " "${install_dir}" - -# install "kiwi" script -uri="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi" tmp_file="$(mktemp)" -if ! curl --proto '=https' --tlsv1.2 -sSf -o "${tmp_file}" "${uri}" >/dev/null 2>/dev/null; then +if ! curl --proto '=https' --tlsv1.2 -sSf -o "${tmp_file}" "${KIWI_URI}" >/dev/null 2>/dev/null; then rm "${tmp_file}" - echo "Download 'kiwi' failed!" >/dev/stderr - exit 1 + die "Downloading 'kiwi' failed" fi if ! install -m 0755 "${tmp_file}" "${install_dir}/kiwi" >/dev/null 2>/dev/null; then rm "${tmp_file}" - echo "Install 'kiwi' failed!" >/dev/stderr - exit 1 + die "Installing 'kiwi' failed" fi -rm "${tmp_file}" - # finalization +rm "${tmp_file}" echo "OK" exit 0 diff --git a/kiwi b/kiwi index dd34869..cfaf18b 100755 --- a/kiwi +++ b/kiwi @@ -21,11 +21,23 @@ KIWI_REPO="https://github.com/ldericher/kiwi-config" # use latest version by default KIWI_VERSION="master" +# URI of "kiwi" launcher script +KIWI_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/kiwi" +INSTALLER_URI="https://raw.githubusercontent.com/ldericher/kiwi-config/master/install.sh" +# canary file: limit curl requests +CANARY_FILENAME="/var/lock/kiwi-config.canary" +CANARY_MAXAGE=600 + ################### # DYNAMIC STRINGS # ################### +# uri of correct kiwi-config archive +kiwi_archive_uri() { + echo "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz" +} + # directory of correct installation kiwi_installdir() { echo "${KIWI_BASEDIR}/${KIWI_VERSION}" @@ -45,6 +57,28 @@ kiwi_executable() { WORKDIR="$(pwd)" +############# +# FUNCTIONS # +############# + +# prompt yes/no question (default yes) +yes_no() { + # prompt and read from terminal + printf "%s [Y|n] " "${1}" + read -r answer /dev/stderr; then + # should reinstall, so download installer + installer="$(curl --proto '=https' --tlsv1.2 -sSfL "${INSTALLER_URI}")" + + if yes_no "Use sudo to run as root?"; then + # enable system-wide install + echo "${installer}" | sudo sh + + else + # per-user install + echo "${installer}" | sh + fi + + else + echo "" >/dev/stderr + echo "####################" >/dev/stderr + echo "Please manually update your kiwi launcher by re-running the installation process:" >/dev/stderr + echo "https://github.com/ldericher/kiwi-config/#installation" >/dev/stderr + echo "####################" >/dev/stderr + echo "" >/dev/stderr + + fi + fi + + # refresh canary + touch "${CANARY_FILENAME}" +fi + # check if pwd is a kiwi folder if [ -f "./${KIWI_CONF_NAME}" ]; then # determine needed kiwi-config version @@ -88,7 +177,7 @@ if [ ! -x "$(kiwi_executable)" ]; then cd "${tmpdir}" || : # download archive - curl --proto '=https' --tlsv1.2 -sSfL "${KIWI_REPO}/archive/${KIWI_VERSION}.tar.gz" | tar xz + curl --proto '=https' --tlsv1.2 -sSfL "$(kiwi_archive_uri)" | tar xz # read archive version tag cd "kiwi-config-${KIWI_VERSION}" || :