#!/bin/bash
# Copyright (c) 2008, 2009 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.
#
# The purpose of this script is to provide a wrapper around the swi utility
# that can be run when not chrooted into a workspace when the workspace is on a
# machine that does not have a version of python installed capable of running
# the swi utility.  Hopefully by running
# "/path/to/workspace/usr/bin/swi-wrapper chroot" a developer can get themselves
# chrooted into the workspace.
looksLikeRoot() {
   if [ -d $1/lib ] && [ -d $1/usr/bin ]; then
      return 0   
   fi
   return 1
}

GUESS=$(dirname $0)/../..
if looksLikeRoot ${GUESS}; then
   export PREFIX=${GUESS}
else
   # This could be improved to do something smarter.   
   GUESS=`pwd`
   echo "Failed to guess path to rootfs; falling back to pwd (${GUESS})"
   if looksLikeRoot ${GUESS}; then
      export PREFIX=${GUESS}
   else
      echo "${GUESS} was also a bad guess; giving up..."
      exit 1
   fi
fi
export PATH=${PREFIX}/bin:${PREFIX}/usr/bin:${PREFIX}/sbin:${PREFIX}/usr/sbin
export LD_LIBRARY_PATH=${PREFIX}/lib:${PREFIX}/usr/lib
export PYTHONPATH=${PREFIX}/usr/lib/python2.5:${PREFIX}/usr/lib/python2.5/site-packages
export EXECPREFIX=${PREFIX}/lib/ld-linux.so.2
${EXECPREFIX} ${PREFIX}/usr/bin/python ${PREFIX}/usr/bin/swi $*
