OILS / install View on Github | oilshell.org

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