#!/bin/bash 
specdir=/usr/share/all-spec
#[ -f ./spgrep ] && specdir=$(realpath ./)
unset SPEC
unset REPO
unset PLATFORM
unset PATTERN
unset BEGIN 
unset LIST
unset ABF

HLP() {
    echo "
$(basename $0) - util to find in Rosa linux rpm specs database

Usage:
    $(basename $0) %build                     - find str '%build' in all installed spec databases
    $(basename $0) bcond_with -p 21 -r main   - find str 'bcond_with' in rosa2021.1 main repository
    $(basename $0) --list                     - list of all projects found in installed specs databases
    $(basename $0) --list -p 23 -r contrib    - list of projects of contrib repo rosa2023.1 platform
    
Parameters:
    -h | --help                   - this help
    -p | --platform               - platform (21, 2021, rosa2021.1 is same)
    -s | --spec                   - show only specified  spec file
    -r | --repo                   - reposytory (main, contrib, nonfree)
    -l | --list                   - show list of all projects found with -p, and -r patameters
    -a | --abf                    - show spec from abf, not from local specs database (with -s only)
"
    exit ${LINENO}
}

[ $1 ] || HLP

while [ $1 ] ; do
  case "$1" in
    "-h" | "--help" ) HLP ;;
    "-p" | "--platform" ) shift ; PLATFORM="$1";;
    "-s" | "--spec" ) shift; SPEC="$1";;
    "-r" | "--repo" ) shift; REPO="$1";;
    "-l" | "--list") LIST='y' ;;
    "-a" | "--abf" ) ABF='y' ;;
    "-"*[A-Za-z]*) echo "$(basename "$0"): invalid option -- '$(echo $1 | tr -d '-')'" >&2; exit 1;;
    *)  PATTERN="$1";;
  esac
  shift
done

if [ "$PLATFORM" ] ; then
    case $PLATFORM in
    "23" | "2023.1" | "rosa2023.1" ) PLATFORM=23 
				     PNAME_FULL=rosa2023.1 ;;
    "21" | "2021.1" | "rosa2021.1" ) PLATFORM=21 
				     PNAME_FULL=rosa2021.1 ;;
    "16" | "2016.1" | "rosa2016.1" ) PLATFORM=16 
				     PNAME_FULL=rosa2016.1 ;;
    "12" | "2012.1" | "rosa2012.1" ) PLATFORM=12
				    PNAME_FULL=rosa2012.1 ;;
    "13" | "rosa13" ) 	PLATFORM=13
			PNAME_FULL=rosa13 ;;
    *)	echo "Unknown platform $PLATFORM"
	exit ${LINENO} ;;
    esac
    BEGIN=$PLATFORM
else
    PLATFORM='*'
    BEGIN='.*'
    PNAME_FULL="rosa$(rpm --eval %rosa_release)"
fi

if [ "$REPO" ] ; then
    case $REPO in
    "main" | "mr" ) REPO='mr' ;;
    "contrib" | "cr" ) REPO='cr' ;;
    "nonfree" | "non-free" | "nr" ) REPO='nr' ;;
    esac
else
    REPO='.*'
fi


show_spec(){
    grep -shE '^'"${BEGIN}${REPO}"'\|'${SPEC}'-[[:digit:]].*[[:digit:]]\|' ${specdir}/all_spec_${PLATFORM} |cut -f2- | less
    exit
}

show_spec_net(){
    wget -q https://abf.io/import/${SPEC}/raw/$PNAME_FULL/${SPEC}.spec -O - | less
    exit
}

list(){
    grep -shE '^'"${BEGIN}${REPO}" ${specdir}/all_spec_${PLATFORM} 2>/dev/null |cut -f2 -d '|' |sed 's/\-[[:digit:]]\+.*$//' |sort -u
    exit
}

find_str(){
    grep -shE '^'"${BEGIN}${REPO}".*"$PATTERN" ${specdir}/all_spec_${PLATFORM} | less
    exit
}

[ ! $SPEC ] && [ ! $LIST ] && find_str $PATTERN
[ $LIST ] && list
[ $ABF ] && show_spec_net
show_spec

