|  | @@ -0,0 +1,220 @@
 | 
	
		
			
			|  | 1 | +#!/bin/bash
 | 
	
		
			
			|  | 2 | +
 | 
	
		
			
			|  | 3 | +TRIGGER_COPR_CONFIG=${TRIGGER_COPR_CONFIG:-}
 | 
	
		
			
			|  | 4 | +TRIGGER_COPR_PKGNAME=${TRIGGER_COPR_PKGNAME:-}
 | 
	
		
			
			|  | 5 | +
 | 
	
		
			
			|  | 6 | +warn() {
 | 
	
		
			
			|  | 7 | +    echo "$1" >&2
 | 
	
		
			
			|  | 8 | +}
 | 
	
		
			
			|  | 9 | +
 | 
	
		
			
			|  | 10 | +die() {
 | 
	
		
			
			|  | 11 | +    warn "fatal: $1"
 | 
	
		
			
			|  | 12 | +    exit 3
 | 
	
		
			
			|  | 13 | +}
 | 
	
		
			
			|  | 14 | +
 | 
	
		
			
			|  | 15 | +usage() {
 | 
	
		
			
			|  | 16 | +    warn "usage: $0 [options] MODE"
 | 
	
		
			
			|  | 17 | +    warn ""
 | 
	
		
			
			|  | 18 | +    warn "Options:"
 | 
	
		
			
			|  | 19 | +    warn ""
 | 
	
		
			
			|  | 20 | +    warn "  -b BRN  build from branch BRN (default: last tag)"
 | 
	
		
			
			|  | 21 | +    warn "  -c COPR_PROJECT  COPR project name"
 | 
	
		
			
			|  | 22 | +    warn "  -C CONF   config file"
 | 
	
		
			
			|  | 23 | +    warn "  -n      dry mode, don't do anything (just show"
 | 
	
		
			
			|  | 24 | +    warn "          what would be done)"
 | 
	
		
			
			|  | 25 | +    warn "  -r REL  use REL as Release number in SPEC file"
 | 
	
		
			
			|  | 26 | +    warn "  -v VER  use VER as Version number in SPEC file"
 | 
	
		
			
			|  | 27 | +    warn "  -u URL  use URL as base (to get last tag and"
 | 
	
		
			
			|  | 28 | +    warn "          compose Source0 in SPEC file)"
 | 
	
		
			
			|  | 29 | +    warn ""
 | 
	
		
			
			|  | 30 | +    warn "If -b is not used, build is launched from last tag available"
 | 
	
		
			
			|  | 31 | +    warn "in the GitHub repo.  In that case, Release is '1' and Version"
 | 
	
		
			
			|  | 32 | +    warn "is deduced from the tag by removing the initial 'v'."
 | 
	
		
			
			|  | 33 | +    warn ""
 | 
	
		
			
			|  | 34 | +    warn "If -b is used, the project repo is temporarily cloned, and"
 | 
	
		
			
			|  | 35 | +    warn "both Version and Release are found by consulting git-describe"
 | 
	
		
			
			|  | 36 | +    warn "on the specified branch."
 | 
	
		
			
			|  | 37 | +    warn ""
 | 
	
		
			
			|  | 38 | +    warn "If MODE is 'scratch' (default), Release is pre-fixed by string"
 | 
	
		
			
			|  | 39 | +    warn "'0.scratch.' and build is triggered in scratch COPR project."
 | 
	
		
			
			|  | 40 | +    exit 2
 | 
	
		
			
			|  | 41 | +}
 | 
	
		
			
			|  | 42 | +
 | 
	
		
			
			|  | 43 | +last_version() {
 | 
	
		
			
			|  | 44 | +    git ls-remote --tag "$UrlBase" \
 | 
	
		
			
			|  | 45 | +      | grep '/tags/' \
 | 
	
		
			
			|  | 46 | +      | cut -d/ -f3 \
 | 
	
		
			
			|  | 47 | +      | grep -v '[^0-9a-z.]' \
 | 
	
		
			
			|  | 48 | +      | sort -V \
 | 
	
		
			
			|  | 49 | +      | tail -1 \
 | 
	
		
			
			|  | 50 | +      | sed "s/^v//"
 | 
	
		
			
			|  | 51 | +}
 | 
	
		
			
			|  | 52 | +
 | 
	
		
			
			|  | 53 | +mkspec() {
 | 
	
		
			
			|  | 54 | +    local self_version
 | 
	
		
			
			|  | 55 | +    local cl_date
 | 
	
		
			
			|  | 56 | +    self_version="$(basename "$0") $(git describe --tags)"
 | 
	
		
			
			|  | 57 | +    cl_date=$(LC_ALL=C date +"%a %b %d %Y")
 | 
	
		
			
			|  | 58 | +    sed -e "
 | 
	
		
			
			|  | 59 | +        s|__APP_VERSION__|$Version|
 | 
	
		
			
			|  | 60 | +        s|__APP_RELEASE__|$Release|
 | 
	
		
			
			|  | 61 | +        s|__APP_URLBASE__|$UrlBase|
 | 
	
		
			
			|  | 62 | +        s|__APP_BUILDSCRIPT_VERSION__|$self_version|
 | 
	
		
			
			|  | 63 | +        s|__APP_DATE__|$cl_date|
 | 
	
		
			
			|  | 64 | +    " <"$PkgName.spec.in"
 | 
	
		
			
			|  | 65 | +}
 | 
	
		
			
			|  | 66 | +
 | 
	
		
			
			|  | 67 | +git_guess() {
 | 
	
		
			
			|  | 68 | +    #
 | 
	
		
			
			|  | 69 | +    # Print git-guessed $1
 | 
	
		
			
			|  | 70 | +    #
 | 
	
		
			
			|  | 71 | +    local what=$1   # what we want (ver|rel)
 | 
	
		
			
			|  | 72 | +    local describe  # full git-describe output
 | 
	
		
			
			|  | 73 | +    local xtra=     # extra part (-N-gHASH)
 | 
	
		
			
			|  | 74 | +    local num=0     # num. of commits since tag (N)
 | 
	
		
			
			|  | 75 | +    local sha=      # '.g'+sha1 of this commit (gHASH)
 | 
	
		
			
			|  | 76 | +    describe=$(git describe --tags --always HEAD)
 | 
	
		
			
			|  | 77 | +    case $describe in
 | 
	
		
			
			|  | 78 | +        *-*)                        #     v1.2.3-21-g654cba
 | 
	
		
			
			|  | 79 | +            tag=${describe%%-*}     # tag=v1.2.3
 | 
	
		
			
			|  | 80 | +            xtra=${describe#$tag-}  # xtra=-21-g654cba
 | 
	
		
			
			|  | 81 | +            num=${xtra%%-*}         # num=21
 | 
	
		
			
			|  | 82 | +            sha=.${xtra#$num-}      # sha=.g654cba
 | 
	
		
			
			|  | 83 | +            ;;
 | 
	
		
			
			|  | 84 | +        *)
 | 
	
		
			
			|  | 85 | +            tag=$describe
 | 
	
		
			
			|  | 86 | +            ;;
 | 
	
		
			
			|  | 87 | +    esac
 | 
	
		
			
			|  | 88 | +    case $what in
 | 
	
		
			
			|  | 89 | +        ver)    echo "${tag#v}"         ;;
 | 
	
		
			
			|  | 90 | +        rel)    echo "$((num + 1))$sha" ;;
 | 
	
		
			
			|  | 91 | +    esac
 | 
	
		
			
			|  | 92 | +}
 | 
	
		
			
			|  | 93 | +
 | 
	
		
			
			|  | 94 | +choose_relpfx() {
 | 
	
		
			
			|  | 95 | +    #
 | 
	
		
			
			|  | 96 | +    # Choose COPR project based on $Mode
 | 
	
		
			
			|  | 97 | +    #
 | 
	
		
			
			|  | 98 | +    test "$Mode" == scratch && echo 0.scratch.
 | 
	
		
			
			|  | 99 | +    return 0
 | 
	
		
			
			|  | 100 | +}
 | 
	
		
			
			|  | 101 | +
 | 
	
		
			
			|  | 102 | +read_conffile() {
 | 
	
		
			
			|  | 103 | +    #
 | 
	
		
			
			|  | 104 | +    # Read item T
 | 
	
		
			
			|  | 105 | +    #
 | 
	
		
			
			|  | 106 | +    local what=$1
 | 
	
		
			
			|  | 107 | +    local fieldn
 | 
	
		
			
			|  | 108 | +    case $what in
 | 
	
		
			
			|  | 109 | +        urlbase) fieldn=2 ;;
 | 
	
		
			
			|  | 110 | +        copr)    fieldn=3 ;;
 | 
	
		
			
			|  | 111 | +    esac
 | 
	
		
			
			|  | 112 | +    grep -v '^[[:blank:]]*#' "$ConfFile" \
 | 
	
		
			
			|  | 113 | +      | grep "^ *$Mode\>" \
 | 
	
		
			
			|  | 114 | +      | sed 's/  */ /g' \
 | 
	
		
			
			|  | 115 | +      | cut -d' ' -f$fieldn
 | 
	
		
			
			|  | 116 | +}
 | 
	
		
			
			|  | 117 | +
 | 
	
		
			
			|  | 118 | +choose_conffile() {
 | 
	
		
			
			|  | 119 | +    #
 | 
	
		
			
			|  | 120 | +    # Find config file and echo its name
 | 
	
		
			
			|  | 121 | +    #
 | 
	
		
			
			|  | 122 | +    find . -name '*.trigger' \
 | 
	
		
			
			|  | 123 | +      | cut -d/ -f2 \
 | 
	
		
			
			|  | 124 | +      | grep .
 | 
	
		
			
			|  | 125 | +}
 | 
	
		
			
			|  | 126 | +
 | 
	
		
			
			|  | 127 | +do_action() {
 | 
	
		
			
			|  | 128 | +    local url
 | 
	
		
			
			|  | 129 | +    case $Action in
 | 
	
		
			
			|  | 130 | +        build)
 | 
	
		
			
			|  | 131 | +            copr build "$CoprProject" "$Tmp/$PkgName.spec"
 | 
	
		
			
			|  | 132 | +            printf '\a'
 | 
	
		
			
			|  | 133 | +            ;;
 | 
	
		
			
			|  | 134 | +        demo)
 | 
	
		
			
			|  | 135 | +            warn "demo mode active, we would build:"
 | 
	
		
			
			|  | 136 | +            warn "    at $CoprProject"
 | 
	
		
			
			|  | 137 | +            test -n "$Branch" && warn "    using branch $Branch"
 | 
	
		
			
			|  | 138 | +            test -n "$Branch" || warn "    using last tag"
 | 
	
		
			
			|  | 139 | +            warn "    from $UrlBase"
 | 
	
		
			
			|  | 140 | +            warn "    yielding $PkgName-$Version-$Release.*.rpm"
 | 
	
		
			
			|  | 141 | +            warn ""
 | 
	
		
			
			|  | 142 | +            warn "===== BEGIN $PkgName.spec ====="
 | 
	
		
			
			|  | 143 | +            cat "$Tmp/$PkgName.spec"
 | 
	
		
			
			|  | 144 | +            warn "===== END $PkgName.spec ====="
 | 
	
		
			
			|  | 145 | +            return 1
 | 
	
		
			
			|  | 146 | +            ;;
 | 
	
		
			
			|  | 147 | +        mkspec)
 | 
	
		
			
			|  | 148 | +            cat "$Tmp/$PkgName.spec"
 | 
	
		
			
			|  | 149 | +            ;;
 | 
	
		
			
			|  | 150 | +        rpmstuff)
 | 
	
		
			
			|  | 151 | +            url=$(
 | 
	
		
			
			|  | 152 | +                grep -o 'Source.*:.*http.*' "$Tmp/$PkgName.spec" \
 | 
	
		
			
			|  | 153 | +                  | sed "
 | 
	
		
			
			|  | 154 | +                        s/.*http/http/
 | 
	
		
			
			|  | 155 | +                        s/ *$//
 | 
	
		
			
			|  | 156 | +                        s/%{version}/$Version/
 | 
	
		
			
			|  | 157 | +                    "
 | 
	
		
			
			|  | 158 | +            )
 | 
	
		
			
			|  | 159 | +            wget --quiet "$url"
 | 
	
		
			
			|  | 160 | +            cat "$Tmp/$PkgName.spec" > "$PkgName.spec"
 | 
	
		
			
			|  | 161 | +            ;;
 | 
	
		
			
			|  | 162 | +    esac
 | 
	
		
			
			|  | 163 | +}
 | 
	
		
			
			|  | 164 | +
 | 
	
		
			
			|  | 165 | +main() {
 | 
	
		
			
			|  | 166 | +    local Version       # Version in SPEC file
 | 
	
		
			
			|  | 167 | +    local Release       # Release in SPEC file
 | 
	
		
			
			|  | 168 | +    local CoprProject   # COPR project
 | 
	
		
			
			|  | 169 | +    local UrlBase       # GitHub URL base
 | 
	
		
			
			|  | 170 | +    local Tmp           # our temp
 | 
	
		
			
			|  | 171 | +    local Branch        # branch to use, if empty, tags are considered
 | 
	
		
			
			|  | 172 | +    local Mode=scratch  # implies COPR project and release prefix
 | 
	
		
			
			|  | 173 | +    local Action=build  # what to do
 | 
	
		
			
			|  | 174 | +    local ConfFile      # config file to use
 | 
	
		
			
			|  | 175 | +    local PkgName       # package name
 | 
	
		
			
			|  | 176 | +    local es=0          # exit status
 | 
	
		
			
			|  | 177 | +    which copr >/dev/null \
 | 
	
		
			
			|  | 178 | +     || die "copr not found, try 'sudo install copr-cli'"
 | 
	
		
			
			|  | 179 | +    Tmp=$(mktemp -d)
 | 
	
		
			
			|  | 180 | +    while true; do case $1 in
 | 
	
		
			
			|  | 181 | +        -C) ConfFile=$2;    shift 2 || usage ;;
 | 
	
		
			
			|  | 182 | +        -a) Action=$2;      shift 2 || usage ;;
 | 
	
		
			
			|  | 183 | +        -b) Branch=$2;      shift 2 || usage ;;
 | 
	
		
			
			|  | 184 | +        -c) CoprProject=$2; shift 2 || usage ;;
 | 
	
		
			
			|  | 185 | +        -u) UrlBase=$2;     shift 2 || usage ;;
 | 
	
		
			
			|  | 186 | +        -r) Release=$2;     shift 2 || usage ;;
 | 
	
		
			
			|  | 187 | +        -v) Version=${2#v}; shift 2 || usage ;;
 | 
	
		
			
			|  | 188 | +        -n) Action=demo;    shift ;;
 | 
	
		
			
			|  | 189 | +        -*) usage ;;
 | 
	
		
			
			|  | 190 | +        *)  break ;;
 | 
	
		
			
			|  | 191 | +    esac done
 | 
	
		
			
			|  | 192 | +    Mode=${1:-$Mode}
 | 
	
		
			
			|  | 193 | +    case $Action in
 | 
	
		
			
			|  | 194 | +        build|demo|mkspec|rpmstuff) : ;;
 | 
	
		
			
			|  | 195 | +        *) usage ;;
 | 
	
		
			
			|  | 196 | +    esac
 | 
	
		
			
			|  | 197 | +    test -n "$ConfFile" || ConfFile=$TRIGGER_COPR_CONFIG
 | 
	
		
			
			|  | 198 | +    test -n "$ConfFile" || ConfFile=$(choose_conffile)
 | 
	
		
			
			|  | 199 | +    test -n "$ConfFile" || die "could not find config file"
 | 
	
		
			
			|  | 200 | +    test -r "$ConfFile" || die "could not read config file"
 | 
	
		
			
			|  | 201 | +    test -n "$PkgName"  || PkgName=$TRIGGER_COPR_PKGNAME
 | 
	
		
			
			|  | 202 | +    test -n "$PkgName"  || PkgName=${ConfFile%.trigger}
 | 
	
		
			
			|  | 203 | +    test -n "$CoprProject" || CoprProject=$(read_conffile copr)
 | 
	
		
			
			|  | 204 | +    test -n "$UrlBase"     || UrlBase=$(read_conffile urlbase)
 | 
	
		
			
			|  | 205 | +    if test -n "$Branch"; then
 | 
	
		
			
			|  | 206 | +        die "not implemented"
 | 
	
		
			
			|  | 207 | +        test -n "$Version" || Version=$(git_guess ver)
 | 
	
		
			
			|  | 208 | +        test -n "$Release" || Release=$(git_guess rel)
 | 
	
		
			
			|  | 209 | +    else
 | 
	
		
			
			|  | 210 | +        test -n "$Version" || Version=$(last_version)
 | 
	
		
			
			|  | 211 | +        test -n "$Release" || Release=1
 | 
	
		
			
			|  | 212 | +    fi
 | 
	
		
			
			|  | 213 | +    Release=$(choose_relpfx)$Release
 | 
	
		
			
			|  | 214 | +    mkspec >"$Tmp/$PkgName.spec"
 | 
	
		
			
			|  | 215 | +        do_action
 | 
	
		
			
			|  | 216 | +    rm -rf "$Tmp"
 | 
	
		
			
			|  | 217 | +    return $es
 | 
	
		
			
			|  | 218 | +}
 | 
	
		
			
			|  | 219 | +
 | 
	
		
			
			|  | 220 | +main "$@"
 |