xref: /src/tools/tools/nanobsd/fill_pkg.sh (revision 12cbad923dbcccb3d6f71a77ad3241508f186048)
1#!/bin/sh
2#
3# Copyright (c) 2014 Lev Serebryakov.
4# Copyright (c) 2009 Poul-Henning Kamp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28#
29# Usage:
30# 	$0 [-cv] PACKAGE_DUMP NANO_PACKAGE_DIR /usr/ports/foo/bar [package.pkg]...
31#
32# Will symlink/copy the packages listed, including their runtime dependencies,
33# from the PACKAGE_DUMP to the NANO_PACKAGE_DIR.
34#
35
36: ${PORTSDIR:=/usr/ports}
37
38usage () {
39	echo "Usage: $0 [-cv] package-dump-dir nano-package-dir port-dir-or-pkg ..." 1>&2
40	exit 2
41}
42
43msg () {
44	local l
45	l=$1 ; shift
46	[ "$l" -le "$VERBOSE" ] && echo $*
47}
48
49ports_recurse() (
50	local outputfile dumpdir type fullpath pkgname p
51	outputfile=$1 ; shift
52	dumpdir=$1    ; shift
53	for p do
54		if [ -d "$p" -a -f "$p/Makefile" ] ; then
55			msg 3 "$p: full path to port"
56			pkgname=`cd "$p" && make package-name`
57			type=port
58			fullpath=$p
59		elif [ -d "${PORTSDIR}/$p" -a -f "${PORTSDIR}/$p/Makefile" ] ; then
60			msg 3 "$p: path to port relative to ${PORTSDIR}}"
61			pkgname=`cd "${PORTSDIR}/$p" && make package-name`
62			type=port
63			fullpath=${PORTSDIR}/$p
64		elif [ "${p%.pkg}" != "$p" -a -f "$p" ] && pkg info -F "$p" > /dev/null 2>&1 ; then
65			msg 3 "$p: full package file name"
66			pkgname=`basename "$p" | sed 's/\.pkg$//I'`
67			type=pkg
68			fullpath=$p
69		elif [ "${p%.pkg}" != "$p" -a -f "$dumpdir/$p" ] && pkg info -F "$dumpdir/$p" > /dev/null 2>&1 ; then
70			msg 3 "$p: package file name relative to $dumpdir"
71			pkgname=`basename "$p" | sed 's/\.pkg$//I'`
72			type=pkg
73			fullpath=$dumpdir/$p
74		elif [ -f "$dumpdir/$p.pkg" ] && pkg info -F "$dumpdir/$p.pkg" > /dev/null 2>&1 ; then
75			msg 3 "$p: package name relative to $dumpdir"
76			pkgname=`basename "$p"`
77			type=pkg
78			fullpath=$dumpdir/$p.pkg
79		else
80			echo "Missing port or package $p" 1>&2
81			exit 2
82		fi
83		if grep -q "^$pkgname\$" "$outputfile" ; then
84			msg 3 "$pkgname was added already"
85			true
86		elif [ "$type" = "port" ] ; then
87			(
88				cd "$fullpath"
89				rd=`make -V RUN_DEPENDS ${PORTS_OPTS}`
90				ld=`make -V LIB_DEPENDS ${PORTS_OPTS}`
91
92				for dep in $rd $ld ; do
93					arg=`echo $dep | sed 's/^[^:]*:\([^:]*\).*$/\1/'`
94					msg 2 "Check $arg as requirement for port $pkgname"
95					ports_recurse "$outputfile" "$dumpdir" "$arg"
96				done
97			)
98			msg 1 "Add $pkgname"
99			echo "$pkgname" >> "$outputfile"
100		else
101			dir=`dirname "$p"` # Get directory from SPECIFIED path, not from full path
102			if [ "$dir" = "." ] ; then
103			  dir=""
104			else
105			  dir=${dir}/
106			fi
107			deps=`pkg info -dF "$fullpath" | grep -v "$pkgname:"`
108			for dep in $deps ; do
109				arg=`echo $dep | sed -e "s|^|$dir|" -e 's/$/.pkg/'`
110				msg 2 "Check $arg as requirement for package $pkgname"
111				ports_recurse "$outputfile" "$dumpdir" "$arg"
112			done
113			msg 1 "Add $pkgname"
114			echo "$pkgname" >> "$outputfile"
115		fi
116	done
117)
118
119COPY="ln -s"
120VERBOSE=0
121
122while getopts cv opt ; do
123	case "$opt" in
124	  c) COPY="cp -p"              ;;
125	  v) VERBOSE=$(($VERBOSE + 1)) ;;
126	[?]) usage                     ;;
127	esac
128done
129shift $(( ${OPTIND} - 1 ))
130
131if [ "$#" -lt 3 ] ; then
132	usage
133fi
134
135NANO_PKG_DUMP=`realpath $1`
136shift;
137if [ ! -d "$NANO_PKG_DUMP" ] ; then
138	echo "$NANO_PKG_DUMP is not a directory" 1>&2
139	usage
140fi
141
142NANO_PKG_DIR=`realpath $1`
143shift;
144if [ ! -d "$NANO_PKG_DIR" ] ; then
145	echo "$NANO_PKG_DIR is not a directory" 1>&2
146	usage
147fi
148
149# Cleanup
150rm -rf "$NANO_PKG_DIR/"*
151
152PL=$NANO_PKG_DIR/_list
153true > "$PL"
154
155for p do
156	ports_recurse "$PL" "$NANO_PKG_DUMP" "$p"
157done
158
159for i in `cat "$PL"` ; do
160	if [ -f "$NANO_PKG_DUMP/$i.pkg" ] ; then
161		$COPY "$NANO_PKG_DUMP/$i.pkg" "$NANO_PKG_DIR"
162	else
163		echo "Package $i missing in $NANO_PKG_DUMP" 1>&2
164		exit 1
165	fi
166done
167
168rm -f "$PL"
169exit 0
170