summaryrefslogblamecommitdiff
path: root/scripts/apk_create
blob: 6bbb9ee7d264368d25ac4dddfc14a37f4116affb (plain) (tree)


































































































































































































                                                                                                   
#!/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"