| 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 | # Also shared with the old "oil.ovm" build.
 | 
| 7 | 
 | 
| 8 | # NOTE: 'install' is part of coreutils and busybox.
 | 
| 9 | 
 | 
| 10 | # The variable DESTDIR allows staged installs, where the installed files are
 | 
| 11 | # not placed directly into the location they're expected to be executed from.
 | 
| 12 | # They are placed in a temp dir first, which they are NOT expected to run out of.
 | 
| 13 | #
 | 
| 14 | # https://www.gnu.org/prep/standards/html_node/DESTDIR.html
 | 
| 15 | #
 | 
| 16 | # Staged installs are the default method of installation by package managers
 | 
| 17 | # such as gentoo-portage.
 | 
| 18 | #
 | 
| 19 | # https://devmanual.gentoo.org/quickstart/index.html
 | 
| 20 | 
 | 
| 21 | # old tarball
 | 
| 22 | 
 | 
| 23 | readonly OVM_NAME=oil.ovm
 | 
| 24 | readonly OVM_PATH=_bin/$OVM_NAME
 | 
| 25 | 
 | 
| 26 | readonly OILS_PATH=_bin/cxx-opt-sh/oils-for-unix.stripped
 | 
| 27 | 
 | 
| 28 | 
 | 
| 29 | log() {
 | 
| 30 |   # 4 space indent
 | 
| 31 |   echo "    $@" >& 2
 | 
| 32 | }
 | 
| 33 | 
 | 
| 34 | die() {
 | 
| 35 |   echo "FATAL install error: $@" >& 2
 | 
| 36 |   exit 1
 | 
| 37 | }
 | 
| 38 | 
 | 
| 39 | my_install() {
 | 
| 40 |   ### A bit like install -v.  OpenBSD doesn't have -v
 | 
| 41 | 
 | 
| 42 |   echo "  + install $@" >& 2
 | 
| 43 |   install "$@"
 | 
| 44 | }
 | 
| 45 | 
 | 
| 46 | install_bin_and_links() {
 | 
| 47 |   ### Install an executable and symlinks.
 | 
| 48 | 
 | 
| 49 |   bin_src=$1
 | 
| 50 |   bin_new_name=$2
 | 
| 51 |   shift 2
 | 
| 52 | 
 | 
| 53 |   # symlinks are the remaining args
 | 
| 54 | 
 | 
| 55 |   # NOTE: The configure step generates this
 | 
| 56 |   if ! . _build/detected-config.sh; then
 | 
| 57 |     die "Can't find _build/detected-config.sh.  Run './configure'"
 | 
| 58 |   fi
 | 
| 59 |   # Now $PREFIX should be defined
 | 
| 60 | 
 | 
| 61 |   #
 | 
| 62 |   # Install the shell binary
 | 
| 63 |   #
 | 
| 64 | 
 | 
| 65 |   bin_dest_dir="${DESTDIR}${PREFIX}/bin"
 | 
| 66 |   bin_dest="$bin_dest_dir/$bin_new_name"
 | 
| 67 | 
 | 
| 68 |   if ! my_install -d "$bin_dest_dir"; then
 | 
| 69 |     die "Couldn't create $bin_dest_dir"
 | 
| 70 |   fi
 | 
| 71 | 
 | 
| 72 |   if ! my_install "$bin_src" "$bin_dest"; then
 | 
| 73 |     die "Couldn't install $bin_src -> $bin_dest"
 | 
| 74 |   fi
 | 
| 75 |   log "Installed $bin_dest"
 | 
| 76 | 
 | 
| 77 |   working_dir=$PWD  # save for later
 | 
| 78 | 
 | 
| 79 |   cd "$bin_dest_dir"
 | 
| 80 |   for link in "$@"; do
 | 
| 81 |     if ! ln -s -f "$bin_new_name" "$link"; then  # -f to overwrite
 | 
| 82 |       die "Couldn't create $link symlink"
 | 
| 83 |     fi
 | 
| 84 |     log "Created '$link' symlink"
 | 
| 85 |   done
 | 
| 86 | 
 | 
| 87 |   #
 | 
| 88 |   # Install man page
 | 
| 89 |   #
 | 
| 90 | 
 | 
| 91 |   # Relevant:
 | 
| 92 |   # https://unix.stackexchange.com/questions/90759/where-should-i-install-manual-pages-in-user-directory
 | 
| 93 |   # https://www.freebsd.org/cgi/man.cgi?query=install
 | 
| 94 | 
 | 
| 95 |   cd "$working_dir"
 | 
| 96 | 
 | 
| 97 |   # e.g. /usr/local/share/man/man1
 | 
| 98 |   man_dest_dir="${DESTDIR}${DATAROOTDIR}/man/man1"
 | 
| 99 | 
 | 
| 100 |   if ! my_install -d "$man_dest_dir"; then
 | 
| 101 |     die "Couldn't create $man_dest_dir"
 | 
| 102 |   fi
 | 
| 103 | 
 | 
| 104 |   # -m so it's not executable
 | 
| 105 |   if ! my_install -m 644 doc/osh.1 "$man_dest_dir"; then
 | 
| 106 |     die "Couldn't install man page"
 | 
| 107 |   fi
 | 
| 108 |   log "Installed man page"
 | 
| 109 | }
 | 
| 110 | 
 | 
| 111 | if test -f "$OVM_PATH"; then
 | 
| 112 |   # Python tarball keeps 'oil' for compatibility
 | 
| 113 |   install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh oil
 | 
| 114 | elif test -f "$OILS_PATH"; then
 | 
| 115 |   # new name is 'ysh', which points at oils-for-unix
 | 
| 116 |   install_bin_and_links "$OILS_PATH" 'oils-for-unix' osh ysh
 | 
| 117 | else
 | 
| 118 |   die "Couldn't find $OVM_PATH or $OILS_PATH"
 | 
| 119 | fi
 | 
| 120 | 
 |