1 | #!/bin/sh
|
2 | #
|
3 | # POSIX shell script to install oils-for-unix into the proper directory.
|
4 | # Distributed with the source tarball.
|
5 | #
|
6 | # For usage, run:
|
7 | #
|
8 | # ./install --help
|
9 | #
|
10 | # Also shared with the old "oil.ovm" build.
|
11 |
|
12 | # NOTE: 'install' is part of coreutils and busybox.
|
13 |
|
14 | # old tarball
|
15 | readonly OVM_NAME=oil.ovm
|
16 | readonly OVM_PATH=_bin/$OVM_NAME
|
17 |
|
18 | log() {
|
19 | # 4 space indent
|
20 | echo " $@" >& 2
|
21 | }
|
22 |
|
23 | die() {
|
24 | echo "FATAL install error: $@" >& 2
|
25 | exit 1
|
26 | }
|
27 |
|
28 | my_install() {
|
29 | ### A bit like install -v. OpenBSD doesn't have -v
|
30 |
|
31 | echo " + install $@" >& 2
|
32 | install "$@"
|
33 | }
|
34 |
|
35 | install_bin_and_links() {
|
36 | ### Install an executable and symlinks.
|
37 |
|
38 | bin_src=$1
|
39 | bin_new_name=$2
|
40 | shift 2
|
41 |
|
42 | # symlinks are the remaining args
|
43 |
|
44 | # NOTE: The configure step generates this
|
45 | if ! . _build/detected-config.sh; then
|
46 | die "Can't find _build/detected-config.sh. Run './configure'"
|
47 | fi
|
48 | # Now $PREFIX should be defined
|
49 |
|
50 | #
|
51 | # Install the shell binary
|
52 | #
|
53 |
|
54 | bin_dest_dir="${DESTDIR}${PREFIX}/bin"
|
55 | bin_dest="$bin_dest_dir/$bin_new_name"
|
56 |
|
57 | if ! my_install -d "$bin_dest_dir"; then
|
58 | die "Couldn't create $bin_dest_dir"
|
59 | fi
|
60 |
|
61 | if ! my_install "$bin_src" "$bin_dest"; then
|
62 | die "Couldn't install $bin_src -> $bin_dest"
|
63 | fi
|
64 | log "Installed $bin_dest"
|
65 |
|
66 | working_dir=$PWD # save for later
|
67 |
|
68 | cd "$bin_dest_dir"
|
69 | for link in "$@"; do
|
70 | if ! ln -s -f "$bin_new_name" "$link"; then # -f to overwrite
|
71 | die "Couldn't create $link symlink"
|
72 | fi
|
73 | log "Created '$link' symlink"
|
74 | done
|
75 |
|
76 | #
|
77 | # Install man page
|
78 | #
|
79 |
|
80 | # Relevant:
|
81 | # https://unix.stackexchange.com/questions/90759/where-should-i-install-manual-pages-in-user-directory
|
82 | # https://www.freebsd.org/cgi/man.cgi?query=install
|
83 |
|
84 | cd "$working_dir"
|
85 |
|
86 | # e.g. /usr/local/share/man/man1
|
87 | man_dest_dir="${DESTDIR}${DATAROOTDIR}/man/man1"
|
88 |
|
89 | if ! my_install -d "$man_dest_dir"; then
|
90 | die "Couldn't create $man_dest_dir"
|
91 | fi
|
92 |
|
93 | # -m so it's not executable
|
94 | if ! my_install -m 644 doc/osh.1 "$man_dest_dir"; then
|
95 | die "Couldn't install man page"
|
96 | fi
|
97 | log "Installed man page"
|
98 | }
|
99 |
|
100 | show_help() {
|
101 | cat <<'EOF'
|
102 | Install the oils-for-unix binary, and symlinks to it, like osh.
|
103 |
|
104 | Usage:
|
105 |
|
106 | ./install # install the stripped binary
|
107 | ./install --from _bin/cxx-opt-sh/oils-for-unix # or a given binary
|
108 | ./install --help # show this help
|
109 |
|
110 | DESTDIR=/tmp/foo ./install
|
111 |
|
112 | The DESTDIR var allows staged installs. This means that the installed files
|
113 | are placed in a temp dir first, NOT the dir they are run from on the target
|
114 | machine.
|
115 |
|
116 | https://www.gnu.org/prep/standards/html_node/DESTDIR.html
|
117 |
|
118 | Package managers such as gentoo-portage used staged installs by default.
|
119 |
|
120 | https://devmanual.gentoo.org/quickstart/index.html
|
121 |
|
122 | EOF
|
123 | }
|
124 |
|
125 | # by default, install the stripped binary
|
126 | FLAG_from=_bin/cxx-opt-sh/oils-for-unix.stripped
|
127 |
|
128 | parse_flags() {
|
129 | while true; do
|
130 | case "$1" in
|
131 | '')
|
132 | break
|
133 | ;;
|
134 | --help)
|
135 | show_help
|
136 | exit 0
|
137 | ;;
|
138 | --from)
|
139 | if test $# -eq 1; then
|
140 | die "--from requires an argument"
|
141 | fi
|
142 | shift
|
143 | FLAG_from=$1
|
144 | ;;
|
145 | *)
|
146 | die "Invalid argument '$1'"
|
147 | ;;
|
148 | esac
|
149 | shift
|
150 | done
|
151 | }
|
152 |
|
153 | main() {
|
154 | parse_flags "$@" # sets FLAG_*, or prints help
|
155 |
|
156 | if test -f "$OVM_PATH"; then
|
157 | # Python tarball keeps 'oil' for compatibility
|
158 | install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh oil
|
159 |
|
160 | elif test -f "$FLAG_from"; then
|
161 | # 'osh' and 'ysh' point at 'oils-for-unix'
|
162 | install_bin_and_links "$FLAG_from" 'oils-for-unix' osh ysh
|
163 |
|
164 | else
|
165 | die "Couldn't find $OVM_PATH or $FLAG_from"
|
166 | fi
|
167 | }
|
168 |
|
169 | main "$@"
|