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 _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 |
|
126 | FLAG_verbose=
|
127 |
|
128 | ARG_oils_binary=
|
129 |
|
130 | parse_flags() {
|
131 | while true; do
|
132 | case "$1" in
|
133 | --help)
|
134 | show_help
|
135 | exit 0
|
136 | ;;
|
137 | -v|--verbose)
|
138 | FLAG_verbose=true
|
139 | ;;
|
140 | -*)
|
141 | die "Invalid flag '$1'"
|
142 | ;;
|
143 | *)
|
144 | # No more flags
|
145 | break
|
146 | ;;
|
147 | esac
|
148 | shift
|
149 | done
|
150 |
|
151 | # by default, install the stripped binary
|
152 | ARG_oils_binary=${1:-_bin/cxx-opt-sh/oils-for-unix.stripped}
|
153 | }
|
154 |
|
155 | main() {
|
156 | parse_flags "$@" # sets FLAG_*, or prints help
|
157 |
|
158 | if test -n "$FLAG_verbose"; then
|
159 | log "Installing Oils binary $ARG_oils_binary"
|
160 | fi
|
161 |
|
162 | if test -f "$OVM_PATH"; then
|
163 | # Python tarball keeps 'oil' for compatibility
|
164 | install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh oil
|
165 |
|
166 | elif test -f "$ARG_oils_binary"; then
|
167 | # 'osh' and 'ysh' point at 'oils-for-unix'
|
168 | install_bin_and_links "$ARG_oils_binary" 'oils-for-unix' osh ysh
|
169 |
|
170 | else
|
171 | die "Couldn't find $OVM_PATH or $ARG_oils_binary"
|
172 | fi
|
173 | }
|
174 |
|
175 | main "$@"
|