summaryrefslogtreecommitdiff
path: root/scripts/apk_create
blob: 6bbb9ee7d264368d25ac4dddfc14a37f4116affb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh
#
# apk_create - a utility for creating software packages.
#
# Copyright (c) 2005 Natanael Copa
#
# Distributed under GPL-2
#

PROGRAM=apk_create

#load the libraries
. "${APK_LIBS:=/lib/apk}/libutil.sh"
#. "$APK_LIBS/libfetch.sh"
#. "$APK_LIBS/libindex.sh"
#. "$APK_LIBS/libdb.sh"
#. "$APK_LIBS/libpkgf.sh"

# print usage and die
usage() {
	echo "$PROGRAM $VERSION"
	echo "
Usage: apk_create [hvS] [-a [+]arch] [-c [+]desc] [-i iscript] [-I piscript]
      [-k dscript] [-K pdscript] [-l [+]license] [-m metadir] [-o owners] 
      [-p package] [-P [+]pkgs] [-s srcdir] [-u uscript] [-U puscript] 
      [-x excludepattern] [-X excludefile ] [-w [+]uri] pkg-filename

Options:
  -a  Read package architecture from arch or use the argument itself, if
      preceded with an '+'.
  -c  Read package description from desc or use the argument itself, if
      preceded with an '+'.
  -f  Use fakeroot when creating the package.
  -h  Show help and exit.
  -i  Use iscript as pre-install script.
  -I  Use piscript as post-install script.
  -k  Use dscript as deinstall script.
  -K  Use pdscript as post-deinstall script.
  -l  Read package license from license or use the argument itself, if
      preceded with an '+'.
  -m  Use all metafiles found in metadir.
  -o  Use file as OWNERS file.
  -p  Use package as source in addition to or instead of source dir.
  -P  Read space separated dependencies from pkgs or use the argument itself,
      if preceded with an '+'.
  -s  Set source directory to srcdir.
  -S  Strip ELF files with sstrip.
  -T  Include files listed in includefile.
  -u  Use uscript as pre-update script.
  -U  Use puscript as post-upgrade script.
  -v  Turn on verbose output.
  -w  Read webpage URI from uri or use the argument itself. if preceded
      with an '+'. 
  -x  Exclude files matching excludepattern.
  -X  Exclude files listed in excludefile.
  "
	exit 1
}

# if $1 start with a '+', strip the '+' and echo the reset
# otherwise read contents from file $1
get_arg_or_file() {
	local tmp
	# we can do this with awk
	# but we want it more readable...
	# echo "$1" | awk '
	#		/^\+/ { print substr($0, 2)}
	#		/^[^\+]/ { readline < $0 ; print}'
	if echo "$1" | grep '^+' > /dev/null; then
		echo "$1" | sed 's/^\+//'
		return 0
	fi
	cat "$1" || die "Could not read file '$1'"
}

#parse args
unset vflag


while getopts "a:c:fhi:I:k:K:l:m:o:p:P:Ss:T:u:U:vX:x:w:" opt ; do
	case "$opt" in
		a)  ARCH=`get_arg_or_file "$OPTARG"`;;
		h)  usage;;
		c)  DESC=`get_arg_or_file "$OPTARG"`;;
		i)  PRE_INSTALL="$OPTARG";;
		I)  POST_INSTALL="$OPTARG";;
		k)  DSCRIPT="$OPTARG";;
		K)  POST_DEINSTALL="$OPTARG";;
		l)  LICENSE=`get_arg_or_file "$OPTARG"`;;
		m)  METADIR="$OPTARG";;
		o)  OWNERS="$OPTARG";;
		p)  case "$OPTARG" in
			*.tar.bz) UNTAR="tar -zxf \"$OPTARG\"";;
			*.tar.bz2) UNTAR="tar -jxf \"$OPTARG\"";;
			*) die "Only .tar.gz and .tar.bz2 are supported";;
		    esac;;
		P)  DEPEND=`get_arg_or_file "$OPTARG"`;;
		s)  SRCDIR="$OPTARG";;
		S)  SSTRIP="-S";;
		T)  INCLUDE_FROM="$INCLUDE_FROM --include-from=$OPTARG";;
		u)  PRE_UPDATE="$OPTARG";;
		U)  POST_UPDATE="$OPTARG";;		
		v)  VERBOSE="-v" ;;
		x)  EXCLUDE="$EXCLUDE --exclude=$OPTARG";;
		X)  EXCLUDE_FROM="$EXCLUDE_FROM --exclude-from=$OPTARG";;
		w)  WWW=`get_arg_or_file "$OPTARG"`;;
		\?)	usage;;
	esac
done
shift `expr $OPTIND - 1`

# if -s and -p is not specified, use current dir as default.
[ -z "$SRCDIR$UNTAR" ] && SRCDIR="."

[ $# -ne 1 ] && usage

which rsync > /dev/null || die "This program depends on rsync."

if [ "`whoami`" != "root" ] ; then
	SUDO=sudo
fi

# find absolute path to package
case "$1" in
	/*) PKGVF="$1";;
	*) PKGVF="$PWD/$1";;
esac

PV="`basename \"$PKGVF\" .apk`"
echo "$PV" | grep -- '-[0-9].*' > /dev/null || die "Package name $PKGVF does not have any version."
P="`echo $PV | sed 's/-[0-9].*//'`"
V="`echo $PV | sed 's/^'$P'-//'`"

# clean temp dir
tmp="$APK_TMPDIR/$PV"
rm -rf "$tmp"

# copy files
if [ "$UNTAR" ] ; then
	$UNTAR $INCLUDE_FROM $EXCLUDE $EXCLUDE_FROM -C "$tmp" || \
		die "Failed to unpack"
fi

if [ "$SRCDIR" ] ; then
	rsync -ra $INCLUDE_FROM $EXCLUDE $EXCLUDE_FROM "$SRCDIR/." "$tmp" ||\
		die "Failed to copy files from '$SRCDIR/.' to '$tmp'".
fi

# run sstrip
[ "$SSTRIP" ] && find "$tmp" -type f \
		| xargs -n1 file \
		| grep ELF \
		| grep -v relocatable \
		| cut -d : -f 1 \
		| while read f ; do
			$SUDO sstrip "$f"
		done

# create meta data
dbdir=`beautify_path "$tmp/$APK_DBDIR"`
db="$dbdir/$PV"
mkdir -p "$db"

if [ "$METADIR" ] ; then
	cp $METADIR/* "$db/" || die "Failed to copy files from '$METADIR'"
fi
[ "$ARCH" ] && echo "$ARCH" > "$db/ARCH"
[ "$DEPEND" ] && echo "$DEPEND" > "$db/DEPEND"
[ "$DESC" ] && echo "$DESC" > "$db/DESC"
[ "$LICENSE" ] && echo "$LICENSE" > "$db/LICENSE"
[ "$WWW" ] && echo "$WWW" > "$db/WWW"

[ "$PRE_INSTALL" ] && cp -a "$PRE_INSTALL" "$db/pre-install"
[ "$POST_INSTALL" ] && cp -a "$POST_INSTALL" "$db/post-install"
[ "$DSCRIPT" ] && cp -a "$DSCRIPT" "$db/pre-deinstall"
[ "$POST_DEINSTALL" ] && cp -a "$POST_DEINSTALL" "$db/post-deinstall"
[ "$PRE_UPDATE" ] && cp -a "$PRE_UPDATE" "$db/pre-update"
[ "$POST_UPDATE" ] && cp -a "$POST_UPDATE" "$db/post-update"
[ "$OWNERS" ] && cp -a "$OWNERS" "$db/OWNERS"

# check if var/db/apk contains more than it should
[ `ls "$dbdir" | wc -l` -gt 1 ] && die "$APK_DBDIR should only contain the directory $PV."

# check if arch, desc, license and www info is there
[ -f "$db/ARCH" ] || die "Architecture not set. Use -a."
[ -f "$db/DESC" ] || die "Description not set. Use -c."
[ -f "$db/LICENSE" ] || die "License not set. Use -l."
[ -f "$db/WWW" ] || eecho "Warning: Homepage not set (use -w)."

# now, lets create the package
cd "$tmp"
tar --owner=root --group=root $VERBOSE -zcf "$PKGVF" *

# cleanup
rm -rf "$tmp"