#!/usr/bin/ksh

typeset def_local_OPT_vars=0
[[ -n $1 ]] && def_local_OPT_vars=1

function foo
{
    typeset opt
    if (( def_local_OPT_vars )); then
        typeset OPTIND OPTARG
    fi

    while getopts hi: opt; do
        case $opt in
            h) echo "[${.sh.fun}] -$opt" ;;
            i) echo "[${.sh.fun}] -$opt $OPTARG" ;;
        esac
    done
    shift $((OPTIND - 1))

    echo "[${.sh.fun}] $*"
}

typeset opt
set - -a a-main -b -c c-main main1 main2
while getopts a:bc: opt; do
    case $opt in
        [ac]) echo "[main] -$opt $OPTARG"
              foo -h -i i-foo foo1 foo2
              ;;
        b)    echo "[main] -$opt"
              foo -h -i i-foo foo1 foo2
              ;;
    esac
done
shift $((OPTIND - 1))
echo "[main] $*"
