xref: /dragonfly/contrib/bmake/mk/stage-install.sh (revision 631c21f2)
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#	Before we run install(1), we check if "dest" needs to be a
32#	directory (more than one file in "args") and create it
33#	if necessary.
34#
35# SEE ALSO:
36#	meta.stage.mk
37#
38
39# RCSid:
40#	$Id: stage-install.sh,v 1.9 2020/08/28 01:04:13 sjg Exp $
41#
42#	@(#) Copyright (c) 2013-2020, Simon J. Gerraty
43#
44#	This file is provided in the hope that it will
45#	be of use.  There is absolutely NO WARRANTY.
46#	Permission to copy, redistribute or otherwise
47#	use this file is hereby granted provided that
48#	the above copyright notice and this notice are
49#	left intact.
50#
51#	Please send copies of changes and bug-fixes to:
52#	sjg@crufty.net
53#
54
55INSTALL=${REAL_INSTALL:-install}
56OBJDIR=.
57
58while :
59do
60    case "$1" in
61    *=*) eval "$1"; shift;;
62    *) break;;
63    esac
64done
65
66# get last entry from "$@" without side effects
67last_entry() {
68    while [ $# -gt 8 ]
69    do
70        shift 8
71    done
72    eval last=\$$#
73    echo $last
74}
75
76# mkdir $dest if needed (more than one file)
77mkdir_if_needed() {
78    (
79        lf=
80        while [ $# -gt 8 ]
81        do
82            shift 4
83        done
84        for f in "$@"
85        do
86            [ -f $f ] || continue
87            [ $f = $dest ] && continue
88            if [ -n "$lf" ]; then
89                # dest must be a directory
90                mkdir -p $dest
91                break
92            fi
93            lf=$f
94        done
95    )
96}
97
98args="$@"
99dest=`last_entry "$@"`
100case " $args " in
101*" -d "*) ;;
102*) [ -e $dest ] || mkdir_if_needed "$@";;
103esac
104
105# if .dirdep doesn't exist, just run install and be done
106_DIRDEP=${_DIRDEP:-$OBJDIR/.dirdep}
107[ -s $_DIRDEP ] && EXEC= || EXEC=exec
108$EXEC $INSTALL "$@" || exit 1
109
110# from meta.stage.mk
111LnCp() {
112    rm -f $2 2> /dev/null
113    ln $1 $2 2> /dev/null || cp -p $1 $2
114}
115
116StageDirdep() {
117  t=$1
118  if [ -s $t.dirdep ]; then
119      cmp -s $_DIRDEP $t.dirdep && return
120      echo "ERROR: $t installed by `cat $t.dirdep` not `cat $_DIRDEP`" >&2
121      exit 1
122  fi
123  LnCp $_DIRDEP $t.dirdep || exit 1
124}
125
126if [ -f $dest ]; then
127    # a file, there can be only one .dirdep needed
128    StageDirdep $dest
129elif [ -d $dest ]; then
130    for f in $args
131    do
132        test -f $f || continue
133        StageDirdep $dest/${f##*/}
134    done
135fi
136