1#!/usr/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source. A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11
12# Copyright 2021 OmniOS Community Edition (OmniOSce) Association.
13
14set -o errexit -o pipefail
15trap 'echo Error occured at line $LINENO' ERR
16
17if [[ ! -v CODEMGR_WS ]] || (($# < 1)); then
18	cat <<- EOM
19This script should be run within a bldenv by issuing 'make update' in
20\$CODEMGR_WS/usr/src/data/zoneinfo
21	EOM
22	exit 1
23fi
24
25MANIFEST=$CODEMGR_WS/usr/src/pkg/manifests/system-data-zoneinfo.mf
26PREFIX=usr/share/lib/zoneinfo
27
28if [[ ! -f "$MANIFEST" ]]; then
29	echo "Could not find $MANIFEST"
30	exit 1
31fi
32
33typeset -A links
34typeset -A targets
35for f in "$@"; do
36	if [[ ! -r "$f" ]]; then
37		echo "Could not read $f"
38		exit 1
39	fi
40	echo "+++ Processing input file $f"
41	grep '^Link' "$f" | tr -s '[:space:]' | \
42	    while IFS=$' \t' read _ tgt src _; do
43
44		osrc=$src
45		targets[$tgt]=1
46
47		printf "    %20s => %s\n" $src $tgt
48
49		while [[ $src == */* && ${src%%/*} == ${tgt%%/*} ]]; do
50			src=${src#*/}
51			tgt=${tgt#*/}
52		done
53
54		# On no matches, grep -o exits non-zero, hence the || true to
55		# satisfy the shell's errexit option.
56		sslashes=$(echo $src | grep -o / | wc -l || true)
57		r=
58		while ((sslashes-- > 0)); do
59			r+="../"
60		done
61		links[$osrc]="$r$tgt"
62	done
63done
64
65tmpf1=`mktemp`
66tmpf2=`mktemp`
67trap 'rm -f $tmpf1 $tmpf2' EXIT
68[[ -n "$tmpf1" && -f "$tmpf1" ]]
69[[ -n "$tmpf2" && -f "$tmpf2" ]]
70
71cp $MANIFEST $tmpf1
72pkgfmt -u $tmpf1
73
74echo "+++ Removing existing hardlinks from manifest"
75egrep -v "^hardlink " $tmpf1 > $tmpf2
76mv $tmpf2 $tmpf1
77
78echo "+++ Removing existing targets from manifest"
79for i in "${!links[@]}" "${!targets[@]}"; do
80	egrep -v "^file path=$PREFIX/$i\$" $tmpf1 > $tmpf2
81	mv $tmpf2 $tmpf1
82done
83
84echo "+++ Adding new entries to manifest"
85{
86	for i in "${!targets[@]}"; do
87		echo "file path=$PREFIX/$i"
88	done
89	for i in "${!links[@]}"; do
90		echo "hardlink path=$PREFIX/$i target=${links[$i]}"
91	done
92} >> $tmpf1
93
94echo "+++ Formatting manifest"
95pkgfmt -fv2 $tmpf1
96
97mv $tmpf1 $MANIFEST
98
99