#!/bin/sh

#
# Commands executed by this script will log evey file they open.
# 
# This script mainly serves to show how to use the libtricks
# functionality.

LOGFILE=`pwd`/trick-open.log

help ()
{
    echo "trick-logopen: log all open() calls of files"
    echo "  Usage: trick-open [--file logfilename] [--libpath path] [command]"
    echo "  by default logging goes to ./trick-open.log"
}


while true; do
  case $1 in
    --file)
      shift
      LOGFILE="$1"
      ;;
    --libpath)
      shift
      LIBPATH="$1"
      ;;
    --help)
      help
      exit 0
      ;;
    --version)
      echo trick-chroot, version 0.2
      exit 0
      ;;
    *)
      break;
      ;;
  esac
  shift
done

LD_PRELOAD=${LIBPATH:-/usr/lib/libtricks}/libtricks.so.0

VPATH="writefile(\"$LOGFILE\",\"open(\\\"\$name\\\", mode=\$need), PID=\$PID, command=\$cmd\")=:"

# If you only want to see files in /tmp, add 'check(\"/tmp\")&' to the
# beginning of the VPATH.

# If you only want to see files that are opened with 'mode' exec (i.e.,
# binaries that are executed), add 'needexec' to the beginning of the 
# VPATH. 

# And so on, see /usr/doc/libtricks/README for loads more possibilities.

if test -z "$*"; then
  export LD_PRELOAD VPATH
  /bin/sh
else
  export LD_PRELOAD VPATH
  "$@"

fi

