commit | author | age
|
8ea4ef
|
1 |
#!/bin/bash |
f6854c
|
2 |
#A basic bash script to automate the building of arch packages |
JG |
3 |
# Usage: main.sh init|add|build_all [-f force] |
8ea4ef
|
4 |
|
8c5114
|
5 |
source vars.sh |
8ea4ef
|
6 |
|
JG |
7 |
ERRORS="" |
|
8 |
|
|
9 |
#Helper for finding newest and oldest files |
|
10 |
#Sourced from stack overflow |
f6854c
|
11 |
# Usage: newold_matching_file [n/o] [filename] |
8ea4ef
|
12 |
function newold_matching_file |
JG |
13 |
{ |
|
14 |
# Use ${1-} instead of $1 in case 'nounset' is set |
|
15 |
local -r glob_pattern=${2-} |
|
16 |
|
|
17 |
# To avoid printing garbage if no files match the pattern, set |
|
18 |
# 'nullglob' if necessary |
|
19 |
local -i need_to_unset_nullglob=0 |
|
20 |
if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then |
|
21 |
shopt -s nullglob |
|
22 |
need_to_unset_nullglob=1 |
|
23 |
fi |
|
24 |
|
|
25 |
file= |
|
26 |
for f in $glob_pattern ; do |
|
27 |
if [ $1 == "n" ]; then |
|
28 |
[[ -z $f || $f -nt $_file ]] && file=$f |
|
29 |
elif [ $1 == "o" ]; then |
|
30 |
[[ -z $f || $f -ot $_file ]] && file=$f |
|
31 |
fi |
|
32 |
done |
|
33 |
|
|
34 |
# To avoid unexpected behaviour elsewhere, unset nullglob if it was |
|
35 |
# set by this function |
|
36 |
(( need_to_unset_nullglob )) && shopt -u nullglob |
|
37 |
|
|
38 |
# Use printf instead of echo in case the file name begins with '-' |
|
39 |
[[ -n $file ]] && printf '%s\n' "$file" |
|
40 |
|
|
41 |
return 0 |
|
42 |
} |
|
43 |
|
|
44 |
#Build latest version of a package |
f6854c
|
45 |
# Usage: build_pkg [package name] [new?] [-f force] |
8ea4ef
|
46 |
function build_pkg { |
JG |
47 |
#check if PKGBUILD has updated, don't rebuild if hasn't changed |
|
48 |
if [[ ! -z $(git pull | grep "Already up to date.") && -z $(echo $1 | grep git) && -z $2 ]]; then |
|
49 |
return 2 |
|
50 |
fi |
f6854c
|
51 |
|
JG |
52 |
#remove old versions before build |
09a580
|
53 |
rm *$1*.pkg.tar.xz* |
f6854c
|
54 |
|
JG |
55 |
#make and force rebuild if is git package |
8ea4ef
|
56 |
makepkg -s --noconfirm $([ $CLEAN == "Y" ] && echo "-c") $([ $SIGN == "Y" ] && echo "--sign --key $KEY") $([ "$2" == "-f" ] && echo -f) |
JG |
57 |
if [ $? != 0 ]; then |
|
58 |
#Register error |
f6854c
|
59 |
ERRORS="$ERRORS $1" |
8ea4ef
|
60 |
return 1 |
JG |
61 |
fi |
|
62 |
|
|
63 |
#copy package to repo directory |
f6854c
|
64 |
#latest="$(newold_matching_file n '*.pkg.tar.xz')" |
c3d80e
|
65 |
#or f in $(find g.tar.xz' |
C |
66 |
#o |
09a580
|
67 |
rm $REPODIR/*$1*.pkg.tar.xz* |
C |
68 |
cp *$1*.pkg.tar.xz $REPODIR/ |
|
69 |
[ "$SIGN" == "Y" ] && cp *$1*.pkg.tar.xz.sig $REPODIR |
|
70 |
repo-add $([ "$SIGN" == "Y" ] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.xz $REPODIR/*$1*.pkg.tar.xz |
c3d80e
|
71 |
#one |
8ea4ef
|
72 |
|
JG |
73 |
#Remove old versions of packages |
f6854c
|
74 |
#TODO: Want to be able to keep multiple versions of old packages, future work |
JG |
75 |
#Currently old package versions stay in the repodir indefinately |
|
76 |
# while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ] |
|
77 |
# do |
|
78 |
# old=$(newold_matching_file o '*.pkg.tar.xz') |
|
79 |
# rm $REPODIR/$old $old |
|
80 |
# done |
8ea4ef
|
81 |
return 0 |
JG |
82 |
} |
|
83 |
|
|
84 |
#Update packages in BUILDDIR |
f6854c
|
85 |
# Usage: build_all [-f force] |
8ea4ef
|
86 |
function build_all { |
JG |
87 |
#system update |
|
88 |
if [ $UPDATE == "Y" ]; then |
|
89 |
sudo pacman -Syu --noconfirm |
|
90 |
fi |
|
91 |
#update every package currently stored |
c3d80e
|
92 |
for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d) |
8ea4ef
|
93 |
do |
JG |
94 |
cd $d |
|
95 |
build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 |
|
96 |
done |
|
97 |
|
|
98 |
return 0 |
|
99 |
} |
|
100 |
|
|
101 |
#Add a new package to be built |
f6854c
|
102 |
#There is no name checking so be sure to put in the name correctly |
JG |
103 |
# Usage: add [package name] |
8ea4ef
|
104 |
function add { |
JG |
105 |
cd $BUILDDIR |
|
106 |
git clone https://aur.archlinux.org/$1.git |
|
107 |
cd $1 |
f11bfd
|
108 |
build_pkg $1 new -f |
8ea4ef
|
109 |
return 0 |
c3d80e
|
110 |
} |
C |
111 |
|
|
112 |
#Remove a package from the build list and repository |
|
113 |
# Usage remove [package name] |
|
114 |
function remove { |
|
115 |
rm -rf $BUILDDIR/$1* |
|
116 |
repo-remove $REPODIR/$REPONAME.db.tar.xz $1 |
|
117 |
rm $REPODIR/$1* |
8ea4ef
|
118 |
} |
JG |
119 |
|
|
120 |
#Check config and create build folders |
f6854c
|
121 |
#Set variables before usage |
JG |
122 |
# Usage: init |
8ea4ef
|
123 |
function init { |
JG |
124 |
#check for configuration here |
|
125 |
[ -z $REPODIR ] && echo "Enter REPODIR" && return 1 |
|
126 |
[ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2 |
|
127 |
[ -z $REPONAME ] && echo "Enter REPONAME" && return 3 |
|
128 |
|
|
129 |
#make build directories |
|
130 |
[ ! -d $REPODIR ] && mkdir -p $REPODIR |
|
131 |
[ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR |
|
132 |
|
|
133 |
#packages required to build others |
|
134 |
sudo pacman -S --noconfirm base-devel git |
|
135 |
|
|
136 |
#add repo to pacman.conf so can install own packages |
|
137 |
if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then |
|
138 |
printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf |
|
139 |
fi |
|
140 |
|
|
141 |
#create GPG key for package signing |
|
142 |
if [ "$SIGN" == "Y" && "$KEY" == "" ]; then |
|
143 |
( |
|
144 |
echo "Key-Type: RSA" |
|
145 |
echo "Key-Length: 2048" |
|
146 |
echo "Subkey-Type: RSA" |
|
147 |
echo "Subkey-Length: 2048" |
|
148 |
echo "Passphrase: \"\"" |
|
149 |
echo "Expire-Date: 0" |
|
150 |
echo "Name-Real: John Doe" |
|
151 |
echo "Name-Comment: Arch buildbot" |
|
152 |
echo "Name-Email: $(whoami)@localhost" |
|
153 |
echo "%commit" |
|
154 |
) | gpg --batch --generate-key |
|
155 |
gpg --export --output $REPONAME.key --armor "John Doe" |
|
156 |
gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe" |
|
157 |
echo "Please change the key information in this file" |
|
158 |
fi |
|
159 |
|
|
160 |
return 0 |
|
161 |
} |
|
162 |
|
|
163 |
case $1 in |
|
164 |
"init") |
|
165 |
init;; |
|
166 |
"add") |
|
167 |
add $2;; |
|
168 |
"build-all") |
|
169 |
build_all $([ "$2" == "-f" ] && echo "-f") |
|
170 |
if [ "$ERRORS" != "" ]; then |
|
171 |
echo "Errors in packages $ERRORS" |
|
172 |
if [ "$EMAIL" != "" ]; then |
|
173 |
printf "Build for $(date)\nErrors found in $ERRORS\nPlease address these soon" | sendmail $EMAIL |
|
174 |
fi |
|
175 |
else |
|
176 |
echo "All packages built successfully" |
|
177 |
fi |
|
178 |
;; |
c3d80e
|
179 |
"remove") |
C |
180 |
remove $2;; |
8ea4ef
|
181 |
*) |
f11bfd
|
182 |
printf "Invalid usage\nUsage: $0 init|add|build-all\n";; |
8ea4ef
|
183 |
esac |