xref: /freebsd/share/mk/stage-install.sh (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1#!/bin/sh
2
3# NAME:
4#	stage-install.sh - wrapper around install
5#
6# SYNOPSIS:
7#	stage-install.sh [variable="value"] "args" "dest"
8#
9# DESCRIPTION:
10#	This script is a wrapper around the normal install(1).
11#	Its role is to add '.dirdep' files to the destination.
12#	The variables we might use are:
13#
14#	INSTALL
15#		Path to actual install(1), default is
16#		$REAL_INSTALL
17#
18#	OBJDIR
19#		Path to the dir where '.dirdep' was generated,
20#		default is '.'
21#
22#	_DIRDEP
23#		Path to actual '.dirdep' file, default is
24#		$OBJDIR/.dirdep
25#
26#	The "args" and "dest" are passed as is to install(1), and if a
27#	'.dirdep' file exists it will be linked or copied to each
28#	"file".dirdep placed in "dest" or "dest".dirdep if it happed
29#	to be a file rather than a directory.
30#
31# SEE ALSO:
32#	meta.stage.mk
33#
34
35# RCSid:
36#	$Id: stage-install.sh,v 1.5 2013/04/19 16:32:24 sjg Exp $
37#
38#	@(#) Copyright (c) 2013, Simon J. Gerraty
39#
40#	This file is provided in the hope that it will
41#	be of use.  There is absolutely NO WARRANTY.
42#	Permission to copy, redistribute or otherwise
43#	use this file is hereby granted provided that
44#	the above copyright notice and this notice are
45#	left intact.
46#
47#	Please send copies of changes and bug-fixes to:
48#	sjg@crufty.net
49#
50
51INSTALL=${REAL_INSTALL:-install}
52OBJDIR=.
53
54while :
55do
56    case "$1" in
57    *=*) eval "$1"; shift;;
58    *) break;;
59    esac
60done
61
62# if .dirdep doesn't exist, just run install and be done
63_DIRDEP=${_DIRDEP:-$OBJDIR/.dirdep}
64[ -s $_DIRDEP ] && EXEC= || EXEC=exec
65$EXEC $INSTALL "$@" || exit 1
66
67# from meta.stage.mk
68LnCp() {
69    rm -f $2 2> /dev/null
70    ln $1 $2 2> /dev/null || cp -p $1 $2
71}
72
73StageDirdep() {
74  t=$1
75  if [ -s $t.dirdep ]; then
76      cmp -s $_DIRDEP $t.dirdep && return
77      echo "ERROR: $t installed by `cat $t.dirdep` not `cat $_DIRDEP`" >&2
78      exit 1
79  fi
80  LnCp $_DIRDEP $t.dirdep || exit 1
81}
82
83args="$@"
84while [ $# -gt 8 ]
85do
86    shift 8
87done
88eval dest=\$$#
89if [ -f $dest ]; then
90    # a file, there can be only one .dirdep needed
91    StageDirdep $dest
92elif [ -d $dest ]; then
93    for f in $args
94    do
95        test -f $f || continue
96        StageDirdep $dest/${f##*/}
97    done
98fi
99