xref: /freebsd/tools/tools/nanobsd/fill_pkg.sh (revision 53b70c86)
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# $FreeBSD$
29#
30# Usage:
31# 	$0 PACKAGE_DUMP NANO_PACKAGE_DIR /usr/ports/foo/bar [package.txz]...
32#
33# Will symlink the packages listed, including their runtime dependencies,
34# from the PACKAGE_DUMP to the NANO_PACKAGE_DIR.
35#
36
37: ${PORTSDIR:=/usr/ports}
38
39usage () {
40	echo "Usage: $0 [-v] package-dump-dir nano-package-dir port-dir-or-pkg ..." 1>&2
41	exit 2
42}
43
44msg () {
45	local l
46	l=$1 ; shift
47	[ "$l" -le "$VERBOSE" ] && echo $*
48}
49
50ports_recurse() (
51	local outputfile dumpdir type fullpath pkgname p
52	outputfile=$1 ; shift
53	dumpdir=$1    ; shift
54	for p do
55		if [ -d "$p" -a -f "$p/Makefile" ] ; then
56			msg 3 "$p: full path to port"
57			pkgname=`cd "$p" && make -V pkgname`
58			type=port
59			fullpath=$p
60		elif [ -d "${PORTSDIR}/$p" -a -f "${PORTSDIR}/$p/Makefile" ] ; then
61			msg 3 "$p: path to port relative to ${PORTSDIR}}"
62			pkgname=`cd "${PORTSDIR}/$p" && make -V pkgname`
63			type=port
64			fullpath=${PORTSDIR}/$p
65		elif [ "${p%.txz}" != "$p" -a -f "$p" ] && pkg info -F "$p" > /dev/null 2>&1 ; then
66			msg 3 "$p: full package file name"
67			pkgname=`basename "$p" | sed 's/\.txz$//I'`
68			type=pkg
69			fullpath=$p
70		elif [ "${p%.txz}" != "$p" -a -f "$dumpdir/$p" ] && pkg info -F "$dumpdir/$p" > /dev/null 2>&1 ; then
71			msg 3 "$p: package file name relative to $dumpdir"
72			pkgname=`basename "$p" | sed 's/\.txz$//I'`
73			type=pkg
74			fullpath=$dumpdir/$p
75		elif [ -f "$dumpdir/$p.txz" ] && pkg info -F "$dumpdir/$p.txz" > /dev/null 2>&1 ; then
76			msg 3 "$p: package name relative to $dumpdir"
77			pkgname=`basename "$p"`
78			type=pkg
79			fullpath=$dumpdir/$p.txz
80		else
81			echo "Missing port or package $p" 1>&2
82			exit 2
83		fi
84		if grep -q "^$pkgname\$" "$outputfile" ; then
85			msg 3 "$pkgname was added already"
86			true
87		elif [ "$type" = "port" ] ; then
88			(
89				cd "$fullpath"
90				rd=`make -V RUN_DEPENDS ${PORTS_OPTS}`
91				ld=`make -V LIB_DEPENDS ${PORTS_OPTS}`
92
93				for dep in $rd $ld ; do
94					arg=`echo $dep | sed 's/^[^:]*:\([^:]*\).*$/\1/'`
95					msg 2 "Check $arg as requirement for port $pkgname"
96					ports_recurse "$outputfile" "$dumpdir" "$arg"
97				done
98			)
99			msg 1 "Add $pkgname"
100			echo "$pkgname" >> "$outputfile"
101		else
102			dir=`dirname "$p"` # Get directory from SPECIFIED path, not from full path
103			if [ "$dir" = "." ] ; then
104			  dir=""
105			else
106			  dir=${dir}/
107			fi
108			deps=`pkg info -dF "$fullpath" | grep -v "$pkgname:"`
109			for dep in $deps ; do
110				arg=`echo $dep | sed -e "s|^|$dir|" -e 's/$/.txz/'`
111				msg 2 "Check $arg as requirement for package $pkgname"
112				ports_recurse "$outputfile" "$dumpdir" "$arg"
113			done
114			msg 1 "Add $pkgname"
115			echo "$pkgname" >> "$outputfile"
116		fi
117	done
118)
119
120VERBOSE=0
121
122while getopts v opt ; do
123	case "$opt" in
124	  v) VERBOSE=$(($VERBOSE + 1)) ;;
125	[?]) usage                     ;;
126	esac
127done
128shift $(( ${OPTIND} - 1 ))
129
130if [ "$#" -lt 3 ] ; then
131	usage
132fi
133
134NANO_PKG_DUMP=`realpath $1`
135shift;
136if [ ! -d "$NANO_PKG_DUMP" ] ; then
137	echo "$NANO_PKG_DUMP is not a directory" 1>&2
138	usage
139fi
140
141NANO_PKG_DIR=`realpath $1`
142shift;
143if [ ! -d "$NANO_PKG_DIR" ] ; then
144	echo "$NANO_PKG_DIR is not a directory" 1>&2
145	usage
146fi
147
148# Cleanup
149rm -rf "$NANO_PKG_DIR/"*
150
151PL=$NANO_PKG_DIR/_list
152true > "$PL"
153
154for p do
155	ports_recurse "$PL" "$NANO_PKG_DUMP" "$p"
156done
157
158for i in `cat "$PL"` ; do
159	if [ -f "$NANO_PKG_DUMP/$i.txz" ] ; then
160		ln -s "$NANO_PKG_DUMP/$i.txz" "$NANO_PKG_DIR"
161	else
162		echo "Package $i misssing in $NANO_PKG_DUMP" 1>&2
163		exit 1
164	fi
165done
166
167rm -f "$PL"
168exit 0
169