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

Chizi123
2020-09-20 86d762dc31655e4677678ec643ab3024f42753e3
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
ac333f 91
8ea4ef 92     #update every package currently stored
c3d80e 93     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 94     do
JG 95         cd $d
96         build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1
97     done
98
99     return 0
100 }
101
102 #Add a new package to be built
f6854c 103 #There is no name checking so be sure to put in the name correctly
JG 104 # Usage: add [package name]
8ea4ef 105 function add {
JG 106     cd $BUILDDIR
107     git clone https://aur.archlinux.org/$1.git
108     cd $1
f11bfd 109     build_pkg $1 new -f
8ea4ef 110     return 0
c3d80e 111 }
C 112
113 #Remove a package from the build list and repository
114 # Usage remove [package name]
115 function remove {
116     rm -rf $BUILDDIR/$1*
117     repo-remove $REPODIR/$REPONAME.db.tar.xz $1
118     rm $REPODIR/$1*
8ea4ef 119 }
JG 120
121 #Check config and create build folders
f6854c 122 #Set variables before usage
JG 123 # Usage: init
8ea4ef 124 function init {
JG 125     #check for configuration here
126     [ -z $REPODIR ] && echo "Enter REPODIR" && return 1
127     [ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2
128     [ -z $REPONAME ] && echo "Enter REPONAME" && return 3
129
130     #make build directories
131     [ ! -d $REPODIR ] && mkdir -p $REPODIR
132     [ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR
133
134     #packages required to build others
135     sudo pacman -S --noconfirm base-devel git
136
137     #add repo to pacman.conf so can install own packages
138     if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then
139         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
140     fi
141
142     #create GPG key for package signing
143     if [ "$SIGN" == "Y" && "$KEY" == "" ]; then
144         (
145             echo "Key-Type: RSA"
146             echo "Key-Length: 2048"
147             echo "Subkey-Type: RSA"
148             echo "Subkey-Length: 2048"
149             echo "Passphrase: \"\""
150             echo "Expire-Date: 0"
151             echo "Name-Real: John Doe"
152             echo "Name-Comment: Arch buildbot"
153             echo "Name-Email: $(whoami)@localhost"
154             echo "%commit"
155         ) | gpg --batch --generate-key
156         gpg --export --output $REPONAME.key --armor "John Doe"
157         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
158         echo "Please change the key information in this file"
159     fi
160
161     return 0
162 }
163
86d762 164 function send_email {
C 165     (
166     echo "From: build@localhost"
167     echo "To: $EMAIL"
168     echo "Subject: Build errors"
169     echo "There were build errors for the build at $(date), please address them soon."
170     echo "The errors were: $ERRORS"
171     ) | sendmail -t
172 }
173
8ea4ef 174 case $1 in
JG 175     "init")
176         init;;
177     "add")
178         add $2;;
179     "build-all")
180         build_all $([ "$2" == "-f" ] && echo "-f")
181         if [ "$ERRORS" != "" ]; then
182             echo "Errors in packages $ERRORS"
183             if [ "$EMAIL" != "" ]; then
86d762 184                 send_email
8ea4ef 185             fi
JG 186         else
187             echo "All packages built successfully"
188         fi
189         ;;
c3d80e 190     "remove")
C 191         remove $2;;
8ea4ef 192     *)
f11bfd 193         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 194 esac