#!/bin/sh

# Setup variables
prefix=/usr
exec_prefix=/usr
bindir=/usr/bin
datarootdir=${prefix}/share
datadir=/usr/share
homedata=$HOME/.sear

WFUT_BIN=wfut
MEDIA_CHANNEL="sear-media-0.7"
UPDATED=0

# Create dir if required 
if [ ! -d $homedata ] ; then
  echo "Creating sear configuration directory..."
  mkdir $homedata
fi

## First check to see if a native compile of wfut exists
if [ "$UPDATED" -eq "0" ] ; then
  # Find the wfut binary found in path first
  WFUT_BIN="`which wfut 2>/dev/null`"

  if test -z "$WFUT_BIN"; then
    # wfut might be installed with the same prefix as sear...
    if test -x $bindir/wfut; then
      WFUT_BIN="$bindir/wfut"
    else
      echo "Could not find the 'wfut' binary. Update will not be run"
    fi
  fi

  if test -n "$WFUT_BIN"; then
    # We need to change here so updater knows where to find / store data
    pushd $homedata
    # Update
    if [ -e  $datadir/sear/$MEDIA_CHANNEL/wfut.xml ]; then
      $WFUT_BIN -S $datadir/sear/$MEDIA_CHANNEL -u $MEDIA_CHANNEL
    else
      $WFUT_BIN -u$MEDIA_CHANNEL
    fi
    # Restore dir
    popd
    UPDATED=1
  fi
fi


if [ "$UPDATED" -eq "0" ] ; then
   echo "Error running WFUT. No native 'wfut' found on path."
fi

# Execute real sear binary
echo "Starting Sear...."
$bindir/sear-bin

