| 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 "oils-ref.ovm" build.
|
| 11 |
|
| 12 | # NOTE: 'install' is part of coreutils and busybox.
|
| 13 |
|
| 14 | # old tarball
|
| 15 | readonly OVM_NAME=oils-ref.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 | ./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 | Env vars respected:
|
| 110 |
|
| 111 | DESTDIR=/tmp/foo ./install
|
| 112 |
|
| 113 | The DESTDIR var allows staged installs. This means that the installed files
|
| 114 | are placed in a temp dir first, NOT the dir they are run from on the target
|
| 115 | machine.
|
| 116 |
|
| 117 | https://www.gnu.org/prep/standards/html_node/DESTDIR.html
|
| 118 |
|
| 119 | Package managers such as gentoo-portage used staged installs by default.
|
| 120 |
|
| 121 | https://devmanual.gentoo.org/quickstart/index.html
|
| 122 |
|
| 123 | EOF
|
| 124 | }
|
| 125 |
|
| 126 |
|
| 127 | FLAG_verbose=
|
| 128 |
|
| 129 | ARG_oils_binary=
|
| 130 |
|
| 131 | parse_flags() {
|
| 132 | while true; do
|
| 133 | case "$1" in
|
| 134 | -h|--help)
|
| 135 | show_help
|
| 136 | exit 0
|
| 137 | ;;
|
| 138 | -v|--verbose)
|
| 139 | FLAG_verbose=true
|
| 140 | ;;
|
| 141 | -*)
|
| 142 | die "Invalid flag '$1'"
|
| 143 | ;;
|
| 144 | *)
|
| 145 | # No more flags
|
| 146 | break
|
| 147 | ;;
|
| 148 | esac
|
| 149 | shift
|
| 150 | done
|
| 151 |
|
| 152 | # by default, install the stripped binary
|
| 153 | ARG_oils_binary=${1:-_bin/cxx-opt-sh/oils-for-unix.stripped}
|
| 154 | }
|
| 155 |
|
| 156 | main() {
|
| 157 | parse_flags "$@" # sets FLAG_*, or prints help
|
| 158 |
|
| 159 | if test -n "$FLAG_verbose"; then
|
| 160 | log "Installing Oils binary $ARG_oils_binary"
|
| 161 | fi
|
| 162 |
|
| 163 | if test -f "$OVM_PATH"; then
|
| 164 | install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh
|
| 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 "$@"
|