1#!/bin/sh
2#
3# Print out the files in some or all lists.
4# Usage: makeplist [options] setname pkgname
5# options:
6#         -a arch             set arch (e.g, m68k, mips, powerpc)
7#         -m machine          set machine (e.g, amiga, i386, macppc)
8#         -s setsdir          directory to find sets
9#         -p prefix prefix for package creation
10#         -I realprefix       prefix for eventual installation
11#         setname pkgname     set and package to build plist for
12#
13
14rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
15. "${rundir}/sets.subr"
16prefix=/
17realprefix=/
18got_realprefix=false
19
20usage() {
21          cat 1>&2 <<USAGE
22Usage: $0 [options] setname pkgname"
23options:"
24          -a arch             set arch (e.g, m68k, mips, powerpc)     [${MACHINE_ARCH}]
25          -m machine          set machine (e.g, amiga, i386, macppc)  [${MACHINE}]
26          -s setsdir          directory to find sets                            [${setsdir}]
27          -p prefix prefix for created plist                [${prefix}]
28          -I realprefix       prefix for eventual installation        [${realprefix}]
29          setname pkgname     set and package to build plist for
30USAGE
31          exit 1
32}
33
34umask 022
35# handle args
36while getopts a:I:m:p:s: ch; do
37          case ${ch} in
38          a)
39                    MACHINE_ARCH="${OPTARG}"
40                    MACHINE_CPU="$(arch_to_cpu "${OPTARG}")"
41                    ;;
42          I)
43                    realprefix=${OPTARG}
44                    got_realprefix=true
45                    ;;
46          m)
47                    MACHINE="${OPTARG}"
48                    ;;
49          p)
50                    prefix="${OPTARG}"
51                    ;;
52          s)
53                    setsdir="${OPTARG}"
54                    ;;
55          *)
56                    usage
57                    ;;
58          esac
59done
60shift $((${OPTIND} - 1))
61if [ $# -ne 2 ]; then
62          usage
63fi
64setname="$1"
65pkgname="$2"
66
67if ! ${got_realprefix}; then
68    realprefix="${prefix}"
69fi
70
71filename="/tmp/makeplist.$$"
72ffilename="/tmp/makeplist.files.$$"
73dfilename="/tmp/makeplist.dirs.$$"
74
75list_set_files "${setname}" | \
76    ${ENV_CMD} PLISTPKG="${pkgname}" ${AWK} '
77          $2 == ENVIRON["PLISTPKG"] {
78                    sub("^\\./", "", $1);
79                    print $1
80          }' | ${SORT} -u > "${filename}"
81
82SELECTDIRS="-prune -type d"
83SELECTNONDIRS="! -type d -print -o ( -type d -prune )"
84
85#
86# XXX: The "lists" do not differentiate between directories and files.
87# But we need to differentiate between them, so we do so by checking
88# what's actually present in the file system.  Files or directories that
89# are listed in the "lists" but that do not exist in the file system end
90# up not appearing in our output, and this subverts a large part of the
91# purpose of the "lists".
92#
93# XXX: Given that we have to figure out what is or is not a directory
94# without assistance from the "lists", it would be much more efficient
95# to consult the metalog instead of the file system.
96#
97
98(
99cd "${prefix}"
100
101#
102# Match the directories.  Use find(1) to avoid repeat calls to
103# 'test -d'.
104#
105# This is a little clever.  I cannot use 'xargs find', because
106# find wants for the option arguments to follow the path arguments.
107# So I use 'xargs echo ${SELECTDIRS}' to make a maximum-length proto-command
108# line.  I use 'read' to peel the options off the front of the
109# command-line, and 'find ${args} ${SELECTDIRS}' to put them at the end.
110#
111xargs echo ${SELECTDIRS} < "${filename}" | \
112while read ignore ignore ignore args; do
113          [ -z "${args}" ] && break
114          ${FIND} ${args} ${SELECTDIRS}
115done | ${AWK} '{ print "@pkgdir " $1; }' > "${dfilename}"
116
117#
118# Match the non-directories.  Use find(1) to avoid repeat calls to
119# 'test ! -d'.  See 'Match the directories' for an explanation of the
120# cleverness.
121#
122xargs echo ${SELECTNONDIRS} < "${filename}" | \
123while read ignore ignore ignore ignore ignore ignore ignore ignore ignore \
124    ignore args; do
125          [ -z "${args}" ] && break
126          ${FIND} ${args} ${SELECTNONDIRS}
127done > "${ffilename}"
128
129)
130
131echo "@cwd ${realprefix}"
132if [ -s "${ffilename}" ]; then
133          cat "${ffilename}"
134fi
135if [ -s "${dfilename}" ]; then
136        ${SORT} -r "${dfilename}"
137fi
138
139rm -f "${filename}" "${ffilename}" "${dfilename}"
140
141exit 0
142