#!/bin/bash
#
#  Helper script for completion, usage with tcsh:
#
#     complete osc 'p@*@`osc.complete`@'
#
#  usage with bash
#
#     complete -C osc.complete osc
#
#  Author: Werner Fink <werner@suse.de>
#

set -o noclobber
shopt -s extglob
typeset -i last
typeset -i count

test "/proc/$PPID/exe" -ef /bin/tcsh || COMMAND_LINE="$COMP_LINE"
cmdline=($COMMAND_LINE)
test "${cmdline[0]}" != "osc" && exit 1

let last=${#COMMAND_LINE}
let last--
let count=${#cmdline[@]}
let count--
test "${COMMAND_LINE:$last}" = " " && let count++
unset last

osccmds=(abortbuild add addremove ar aggregatepac api branch build buildconfig \
    buildhistory buildhist buildinfo buildlog bl cat changedevelrequest changedevelreq \
    cr checkout co commit checkin ci copypac delete del remove rm deleterequest \
    deletereq dr diff di getbinaries help importsrcpkg info init jobhistory jobhist \
    linkpac list ls log maintainer man meta mkpac platforms prjresults pr rdelete \
    rdiff rebuild rebuildpac remotebuildlog rbl rbuildlog repairlink repos repourls \
    request rq resolved results r search setlinkrev status st submitrequest sr \
    submitreq update up updatepacmetafromspec metafromspec vc wipebinaries)
oscreq=(list log show accept decline delete revoke wipe)

oscprj=""
oscpkg=""
lnkprj=""
lnkpkg=""
test -s ${PWD}/.osc/_project && read -t 1 oscprj < ${PWD}/.osc/_project
test -s ${PWD}/.osc/_package && read -t 1 oscpkg < ${PWD}/.osc/_package
if test -s ${PWD}/.osc/_files ; then
    lnkprj=$(command sed -rn '/<linkinfo/{s@.*[[:blank:]]project="([^"]+)".*@\1@p;}' < ${PWD}/.osc/_files)
    lnkpkg=$(command sed -rn '/<linkinfo/{s@.*[[:blank:]]package="([^"]+)".*@\1@p;}' < ${PWD}/.osc/_files)
fi

if test -s ~/.osc.projects ; then
    typeset -i ctime=$(command date -d "$(command stat -c '%z' ~/.osc.projects)" +'%s')
    typeset -i   now=$(command date -d now +'%s')
    ((now - ctime > 86400)) && command osc ls >| ~/.osc.projects
else
    command osc ls >| ~/.osc.projects
fi

submit ()
{
    local -i pos=$1
    local -i off=$2
    local target

    if ((pos == 1)) ; then
        if test -z "${cmdline[$((2+off))]}" -a -n "${oscprj}" ; then
            builtin compgen -W "${oscprj}"
        else
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[$((2+off))]}"
        fi
    fi
    if ((pos == 2)) ; then
        if test -z "${cmdline[$((3+off))]}" -a -n "${oscpkg}" ; then
            builtin compgen -W "${oscpkg}"
        else
            builtin compgen -W "$(command osc ls "${cmdline[$((2+off))]}")" -- "${cmdline[$((3+off))]}"
        fi
    fi
    if ((pos == 3)) ; then
        if test -z "${cmdline[$((4+off))]}" -a -n "${lnkprj}" ; then
            builtin compgen -W "${lnkprj}"
        else
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[$((4+off))]}"
        fi
    fi
    if ((pos == 4)) ; then
        target="${lnkpkg}"
        target="${target:+$target }$oscpkg"
        if test -n "${target}" ; then
            builtin compgen -W "${target}" -- "${cmdline[$((5+off))]}"
        else
            builtin compgen -W "$(command osc ls "${cmdline[$((4+off))]}")" -- "${cmdline[$((5+off))]}"
        fi
    fi
}

case "${cmdline[1]}" in
add|addremove|ar)
    if ((count == 1)) ; then
        builtin compgen -W 'add addremove ar' -- "${cmdline[1]}"
    else
        for x in $(builtin compgen -f -X '.osc' -- "${cmdline[2]}"); do
            test -d $x && builtin echo $x/ || builtin echo $x
        done
    fi
    ;;
branch)
    if ((count == 1)) ; then
        builtin compgen -W 'branch' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    --nodevelproject)
        if ((count == 3)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[3]}"
        fi
        if ((count == 4)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[3]}")" -- "${cmdline[4]}"
        fi
        ;;
    -*) builtin compgen -W '--nodevelproject' -- "${cmdline[3]}" ;;
    *)
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
    esac
    ;;
list|ls)
    if ((count == 1)) ; then
        builtin compgen -W 'list ls' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
    fi
    if ((count == 3)) ; then
        builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
    fi
    ;;
sr|submitreq|submitrequest)
    if ((count == 1)) ; then
        builtin compgen -W 'sr submitreq submitrequest' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    --nodevelproject)
            submit $((count - 2)) 1 ;;
    -*) builtin compgen -W '--nodevelproject' -- "${cmdline[2]}" ;;
    *)  submit $((count - 1)) 0 ;;
    esac
    ;;
rq|request)
    if ((count == 1)) ; then
        builtin compgen -W 'rq request' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    accept|decline|wipe|revoke|log)
        if ((count == 3)) ; then
            builtin echo -n 'ID'
        fi
        ;;
    show)
        if ((count == 3)) ; then
            builtin compgen -W '--diff' -- "${cmdline[3]}"
        else
            builtin echo -n 'ID'
        fi
        ;;
    list)
        if ((count == 3)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[3]}"
        fi
        if ((count == 4)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[3]}")" -- "${cmdline[4]}"
        fi
        ;;
    *)
        ((count == 2)) && builtin compgen -W "$(builtin echo ${oscreq[@]})" -- "${cmdline[2]}"
    esac
    ;;
copypac|linkpac)
    if ((count == 1)) ; then
        builtin compgen -W 'copypac linkpac' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    *)
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
        if ((count == 4)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[4]}"
        fi
        if ((count == 5)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[4]}")" -- "${cmdline[5]}"
        fi
    esac
    ;;
deleterequest|deletereq|dr)
    if ((count == 1)) ; then
        builtin compgen -W 'deleterequest deletereq dr' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    *)
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
    esac
    ;;
changedevelrequest|changedevelreq|cr)
    if ((count == 1)) ; then
        builtin compgen -W 'changedevelrequest changedevelreq cr' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    *)
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
        if ((count == 4)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[4]}"
        fi
        if ((count == 5)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[4]}")" -- "${cmdline[5]}"
        fi
    esac
    ;;
rdiff)
    if ((count == 1)) ; then
        builtin compgen -W 'rdiff' -- "${cmdline[1]}"
    fi
    case "${cmdline[2]}" in
    *)
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
        if ((count == 4)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -P --oldprj= -- "${cmdline[4]#*=}"
        fi
        if ((count == 5)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[4]#*=}")" -P --oldpkg= -- "${cmdline[5]#*=}"
        fi
    esac
    ;;
ci|commit|checkin)
    if ((count == 1)) ; then
        builtin compgen -W 'ci commit checkin' -- "${cmdline[1]}"
    else
        for x in $(builtin compgen -f -X '.osc' -- "${cmdline[2]}"); do
            test -d $x && builtin echo $x/ || builtin echo $x
        done
    fi
    ;;
co|checkout)
    if ((count == 1)) ; then
        builtin compgen -W 'co copypac checkout' -- "${cmdline[1]}"
    else
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
    fi
    ;;
maintainer)
    if ((count == 1)) ; then
        builtin compgen -W 'maintainer' -- "${cmdline[1]}"
    else
        if ((count == 2)) ; then
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
        fi
        if ((count == 3)) ; then
            builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
        fi
    fi
    ;;
up|update)
    if ((count == 1)) ; then
        builtin compgen -W 'up update' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin  compgen -W '--expand-link --unexpand-link' -- "${cmdline[2]}"
    fi
    ;;
meta)
    if ((count == 1)) ; then
        builtin compgen -W 'meta metafromspec' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin compgen -W 'prj pkg' -- "${cmdline[2]}"
    fi
    if ((count == 3)) ; then
        builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[3]}"
    fi
    if ((count == 4)) && test "${cmdline[2]}" = pkg ; then
        builtin compgen -W "$(command osc ls "${cmdline[3]}")" -- "${cmdline[4]}"
    fi
    if ((count == 5)) ; then
        builtin compgen -W '--edit -e' -- "${cmdline[5]}"
    fi
    ;;
wipebinaries)
    if ((count == 1)) ; then
        builtin compgen -W 'wipebinaries' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
    fi
    if ((count == 3)) ; then
        builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
    fi
    if ((count == 4)) ; then
        builtin compgen -W "--expansion --broken --build-failed --build-disabled" -- "${cmdline[4]}"
    fi
    ;;
help)
    if ((count == 1)) ; then
        builtin compgen -W 'help' -- "${cmdline[1]}"
    fi
    ((count == 2)) && builtin compgen -W "$(builtin echo ${osccmds[@]})" -- "${cmdline[2]}"
    ;;
search)
    if ((count == 1)) ; then
        builtin compgen -W 'search' -- "${cmdline[1]}"
    else
        oscsearch="--help --csv -i --involved -v --verbose --description --title \
                   --project --package -e --exact --repos-baseurl"
        builtin compgen -W "${oscsearch}" -- "${cmdline[$count]}"
    fi
    ;;
pr|prjresults)
    if ((count == 1)) ; then
        builtin compgen -W 'pr prjresults' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
    fi
    if ((count > 2)) ; then
        case "${cmdline[$((count-1))]}" in
        -n|--name-filter*)
            builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[$count]}"
            ;;
        -s|--status-filter*)
            OIFS="$IFS"; IFS=:
            builtin compgen -W 'disabled:failed:finished:building:succeeded:broken:scheduled:expansion error:blocked' -- "${cmdline[$count]}"
            IFS="$OIFS"
            ;;
        -p|--project*)
            builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[$count]}"
            ;;
        *)
            builtin compgen -W "-n --name-filter -s --status-filter -c --csv -q --hide-legend" -- "${cmdline[$count]}"
            ;;
        esac
    fi
    ;;
r|results)
    if ((count == 1)) ; then
        builtin compgen -W 'r results' -- "${cmdline[1]}"
    fi
    if ((count == 2)) ; then
        builtin compgen -W "$(command cat ~/.osc.projects)" -- "${cmdline[2]}"
    fi
    if ((count == 3)) ; then
        builtin compgen -W "$(command osc ls "${cmdline[2]}")" -- "${cmdline[3]}"
    fi
    if ((count > 3)) ; then
         builtin compgen -W "-r --repo -a --arch -l --last-build --xml" -- "${cmdline[$count]}"
    fi
    ;;
*)
    ((count == 1)) && builtin compgen -W "$(builtin echo ${osccmds[@]})" -- "${cmdline[1]}"
esac