xref: /freebsd/tools/tools/nanobsd/fill_pkg.sh (revision 069ac184)
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 PACKAGE_DUMP NANO_PACKAGE_DIR /usr/ports/foo/bar [package.txz]...
31#
32# Will symlink 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 [-v] 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 -V pkgname`
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 -V pkgname`
62			type=port
63			fullpath=${PORTSDIR}/$p
64		elif [ "${p%.txz}" != "$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/\.txz$//I'`
67			type=pkg
68			fullpath=$p
69		elif [ "${p%.txz}" != "$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/\.txz$//I'`
72			type=pkg
73			fullpath=$dumpdir/$p
74		elif [ -f "$dumpdir/$p.txz" ] && pkg info -F "$dumpdir/$p.txz" > /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.txz
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/$/.txz/'`
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
119VERBOSE=0
120
121while getopts v opt ; do
122	case "$opt" in
123	  v) VERBOSE=$(($VERBOSE + 1)) ;;
124	[?]) usage                     ;;
125	esac
126done
127shift $(( ${OPTIND} - 1 ))
128
129if [ "$#" -lt 3 ] ; then
130	usage
131fi
132
133NANO_PKG_DUMP=`realpath $1`
134shift;
135if [ ! -d "$NANO_PKG_DUMP" ] ; then
136	echo "$NANO_PKG_DUMP is not a directory" 1>&2
137	usage
138fi
139
140NANO_PKG_DIR=`realpath $1`
141shift;
142if [ ! -d "$NANO_PKG_DIR" ] ; then
143	echo "$NANO_PKG_DIR is not a directory" 1>&2
144	usage
145fi
146
147# Cleanup
148rm -rf "$NANO_PKG_DIR/"*
149
150PL=$NANO_PKG_DIR/_list
151true > "$PL"
152
153for p do
154	ports_recurse "$PL" "$NANO_PKG_DUMP" "$p"
155done
156
157for i in `cat "$PL"` ; do
158	if [ -f "$NANO_PKG_DUMP/$i.txz" ] ; then
159		ln -s "$NANO_PKG_DUMP/$i.txz" "$NANO_PKG_DIR"
160	else
161		echo "Package $i misssing in $NANO_PKG_DUMP" 1>&2
162		exit 1
163	fi
164done
165
166rm -f "$PL"
167exit 0
168