#!/bin/bash
#  Wrapper script to launch winefontcfg
#

# Locate where our root is installed by looking for the directory
# where this this script is located, unwinding symlinks on the way
locate_pic_root()
{
    if [ -z "$PIC_ROOT" ]
    then
        argv0=`which "$0"`
        [ -z "$argv0" ] && argv0="$0"
        echo "$argv0" | egrep "^/" >/dev/null
        if [ $? -ne 0 ]
        then
            argv0="`pwd`/$argv0"
        fi
        dir=`dirname "$argv0"`
        while [ ! -x "$dir/../wine/bin/wine" ]
        do
            [ ! -h "$argv0" ] && break
            argv0=`ls -l "$argv0" | sed -e 's/^.*-> //'`
            echo "$argv0" | egrep "^/" >/dev/null
            if [ $? -ne 0 ]
            then
                argv0="$dir/$argv0"
            fi
            dir=`dirname "$argv0"`
        done
        dir=`echo "$dir" | sed -e 's%\(/\.\)*$%%' -e 's%\(/\./\(\./\)*\)%/%'`
        PIC_ROOT="$dir/.."
        PIC_BINDIR="$dir"
    fi
    if [ ! -x "$PIC_ROOT/wine/bin/wine" ]
    then
        if [ "$1" = "--no-fail" ]
        then
            PIC_ROOT=""
            return 1
        fi
        echo "`basename \"$0\"`:error: could not find wine/bin/wine in '$PIC_ROOT'" >&2
        exit 1
    fi
    # Make sure it's exported
    export PIC_ROOT
    export PIC_BINDIR
    return 0
}

locate_pic_root

exec "$PIC_BINDIR"/wrapper winefontcfg.exe.so "$@"
