#!/bin/bash

GBS_ROOT_I686_0=~/GBS-ROOT/local/BUILD-ROOTS/scratch.i586.0
GBS_ROOT_ARMV7L_0=~/GBS-ROOT/local/BUILD-ROOTS/scratch.armv7l.0
GBS_ROOT_RISCV_0=~/GBS-ROOT/local/BUILD-ROOTS/scratch.riscv64.0
GBS_RPMS_I686=~/GBS-ROOT/local/repos/tizen/i586/RPMS
GBS_SRPMS_I686=~/GBS-ROOT/local/repos/tizen/i586/SRPMS
GBS_RPMS_ARMV7L=~/GBS-ROOT/local/repos/tizen/armv7l/RPMS
GBS_SRPMS_ARMV7L=~/GBS-ROOT/local/repos/tizen/armv7l/SRPMS
GBS_RPMS_RISCV=~/GBS-ROOT/local/repos/tizen_riscv64/riscv64/RPMS
GBS_SRPMS_RISCV=~/GBS-ROOT/local/repos/tizen_riscv64/riscv64/SRPMS
GBS_LOCAL_REPOS=~/GBS-ROOT/local/repos
# define default value for GBS_REPOURL if it is null or not defined
: ${GBS_REPOURL:='http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Base/latest/repos/standard/packages/'}

: ${GBS_REPOURL_UNIFIED:='http://download.tizen.org/releases/milestone/TIZEN/Tizen/Tizen-Unified/latest/repos/standard/packages/'}
#: ${GBS_REPOURL:='http://download.tizen.org/releases/2.2/latest'}

#assert git packages under current dir are clean
function AssertClean()
{
    find . -maxdepth 1 -type d| sed "1d"| xargs -i sh -c "cd {};git status| grep -E 'working tree|directory clean'"
}
#assert git package clean
# such as: AssertCleanWith fake sw-tools
function AssertCleanWith()
{
    echo $*| xargs -n1| xargs -i sh -c "cd {} && git status| grep -E 'working tree|directory clean'"
}

function AssertRPMsI()
{
    find . -maxdepth 1 -type d| sed -e "1d" -e "s|./||g"| xargs -i sh -c "ls ${GBS_RPMS_I686}| grep {}" && \
    find . -maxdepth 1 -type d| sed -e "1d" -e "s|./||g"| xargs -i sh -c "ls ${GBS_SRPMS_I686}| grep {}"
}

function AssertRPMsIWith()
{
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_RPMS_I686}| grep {}" && \
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_SRPMS_I686}| grep {}"
}

function AssertRPMsIWithout()
{
    ! AssertRPMsIWith $*
}

function AssertRPMsA()
{
    find . -maxdepth 1 -type d|sed -e "1d" -e "s|./||g"|xargs -i sh -c "ls ${GBS_RPMS_ARMV7L}| grep {}" && \
    find . -maxdepth 1 -type d|sed -e "1d" -e "s|./||g"|xargs -i sh -c "ls ${GBS_SRPMS_ARMV7L}| grep {}"
}

function AssertRPMsAWith()
{
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_RPMS_ARMV7L}| grep {}" && \
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_SRPMS_ARMV7L}| grep {}"
}

function AssertRPMsAWithout()
{
    ! AssertRPMsAWith $*
}
#Assert build root scratch.i686.0 with rpm xxx installed
#later we can AssertRootI1With AssertRootI2With AssertRootI3With...
function AssertRootI0With()
{
    echo $*| xargs -n1| xargs -I{} sh -c "sudo chroot $GBS_ROOT_I686_0 rpm -q {}"
}
#Assert build root scratch.armv7l.0 with rpm xxx installed
#later we can AssertRootA1With AssertRootA2With AssertRootA3With...
function AssertRootA0With()
{
    echo $*| xargs -n1| xargs -I{} sh -c "sudo chroot $GBS_ROOT_ARMV7L_0 rpm -q {}"
}

function AssertRPMsR()
{
    find . -maxdepth 1 -type d|sed -e "1d" -e "s|./||g"|xargs -i sh -c "ls ${GBS_RPMS_RISCV}| grep {}" && \
    find . -maxdepth 1 -type d|sed -e "1d" -e "s|./||g"|xargs -i sh -c "ls ${GBS_SRPMS_RISCV}| grep {}"
}

function AssertRPMsRWith()
{
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_RPMS_RISCV}| grep {}" && \
    echo $*|xargs -n1| xargs -I{} sh -c "ls ${GBS_SRPMS_RISCV}| grep {}"
}

function AssertRPMsRWithout()
{
    ! AssertRPMsRWith $*
}
