File archive-it of Package kmymoney

#!/bin/bash

declare -A versions
declare -A branches
declare -A patches

if [ ! -e archive-it.conf ] ; then
    cat <<HHH
archive-it.conf file does not exist
should contain:
rcfile=<path to project config file>
product=<product name>
HHH
    return 1 2>/dev/null || exit 1
fi

. ./archive-it.conf

path=
# update spec file
patchit=
demoit=0
# branch name in the source project
branch=
# repository name to update a version
repo=
# target version for current repo
ver=
repopatches=
doall=0
doconfirm=1

echo Loading config file: $rcfile
if [ -e $rcfile ] ; then
    . $rcfile
else
    echo Warning: rcfile $rcfile does not exist
fi

function help() {
        cat <<HHH
Usage:
    archive-it [options]
    Options:
    --help    	This help
    -y          Answer 'y' to all questions
    --demo    	Show options and exit
    --all       Update all files
    --config    Show current repo/patches config
    --path      source files with git repository
    --repo    	Set repository name, like 15.4 (current repository)
    --version   Set package version for curre
    --patch  pt Create patch file using specified commit/tag/branch, comma delimited list
    --branch br base branch name, like master
HHH
}

function showConfig() {
    local repos=()
    for r in ${!versions[@]} ; do
        repos+=($r)
    done
    
    cat <<HHH
Script will run using parameters:
    Path        : $path
HHH
    for r in ${repos[@]} ; do
    cat <<HHH
    Repository  : $r
                  tag/branch  : ${branches[$r]}
                  version     : ${versions[$r]}
                  patches     : ${patches[$r]}
HHH
    done
}

function saveRepoConfig() {
    branches[$repo]=$branch
    versions[$repo]=$ver
    
cat <<HHH > $rcfile
#!/bin/bash
declare -A branches
declare -A versions
declare -A patches
path=$path
repo=$repo

HHH
    
    for r in ${!branches[@]} ; do
        echo 'branches['\'$r\'']='\'${branches[$r]}\' >> $rcfile
    done
    for r in ${!versions[@]} ; do
        echo 'versions['\'$r\'']='\'${versions[$r]}\' >> $rcfile
    done
    for r in ${!patches[@]} ; do
        echo 'patches['\'$r\'']='\'${patches[$r]}\' >> $rcfile
    done
    
}

# return 1 if some repo config parameters are missing
function verifyRepoConfig() {
    local ret=0
    if [ -z "$path" ] ; then
        echo Path to local git repository is not provided. Use --path to set this value.
        ret=1
    fi
    
    if [ -z "$ver" ] ; then
        echo Version is not defined. Use --version to set this value.
        ret=1
    fi
    
    if [ -z "$repo" ] ; then
        echo Rpository name is not defined. Use --repo to set this value.
        ret=1
    fi
    
    if [ -z "$branch" ] ; then
        echo Branch name is not defined. Use --branch to set this value.
        ret=1
    fi
    
    if [ $ret -ne 0 ] ; then
        echo Usage archive-it --help
    fi
    
    return $ret
}

function updatePatches() {
    repopatches=(${patches[$repo]})
    if [ ! -z "$patchit" ] ; then
        patchdelete=0
        patchadd=1
        repopatchesNew=()
        
        if [ "${patchit:0:1}" == "-" ] ; then
            patchdelete=1
            patchit=${patchit:1}
        fi
        
        for p in ${repopatches[@]}
        do
            if [ "$p" == "$patchit" ] ; then
                patchadd=0
                if [ $patchdelete -ne 0 ] ; then
                    continue
                fi
            fi
            repopatchesNew+=($p)
        done
        if [ $patchadd -ne 0 ] ; then
            repopatchesNew+=($patchit)
        fi
        if [ $patchadd -ne 0 ] || [ $patchdelete -ne 0 ] ; then
            patches[$repo]=${repopatchesNew[@]}
        fi
        
        repopatches=(${patches[$repo]})
    fi
    
}

###############################
### parse command line params #
###############################

while [ ! -z "$1" ] ; do
    case $1 in
        --help)
            help
            return 0 2>/dev/null || exit 0
        ;;
        -y)
            doconfirm=0
        ;;
        --config)
            showConfig
            return 0 2>/dev/null || exit 0
        ;;
        --all)
            doall=1
        ;;
        --patch)
            shift
            patchit=$1
        ;;
        --repo)
            shift
            repo="$1"
        ;;
        --demo)
            demoit=1
        ;;
        --branch)
            shift
            branch="$1"
        ;;
        --path)
            shift
            path=$1
        ;;
        --version)
            shift
            ver=$1
        ;;
        *)
            echo Invalid parameter: $1
            help
            return 0 2>/dev/null || exit 0
        ;;
    esac
    shift
done

#####################################
### Run repository update for $repo #
#####################################

runrepos=($repo)
saveok=1
if [ $doall -ne 0 ] ; then
    saveok=0
    branch=
    ver=
    runrepos=()
    for r in ${!versions[@]} ; do
        runrepos+=($r)
    done
else
    if [ ! -z "$repo" ] ; then
        if [ ! -z "$branch" ] ; then
            branches[$repo]="$branch"
        fi
        if [ ! -z "$ver" ] ; then
            versions[$repo]="$ver"
        fi
        updatePatches
    fi
fi

for r in ${runrepos[@]} ; do
    repo=$r
    
    repopatches=(${patches[$repo]})
    branch=${branches[$repo]}
    ver=${versions[$repo]}
    
    cat <<HHH
Config for repository:
    repo    : $repo
    version : $ver
    path    : $path
    patch   : $patchit
    patches : ${repopatches[@]}
    branch  : $branch
HHH
    
    verifyRepoConfig
    if [ $? -ne 0 ] ; then
        continue
    fi
    
    ###############################
    ### save configuration file ###
    ###############################
    
    if [ $saveok -ne 0 ] ; then
        saveRepoConfig
    fi
    
    ###############################
    ### Continue                ###
    ###############################
    
    if [ $doconfirm -ne 0 ] ; then
        echo -n 'Continue [y/N] : '
        read ans
        if [ "$ans" != "y" ] ; then
            continue
        fi
    fi
    
    if [ $demoit -ne 0 ] ; then
        continue
    fi
    
    ###############################
    ### Get a branch            ###
    ###############################
    
    mydirr=`pwd`
    
    if [ ! -d $path ] ; then
        echo Fatal error: Directory $path seems does not exist
        return 0 2>/dev/null || exit 0
    fi
    if [ ! -d $path/.git ] ; then
        echo Fatal error: Directory $path seems not a git repository
        return 0 2>/dev/null || exit 0
    fi
    
    pushd $path >/dev/null
    git checkout $branch
    if [ $? -ne 0 ] ; then
        echo -n 'git failed. Continue? [y/N] : '
        read ans
        if [ "$ans" != "y" ] ; then
            continue
        fi
    fi
    popd >/dev/null
    
    ###############################
    ### Create patch            ###
    ###############################
    
    if [ ${#repopatches[@]} -ne 0 ] ; then
        echo create a patch for current repo
        
        pushd $path >/dev/null
        vcommit=($(git log $branch --no-decorate -1 | head -1))
        basecommit=${vcommit[1]}
        
        for p in ${repopatches[@]}
        do
            echo generate patch for $p as $mydirr/$p-$repo.patch
            vcommit=($(git log $p --no-decorate -1 | head -1))
            patchcommit=${vcommit[1]}
            git diff $basecommit $patchcommit > $mydirr/$p-$repo.patch
        done
        popd >/dev/null
    fi
    
    ###############################
    ### Create archive          ###
    ###############################
    
    echo Creating tar archive...
    [ -d $product-$ver ] && rm -rf $product-$ver
    mkdir $product-$ver
    
    if [ -e $product-$ver.tar.xz ] ; then
        xz -c -d $product-$ver.tar.xz | tar x
    fi
    
    # get a required branch first
    pushd $path >/dev/null
    git checkout $branch
    popd >/dev/null
    
    pushd $product-$ver >/dev/null
    rsync -avz --exclude='/build/*' --exclude='/.git/*' $path/ .
    [ -d .git ] && rm -rf .git
    [ -d .kdev4 ] && rm -rf .kdev4
    [ -d .vscode ] && rm -rf .vscode
    [ -d build ] && rm -rf build
    [ -e CMakeLists.txt.user ] && rm CMakeLists.txt.user
    popd >/dev/null
    
    tar --exclude-vcs --exclude=/build/* -cf $product-$ver.tar $product-$ver
    
    echo Creating xz archive...
    [ -e $product-$ver.tar.xz ] && rm $product-$ver.tar.xz
    xz -9 -M 20480M -T0 $product-$ver.tar
    
    rm -rf $product-$ver
    
    if [ ! -z "$repo" ] && [ -e $product-$repo.spec ]; then
        sed -i "s/Version:.*/Version:        $ver/" $product-$repo.spec
    fi
done

echo Done.
openSUSE Build Service is sponsored by