aboutsummaryrefslogtreecommitdiff
path: root/libexec/recit-init
blob: a5df7d1559db79a9022caecb998b08c38e9c5a8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
set -e

print=""
if [ "$1" = "-" ]; then
  print=1
  shift
fi

shell="$1"
if [ -z "$shell" ]; then
  shell="$(basename "$SHELL")"
fi

resolve_link() {
  $(type -p greadlink readlink | head -1) $1
}

abs_dirname() {
  local cwd="$(pwd)"
  local path="$1"

  while [ -n "$path" ]; do
    cd "${path%/*}"
    local name="${path##*/}"
    path="$(resolve_link "$name" || true)"
  done

  pwd
  cd "$cwd"
}

root="$(abs_dirname "$0")/.."

if [ -z "$print" ]; then
  case "$shell" in
  bash )
    profile='~/.bash_profile'
    ;;
  zsh )
    profile='~/.zshenv'
    ;;
  * )
    profile='your profile'
    ;;
  esac

  { echo "# Load recit automatically by adding"
    echo "# the following to ${profile}:"
    echo
    echo "eval \"\$(${_RECIT_ROOT}/bin/recit init -)\""
    echo
  } >&2

  exit 1
fi

echo "export PATH=\"\${PATH}:${_RECIT_ROOT}/bin\""

case "$shell" in
bash | zsh )
  echo "source \"$root/completions/recit.${shell}\""
  ;;
esac

commands=(`recit commands --sh`)
IFS="|"
cat <<EOS
_recit_wrapper() {
  local command="\$1"
  if [ "\$#" -gt 0 ]; then
    shift
  fi

  case "\$command" in
  ${commands[*]})
    eval \`recit "sh-\$command" "\$@"\`;;
  *)
    command recit "\$command" "\$@";;
  esac
}
EOS

# zsh can't pass argument with aliases, but bash can.
# zsh can have functions with the name being only numbers, but bash can't.
# fix both cases here by letting zsh have a function, and bash have its alias.
case "$shell" in
bash )
  echo "alias recit=_recit_wrapper"
  ;;
zsh )
  echo "recit=_recit_wrapper"
  ;;
esac