OILS / install View on Github | oilshell.org

174 lines, 87 significant
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
15readonly OVM_NAME=oil.ovm
16readonly OVM_PATH=_bin/$OVM_NAME
17
18log() {
19 # 4 space indent
20 echo " $@" >& 2
21}
22
23die() {
24 echo "FATAL install error: $@" >& 2
25 exit 1
26}
27
28my_install() {
29 ### A bit like install -v. OpenBSD doesn't have -v
30
31 echo " + install $@" >& 2
32 install "$@"
33}
34
35install_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
100show_help() {
101 cat <<'EOF'
102Install the oils-for-unix binary, and symlinks to it, like osh.
103
104Usage:
105 ./install # install the stripped binary
106 ./install _bin/cxx-opt-sh/oils-for-unix # or a given binary
107 ./install --help # show this help
108
109 DESTDIR=/tmp/foo ./install
110
111The DESTDIR var allows staged installs. This means that the installed files
112are placed in a temp dir first, NOT the dir they are run from on the target
113machine.
114
115 https://www.gnu.org/prep/standards/html_node/DESTDIR.html
116
117Package managers such as gentoo-portage used staged installs by default.
118
119 https://devmanual.gentoo.org/quickstart/index.html
120
121EOF
122}
123
124
125FLAG_verbose=
126
127ARG_oils_binary=
128
129parse_flags() {
130 while true; do
131 case "$1" in
132 -h|--help)
133 show_help
134 exit 0
135 ;;
136 -v|--verbose)
137 FLAG_verbose=true
138 ;;
139 -*)
140 die "Invalid flag '$1'"
141 ;;
142 *)
143 # No more flags
144 break
145 ;;
146 esac
147 shift
148 done
149
150 # by default, install the stripped binary
151 ARG_oils_binary=${1:-_bin/cxx-opt-sh/oils-for-unix.stripped}
152}
153
154main() {
155 parse_flags "$@" # sets FLAG_*, or prints help
156
157 if test -n "$FLAG_verbose"; then
158 log "Installing Oils binary $ARG_oils_binary"
159 fi
160
161 if test -f "$OVM_PATH"; then
162 # Python tarball keeps 'oil' for compatibility
163 install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh oil
164
165 elif test -f "$ARG_oils_binary"; then
166 # 'osh' and 'ysh' point at 'oils-for-unix'
167 install_bin_and_links "$ARG_oils_binary" 'oils-for-unix' osh ysh
168
169 else
170 die "Couldn't find $OVM_PATH or $ARG_oils_binary"
171 fi
172}
173
174main "$@"