xref: /src/share/mk/stage-install.sh (revision 1876de606eb876b7a97beccfe6fcc89e60a72a25)
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.12 2025/08/09 22:42:24 sjg Exp $
41#
42#	SPDX-License-Identifier: BSD-2-Clause
43#
44#	@(#) Copyright (c) 2013-2020, Simon J. Gerraty
45#
46#	SPDX-License-Identifier: BSD-2-Clause
47#
48#	Please send copies of changes and bug-fixes to:
49#	sjg@crufty.net
50#
51
52INSTALL=${REAL_INSTALL:-install}
53OBJDIR=.
54
55while :
56do
57    case "$1" in
58    *=*) eval "$1"; shift;;
59    *) break;;
60    esac
61done
62
63# get last entry from "$@" without side effects
64last_entry() {
65    while [ $# -gt 8 ]
66    do
67        shift 8
68    done
69    eval last=\$$#
70    echo $last
71}
72
73# mkdir $dest if needed (more than one file)
74mkdir_if_needed() {
75    (
76        lf=
77        while [ $# -gt 8 ]
78        do
79            shift 4
80        done
81        for f in "$@"
82        do
83            [ -f $f ] || continue
84            [ $f = $dest ] && continue
85            if [ -n "$lf" ]; then
86                # dest must be a directory
87                mkdir -p $dest
88                break
89            fi
90            lf=$f
91        done
92    )
93}
94
95args="$@"
96dest=`last_entry "$@"`
97case " $args " in
98*" -d "*) ;;
99*) [ -e $dest ] || mkdir_if_needed "$@";;
100esac
101
102# if .dirdep doesn't exist, just run install and be done
103_DIRDEP=${_DIRDEP:-$OBJDIR/.dirdep}
104[ -s $_DIRDEP ] && EXEC= || EXEC=exec
105$EXEC $INSTALL "$@" || exit 1
106
107# from meta.stage.mk
108LnCp() {
109    rm -f $2 2> /dev/null
110    ln $1 $2 2> /dev/null || cp -p $1 $2
111}
112
113StageDirdep() {
114  t=$1
115  if [ -s $t.dirdep ]; then
116      cmp -s $_DIRDEP $t.dirdep && return
117      case "${STAGE_CONFLICT:-error}" in
118      [Ee]*) STAGE_CONFLICT=ERROR action=exit;;
119      *) STAGE_CONFLICT=WARNING action=: ;;
120      esac
121      echo "$STAGE_CONFLICT: $t installed by `cat $t.dirdep` not `cat $_DIRDEP`" >&2
122      $action 1
123  fi
124  LnCp $_DIRDEP $t.dirdep || exit 1
125}
126
127if [ -f $dest ]; then
128    # a file, there can be only one .dirdep needed
129    StageDirdep $dest
130elif [ -d $dest ]; then
131    for f in $args
132    do
133        test -f $f || continue
134        StageDirdep $dest/${f##*/}
135    done
136fi
137