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

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