#!/bin/sh 
#
# POSIX shell script to install oils-for-unix into the proper directory.
# Distributed with the source tarball.
#
# For usage, run:
#
#   ./install --help 
#
# Also shared with the old "oils-ref.ovm" build.

# NOTE: 'install' is part of coreutils and busybox.

# old tarball
readonly OVM_NAME=oils-ref.ovm
readonly OVM_PATH=_bin/$OVM_NAME

log() {
  # 4 space indent
  echo "    $@" >& 2
}

die() {
  echo "FATAL install error: $@" >& 2
  exit 1
}

my_install() {
  ### A bit like install -v.  OpenBSD doesn't have -v

  echo "  + install $@" >& 2
  install "$@"
}

install_bin_and_links() {
  ### Install an executable and symlinks.

  bin_src=$1
  bin_new_name=$2
  shift 2

  # symlinks are the remaining args

  # NOTE: The configure step generates this
  if ! . _build/detected-config.sh; then
    die "Can't find _build/detected-config.sh.  Run './configure'"
  fi
  # Now $PREFIX should be defined

  #
  # Install the shell binary
  #

  bin_dest_dir="${DESTDIR}${PREFIX}/bin"
  bin_dest="$bin_dest_dir/$bin_new_name"

  if ! my_install -d "$bin_dest_dir"; then
    die "Couldn't create $bin_dest_dir"
  fi

  if ! my_install "$bin_src" "$bin_dest"; then
    die "Couldn't install $bin_src -> $bin_dest"
  fi
  log "Installed $bin_dest"

  working_dir=$PWD  # save for later

  cd "$bin_dest_dir"
  for link in "$@"; do
    if ! ln -s -f "$bin_new_name" "$link"; then  # -f to overwrite
      die "Couldn't create $link symlink"
    fi
    log "Created '$link' symlink"
  done

  #
  # Install man page
  #

  # Relevant:
  # https://unix.stackexchange.com/questions/90759/where-should-i-install-manual-pages-in-user-directory
  # https://www.freebsd.org/cgi/man.cgi?query=install

  cd "$working_dir"

  # e.g. /usr/local/share/man/man1
  man_dest_dir="${DESTDIR}${DATAROOTDIR}/man/man1"

  if ! my_install -d "$man_dest_dir"; then
    die "Couldn't create $man_dest_dir"
  fi

  # -m so it's not executable
  if ! my_install -m 644 doc/osh.1 "$man_dest_dir"; then
    die "Couldn't install man page"
  fi
  log "Installed man page"
}

show_help() {
  cat <<'EOF'
Install the oils-for-unix binary, and symlinks to it, like osh.

Usage:
  ./install                                # install the stripped binary
  ./install _bin/cxx-opt-sh/oils-for-unix  # or a given binary
  ./install --help                         # show this help

Env vars respected:

  DESTDIR=/tmp/foo ./install

The DESTDIR var allows staged installs.  This means that the installed files
are placed in a temp dir first, NOT the dir they are run from on the target
machine.

    https://www.gnu.org/prep/standards/html_node/DESTDIR.html

Package managers such as gentoo-portage used staged installs by default.

    https://devmanual.gentoo.org/quickstart/index.html

EOF
}


FLAG_verbose=

ARG_oils_binary=

parse_flags() {
  while true; do
    case "$1" in
      -h|--help)
        show_help
        exit 0
        ;;
      -v|--verbose)
        FLAG_verbose=true
        ;;
      -*)
        die "Invalid flag '$1'"
        ;;
      *)
        # No more flags
        break
        ;;
    esac
    shift
  done

  # by default, install the stripped binary
  ARG_oils_binary=${1:-_bin/cxx-opt-sh/oils-for-unix.stripped}
}

main() {
  parse_flags "$@"  # sets FLAG_*, or prints help

  if test -n "$FLAG_verbose"; then
    log "Installing Oils binary $ARG_oils_binary"
  fi

  if test -f "$OVM_PATH"; then
    install_bin_and_links "$OVM_PATH" "$OVM_NAME" osh ysh

  elif test -f "$ARG_oils_binary"; then
    # 'osh' and 'ysh' point at 'oils-for-unix'
    install_bin_and_links "$ARG_oils_binary" 'oils-for-unix' osh ysh

  else
    die "Couldn't find $OVM_PATH or $ARG_oils_binary"
  fi
}

main "$@"
