mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

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