#!/bin/sh

# install - install a program, script, or datafile
# This comes from X11R5 (mit/util/scripts/install.sh)
# and was modified by Aleksey Cheusov <vle@gmx.net>.
#
# Copyright 1991 by the Massachusetts Institute of Technology
# Copyright 2013-2019 by Aleksey Cheusov <vle@gmx.net>
#
# Permission to use, copy, modify, distribute, and sell this software and its
# documentation for any purpose is hereby granted without fee, provided that
# the above copyright notice appear in all copies and that both that
# copyright notice and this permission notice appear in supporting
# documentation, and that the name of M.I.T. not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission.  M.I.T. makes no representations about the
# suitability of this software for any purpose.  It is provided "as is"
# without express or implied warranty.
#
# Calling this script install-sh is preferred over install.sh, to prevent
# `make' implicit rules from creating a file called install from it
# when there is no Makefile.
#
# This script is compatible with the BSD install script, but was written
# from scratch.  It can only install one file at a time, a restriction
# shared with many OS's install programs.

# set DOITPROG to echo to test this script
doit="${DOITPROG-}"

# put in absolute paths if you don't have them in your path; or use env. vars.

: ${STRIP:=strip}
mvprog="${MVPROG-mv}"
cpprog="${CPPROG-cp}"
lnprog="${LNSPROG-ln -f}"
lnsprog="${LNSPROG-ln -f -s}"
chmodprog="${CHMODPROG-chmod}"
chownprog="${CHOWNPROG-chown}"
chgrpprog="${CHGRPPROG-chgrp}"
stripprog="${STRIPPROG-$STRIP}"
rmprog="${RMPROG-rm}"
mkdirprog="${MKDIRPROG-mkdir}"

instcmd="$cpprog"
chmodcmd=mkcchmod; mod=0755;
chowncmd=":"
chgrpcmd=":"
stripcmd=":"
rmcmd="$rmprog -f"
mvcmd="$mvprog"
src=""
dst=""
dir_arg=""

set -e

mkcchgrp () { $chgrpprog "$grp" "$@"; }
mkcchown () { $chownprog "$own" "$@"; }
mkcchmod () { $chmodprog "$mod" "$@"; }

last_arg() {
    while test $# -gt 1; do
	shift
    done

    printf '%s\n' "$1"
}

while getopts cdg:l:m:o:s f; do
    case "$f" in
        c)
	    true;;
	d)
	    instcmd="$mkdirprog -p"
	    dir_arg=true;;
	l)
	    case "$OPTARG" in
		s)
		    instcmd="$lnsprog";;
		h)
		    instcmd="$lnprog";;
		*)
		    echo "Incorrect linking type '$OPTARG'" 1>&2
		    exit 1;;
	    esac;;
	m)
	    mod="$OPTARG"
	    chmodcmd=mkcchmod;;
	o)
	    own="$OPTARG"
	    chowncmd=mkcchown;;
	g)
	    grp="$OPTARG"
	    chgrpcmd=mkcchgrp;;
	s)
	    stripcmd="$stripprog";;
        *)
	    exit 2;;
    esac
done
shift `expr $OPTIND - 1`

if test "$instcmd" = "$lnsprog" -o "$instcmd" = "$lnprog"; then
    chmodcmd=":"
    chowncmd=":"
    chgrpcmd=":"
    stripcmd=":"
fi

#
copy_file () {
    dsttmp="$dstdir/#inst.$$#"
    trap "rm -f ${dsttmp}" 0

    $doit $instcmd "$src" "$dsttmp"
    $doit $chowncmd "$dsttmp"
    $doit $chgrpcmd "$dsttmp"
    $doit $stripcmd "$dsttmp"
    $doit $chmodcmd "$dsttmp"

    $doit $rmcmd -f "$dst"
    $doit $mvcmd "$dsttmp" "$dst"
}

if test -n "$dir_arg"; then
    # -d dir1 dir2 ... dirN
    for f in "$@"; do
	if ! test -d "$f"; then
	    $doit $instcmd "$f"
	    $doit $chowncmd "$f"
	    $doit $chgrpcmd "$f"
	    $doit $chmodcmd "$f"
	fi
    done
elif test $# -eq 2; then
    # src_file trg_file
    if test -d "$2"; then
	dst="$2/"`basename "$1"`
	dstdir="$2"
    else
	dst="$2"
	dstdir=`dirname "$2"`
    fi

    src="$1"
    copy_file
elif test $# -gt 2; then
    # src_file1 src_file2 ... src_fileN trg_dir
    dstdir=`last_arg "$@"`

    if test -d "$dstdir"; then
	:
    else
	echo "Last argument '$dstdir' is not a directory" 1>&2
	exit 1
    fi

    while test $# -gt 1; do
	src="$1"
	dst="$dstdir"/`basename $1`
	copy_file
	shift
    done
else
    # bad usage
    echo "Missing target file or directory" 1>&2
    exit 1
fi
