diff --git a/install.sh b/install.sh old mode 100644 new mode 100755 index a469101..32f7603 --- a/install.sh +++ b/install.sh @@ -1,70 +1,141 @@ -#!/bin/sh +#!/usr/bin/env bash +# DragonBuild - install.sh +set -e -nosudo() { - echo "This cannot be ran as root or with sudo." - exit 1 -} +CWD="$(realpath ${PWD} || echo ${PWD})" +SCRIPT="$(realpath ${0} || echo $0 )" +RELEASE="1.0.0" +INSTALL_USER="$@" -crd=$PWD +getroot() { + if sudo -n true &>/dev/null; then + printf -- '%s\n %s\n' "Using cached permissons sudo to continue with install" \ + "CRTL-C to cancel." + sleep 3 + else + echo "Enter sudo password to continue:" + sudo -p \ + "$(printf -- '(%s): %s' "sudo" "Password for installation: ")" true + fi + sudo ${SCRIPT} "${USER}" + exit $? +} +[[ ${UID} -eq 0 || ${EUID} -eq 0 ]] || getroot -[[ $UID == 0 || $EUID == 0 ]] && nosudo +onexit() { + [[ -n "${DRAGON_TEMPDIR+x}" ]] && rm -rf ${DRAGON_TEMPDIR} +}; trap "onexit" INT TERM EXIT -sudo -p "Password for installation: " printf "" || exit 1 +needed=() +toInstall=( + "wget" + "ninja" + "python3" + "ldid" + "perl" + "dpkg" + "unzip" +) -need="" +for depend in ${toInstall[@]}; do + command -v ${depend} &>/dev/null || needed+=(${depend}) +done -command -v wget >/dev/null 2>&1 || need+="wget " -command -v ninja >/dev/null 2>&1 || need+="ninja " -command -v python3 >/dev/null 2>&1 || need+="python3 " -command -v ldid >/dev/null 2>&1 || need+="ldid " -command -v perl >/dev/null 2>&1 || need+="perl " -command -v dpkg >/dev/null 2>&1 || need+="dpkg " -command -v unzip >/dev/null 2>&1 || need+="unzip " +OS="" +case "$(uname)" in + Linux) + : "linux" + ;; + Darwin) + : "darwin" + ;; + *) + : "unknown" + ;; +# wsl) +# : "windows" +esac +OS="$_" macosInstall() { - if [ $need != "" ]; then - read -p "Using Brew To Install Dependencies (${need}). Press Enter to Continue." || exit 1 - brew install $need - fi + [[ -z "${needed[@]}" ]] && return + read -p \ + "$(printf -- "Using HomeBrew to Install:\n%s\n Press Enter to Continue. " "${needed[@]}")" _ + + for depend in ${needed[@]}; do + brew install "${depend}" + done } -linuxInstall() { - if [ $need != "" ]; then - read -p "Installing Dependencies (${need}). Press Enter to Continue." || exit 1 - if [ -x "$(command -v apk)" ]; then sudo apk add --no-cache $need || failedinstall=1 - elif [ -x "$(command -v apt-get)" ]; then sudo apt-get install $need || failedinstall=1 - elif [ -x "$(command -v dnf)" ]; then sudo dnf install $need || failedinstall=1 - elif [ -x "$(command -v zypper)" ]; then sudo zypper install $need || failedinstall=1 - else failedinstall=1; - fi - if [ $failedinstall == 1 ]; then - echo "Installing dependencies failed. You need to manually install: $need">&2; - fi - fi +linuxInstall(){ + [[ -z "${needed[@]}" ]] && return + local cmd + + if command -v apk &>/dev/null; then + cmd="apk add --no-cache" + elif command -v apt-get &>/dev/null; then + cmd="apt-get install" + elif command -v dnf &>/dev/null; then + cmd="dnf install" + elif command -v zypper &>/dev/null; then + cmd="zypper install" + else + cmd="true" + fi + + ${cmd} ${needed[@]} || : + [[ "${cmd}" == "true" || $? -ne 0 ]] && \ + { + printf -- '!!! %s\n%s\n' "Installing dependencies failed. Install manually:" \ + "${needed[@]}"; + # sleep 5; + # exit 1; + } } installDragonBuild() { - distr=$(uname -s) - if [ $distr == "Darwin" ]; then macosInstall - else linuxInstall - fi - echo "Downloading DragonBuild..." - cd /tmp - wget https://github.com/DragonBuild/DragonBuild/releases/download/1.0.0/DragonBuild.zip - echo "Installing DragonBuild" - unzip DragonBuild.zip -d ~/.dragonbuild - mv ~/.dragonbuild/DragonBuild/* ~/.dragonbuild && rm -rf ~/.dragonbuild/DragonBuild-master - rm -rf DragonBuild.zip - cd ~ - echo "source ~/.dragonbuild/internal/environment" >> .zshrc - echo "source ~/.dragonbuild/internal/environment" >> .bash_profile - echo "source ~/.dragonbuild/internal/environment" >> .profile - source ~/.dragonbuild/internal/environment - cd ~/.dragonbuild - git pull - git submodule update --init --recursive - cd ~ - sudo ln -s ~/.dragonbuild/dragon /usr/local/bin/dragon + # WE ARE ROOT, be mindful of our actions + export URL="https://github.com/DragonBuild/DragonBuild/releases/download/${RELEASE}/DragonBuild.zip" + # https://github.com/DragonBuild/DragonBuild/archive/1.0.0.zip + # https://github.com/DragonBuild/DragonBuild/releases/download/1.0.0/DragonBuild.zip + export DRAGON_TEMPDIR="${TMPDIR:-/tmp}/dragonbuild-install" + + case ${OS} in + linux) linuxInstall ;; + darwin) macosInstall ;; + *) linuxInstall ;; + esac + + echo "Installing DragonBuild..." + +# we're root and want to install as the user we where called from + cat << EOF | su ${INSTALL_USER} -c bash + shopt -s extglob nullglob + set -e + mkdir ${DRAGON_TEMPDIR} + cd ${DRAGON_TEMPDIR} + wget "${URL}" + + unzip DragonBuild.zip -d \${HOME}/.dragonbuild + mv \${HOME}/.dragonbuild/DragonBuild/* \${HOME}/.dragonbuild + rm -rf \${HOME}/.dragonbuild/DragonBuild-master + + for profile in zshrc bash_profile profile; do + printf -- '\n%s\n' "source ~/.dragonbuild/internal/environment" \ + >> \${HOME}/.\${profile} + done + + source \${HOME}/.dragonbuild/internal/environment + cd \${HOME}/.dragonbuild + + # git pull + # git submodule update --init --recursive + +EOF + + local _HOME="$(su ${INSTALL_USER} -c echo ${HOME})" + + ln -s ${_HOME}/.dragonbuild/dragon /usr/local/bin/dragon || : } installDragonBuild