summaryrefslogtreecommitdiff
path: root/newapkbuild-cpan.in
blob: 9ae726b90e1340a154e5c8bebeab6c4024084da2 (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
196
#!/bin/sh

# script to generate a new APKBUILD
# Copyright (c) 2009 Natanael Copa <natanael.copa@gmail.com>
#
# Distributed under GPL-2
#
# Depends on: busybox utilities, fakeroot, 
#

version=@VERSION@
sysconfdir=@sysconfdir@
datadir=@datadir@

prog=${0##*/}

# source $PACKAGER
for i in $sysconfdir/abuild.conf $HOME/.abuild/abuild.conf; do
	if [ -f "$i" ]; then
		. $i
	fi
done

error() {
	echo "$@" >&2
}

is_url() {
	case "$1" in
	http://*|ftp://*) return 0;;
	esac
	return 1
}

get_cpan_source() {
	local mod="$1"
	wget -q -O - http://search.cpan.org/dist/$mod/ \
		| grep "href.*$mod-[0-9].*Download" \
		| sed "s|.*href=\"\(.*\)\".*|http://search.cpan.org\1|"
}

# create new aport from templates
newaport() {
	local newname= pv= source= pn= mod=
	
	if is_url "$1"; then
		source="$1"
		newname="${1##*/}"
		mod=$(echo $pn | sed 's/-/::/g')
	else
		if echo $1 | grep -q '::'; then
			mod=$1
			pn=$(echo $mod | sed 's/::/-/g')
		else
			pn=$1
			mod=$(echo $pn | sed 's/-/::/g')
		fi
		source=$(get_cpan_source $pn)
		newname="${source##*/}"
	fi
	if [ -z "$source" ]; then
		error "source not found"
		exit 1
	fi
	pn=${newname%-[0-9]*}
	pv=${newname#$pn-}

	if [ "$pn" != "$newname" ]; then
		pv=${newname#$pn-}
		pv=${pv%.t*} #strip .tar.gz .tgz .tar.bz2 etc
	fi
	pkgname=perl-$(echo $pn | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz')

	if [ -e "$pkgname"/APKBUILD ] && [ -z "$force" ]; then
		error "$pkgname/APKBUILD already exist"
		return 1
	fi
	mkdir -p "$pkgname"
	cd "$pkgname"

	# replace pkgver in $source
	if [ -n "$source" ]; then
		source=$(echo "$source" | sed "s/$pv/\$pkgver/g")
	fi

	if [ -z "$url" ]; then
		url="http://search.cpan.org/dist/$pn/"
	fi

	if [ -z "$license" ]; then
		license="GPLv2 or Artistic"
	fi

	if [ -z "$pkgdesc" ]; then
		pkgdesc="$mod perl module"
	fi
	
	# generate header with standard variables
	cat >APKBUILD<<__EOF__
# Contributor:${PACKAGER:+" "}${PACKAGER}
# Maintainer:${MAINTAINER:+" "}${MAINTAINER}
pkgname=$pkgname
pkgver=$pv
pkgrel=0
pkgdesc="$pkgdesc"
url="$url"
arch="noarch"
license="$license"
depends="perl"
makedepends="perl-dev"
install="$install"
subpackages="\$pkgname-doc"
source="$source"

__EOF__

	abuild -f fetch unpack
	# figure out the _builddir
	for i in src/*; do
		if [ -d "$i" ]; then
			sdir=$i
			_builddir=$(echo ${i#*/} | sed "s/$pv/\$pkgver/g")
			_builddir="\"\$srcdir\"/$_builddir"
		fi
	done
	echo "_builddir=$_builddir" >> APKBUILD

	# create the prepare() template
	cat >>APKBUILD<<__EOF__
prepare() {
	local i
	cd "\$_builddir"
	for i in \$source; do
		case \$i in
		*.patch) msg \$i; patch -p1 -i "\$srcdir"/\$i || return 1;;
		esac
	done
}

__EOF__

	# create build()
	cat >>APKBUILD<<__EOF__
build() {
	cd "\$_builddir"
	PERL_MM_USE_DEFAULT=1 perl Makefile.PL INSTALLDIRS=vendor || return 1
	make && make test || return 1
}

package() {
	cd "\$_builddir"
	make DESTDIR="\$pkgdir" install || return 1
	find "\$pkgdir" \\( -name perllocal.pod -o -name .packlist \\) -delete
}

__EOF__
	abuild -r checksum all
}

usage() {
	echo "$prog $version"
	echo "usage: $prog [-cfh] [-d DESC] [-l LICENSE] [-u URL] PKGNAME[-PKGVER]"
	echo "Options:"
	echo " -a  Create autotools (use ./configure ...)"
	echo " -c  Copy a sample init.d, conf.d and install script to new directory"
	echo " -d  Set package description (pkgdesc) to DESC"
	echo " -f  Force even if directory already exist"
	echo " -h  Show this help"
	echo " -l  Set package license to LICENSE"
	echo " -p  Create perl package (Assume Makefile.PL is there)"
	echo " -u  Set package URL"
	echo " -s  Use sourceforge source url"
	echo ""
	exit 0
}

while getopts "acd:fhl:pu:s" opt; do
	case $opt in
		'a') buildtype="autotools";;
		'c') cpinitd=1;;
		'd') pkgdesc="$OPTARG";;
		'f') force=1;;
		'h') usage;;
		'l') license="$OPTARG";;
		'p') buildtype="perl";;
		'u') url="$OPTARG";;
		's') sourceforge=1;;
	esac
done
shift $(( $OPTIND - 1 ))

while [ $# -gt 0 ]; do 
	(newaport $1) || exit 1
	shift
done