diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..7fa9d8b --- /dev/null +++ b/install.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash + +[ "$(whoami)" != "root" ] && echo "Error: installation only works as root!" >&2 && exit 1 + +API_ENDPOINT="https://git.ccc-rheintal.ch/api/v1/repos/spacefreak/pbc" + +TMPDIR='' +function cleanup_tmpdir() { + [ -n "$TMPDIR" -a -d "$TMPDIR" ] && rm -r "$TMPDIR" + trap - EXIT +} + +function download_files() { + local path + local query_params='' + [ -n "$GIT_TAG" ] && query_params="?ref=$GIT_TAG" + + while [ -n "$1" ]; do + path=$1 + shift + curl -s --output-dir "$TMPDIR/" --remote-name "$API_ENDPOINT/raw/$path$query_params" + rc=$? + if (( $rc != 0 )); then + return $rc + fi + done +} + +GIT_TAG=$(curl -s "$API_ENDPOINT/tags" | jq -r '.[] | .name' | sort --version-sort | tail -n 1) +if [ -n "$GIT_TAG" ]; then + echo "Info: installing pbc version $GIT_TAG" +else + echo "Warning: no Git tag found, installing from repository default branch" >&2 +fi + +TMPDIR=$(mktemp -d) +trap cleanup_tmpdir EXIT + +! download_files config.example pbc && echo "Error downloading files!" >&2 && exit 10 +chmod +x "$TMPDIR/pbc" + +"$TMPDIR/pbc" install +rc=$? +if (( $? != 0 )); then + echo "Error installing pbc requirements!" >&2 + exit $rc +fi + +! mv "$TMPDIR/pbc" /usr/local/bin/pbc && echo 'Error moving pbc to /usr/local/bin/pbc !' >&2 && exit 20 + +echo "Success!"