1#!/usr/bin/env bash
2#
3# A good old bash | curl script for direnv.
4#
5set -euo pipefail
6
7{ # Prevent execution if this script was only partially downloaded
8
9  log() {
10    echo "[installer] $*" >&2
11  }
12
13  die() {
14    log "$@"
15    exit 1
16  }
17
18  at_exit() {
19    ret=$?
20    if [[ $ret -gt 0 ]]; then
21      log "the script failed with error $ret.\n" \
22        "\n" \
23        "To report installation errors, submit an issue to\n" \
24        "    https://github.com/direnv/direnv/issues/new/choose"
25    fi
26    exit "$ret"
27  }
28  trap at_exit EXIT
29
30  kernel=$(uname -s | tr "[:upper:]" "[:lower:]")
31  case "$(uname -m)" in
32    x86_64)
33      machine=amd64
34      ;;
35    i686 | i386)
36      machine=386
37      ;;
38    *)
39      die "Machine $(uname -m) not supported by the installer.\n" \
40        "Go to https://direnv for alternate installation methods."
41      ;;
42  esac
43  log "kernel=$kernel machine=$machine"
44
45  : "${use_sudo:=}"
46  : "${bin_path:=}"
47
48  if [[ -z "$bin_path" ]]; then
49    log "looking for a writeable PATH"
50    for path in $(echo "$PATH" | tr ':' '\n'); do
51      if [[ -w $path ]]; then
52        bin_path=$path
53        break
54      fi
55    done
56  fi
57  if [[ -z "$bin_path" ]]; then
58    die "did not find a writeable path in $PATH"
59  fi
60  echo "bin_path=$bin_path"
61
62  log "looking for a download URL"
63  download_url=$(
64    curl -fL https://api.github.com/repos/direnv/direnv/releases/latest \
65    | grep browser_download_url \
66    | cut -d '"' -f 4 \
67    | grep "direnv.$kernel.$machine"
68  )
69  echo "download_url=$download_url"
70
71  log "downloading"
72  curl -o "$bin_path/direnv" -fL "$download_url"
73  chmod +x "$bin_path/direnv"
74
75  cat <<DONE
76
77The direnv binary is now avaible in:
78
79    $bin_path/direnv
80
81The last step is to configure your shell to use it. For example for bash, add
82the following lines at the end of your ~/.bashrc:
83
84    eval "\$(direnv hook bash)"
85
86Then restart the shell.
87
88For other shells, see https://direnv.net/docs/hook.html
89
90Thanks!
91DONE
92}
93