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

Joel Grunbaum
2020-09-16 f11bfdf59da8a87bf4db7e79e87e921a40a25138
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
53     rm "$1*.pkg.tar.xz"
54
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')"
JG 65     for f in '$1*.pkg.tar.xz'
66     do
67         cp $f $REPODIR/$f
68         repo-add $([ "$SIGN" == "Y" ] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.xz $REPODIR/$f
69     done
8ea4ef 70
JG 71     #Remove old versions of packages
f6854c 72     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 73     #Currently old package versions stay in the repodir indefinately
74     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
75     # do
76     #     old=$(newold_matching_file o '*.pkg.tar.xz')
77     #     rm $REPODIR/$old $old
78     # done
8ea4ef 79     return 0
JG 80 }
81
82 #Update packages in BUILDDIR
f6854c 83 # Usage: build_all [-f force]
8ea4ef 84 function build_all {
JG 85     #system update
86     if [ $UPDATE == "Y" ]; then
87         sudo pacman -Syu --noconfirm
88     fi
89     #update every package currently stored
90     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -not -path '*/\.*' -type d)
91     do
92         cd $d
93         build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1
94     done
95
96     return 0
97 }
98
99 #Add a new package to be built
f6854c 100 #There is no name checking so be sure to put in the name correctly
JG 101 # Usage: add [package name]
8ea4ef 102 function add {
JG 103     cd $BUILDDIR
104     git clone https://aur.archlinux.org/$1.git
105     cd $1
f11bfd 106     build_pkg $1 new -f
8ea4ef 107     return 0
JG 108 }
109
110 #Check config and create build folders
f6854c 111 #Set variables before usage
JG 112 # Usage: init
8ea4ef 113 function init {
JG 114     #check for configuration here
115     [ -z $REPODIR ] && echo "Enter REPODIR" && return 1
116     [ -z $BUILDDIR ] && echo "Enter BUILDDIR" && return 2
117     [ -z $REPONAME ] && echo "Enter REPONAME" && return 3
118
119     #make build directories
120     [ ! -d $REPODIR ] && mkdir -p $REPODIR
121     [ ! -d $BUILDDIR ] && mkdir -p $BUILDDIR
122
123     #packages required to build others
124     sudo pacman -S --noconfirm base-devel git
125
126     #add repo to pacman.conf so can install own packages
127     if [ -z $(grep "$REPONAME" /etc/pacman.conf) ]; then
128         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
129     fi
130
131     #create GPG key for package signing
132     if [ "$SIGN" == "Y" && "$KEY" == "" ]; then
133         (
134             echo "Key-Type: RSA"
135             echo "Key-Length: 2048"
136             echo "Subkey-Type: RSA"
137             echo "Subkey-Length: 2048"
138             echo "Passphrase: \"\""
139             echo "Expire-Date: 0"
140             echo "Name-Real: John Doe"
141             echo "Name-Comment: Arch buildbot"
142             echo "Name-Email: $(whoami)@localhost"
143             echo "%commit"
144         ) | gpg --batch --generate-key
145         gpg --export --output $REPONAME.key --armor "John Doe"
146         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
147         echo "Please change the key information in this file"
148     fi
149
150     return 0
151 }
152
153 case $1 in
154     "init")
155         init;;
156     "add")
157         add $2;;
158     "build-all")
159         build_all $([ "$2" == "-f" ] && echo "-f")
160         if [ "$ERRORS" != "" ]; then
161             echo "Errors in packages $ERRORS"
162             if [ "$EMAIL" != "" ]; then
163                 printf "Build for $(date)\nErrors found in $ERRORS\nPlease address these soon" | sendmail $EMAIL
164             fi
165         else
166             echo "All packages built successfully"
167         fi
168         ;;
169     *)
f11bfd 170         printf "Invalid usage\nUsage: $0 init|add|build-all\n";;
8ea4ef 171 esac