#!/bin/sh 
#
# add_app.sh - bundle up a compound BOINC application 
#
# Run this script from the directory in which you built your BOINC
# application to have it automatically copy the latest version of
# the executable to the correct PROJECT/apps subdirectory in order
# to add/update the application.  You then run update_versions.
#
# List on the command line any extra files which are to be sent along
# with the workunit in the application bundle.
#
# In the case of a more complidated app bundle you can either modify
# this script or do the steps 'by hand' with this as reference.
#
# Eric Myers <myers@spy-hill.net>  - 
# @(#) $Id: add_app.sh,v 1.11 2006/11/13 23:13:55 myers Exp $
########################################################################

PROJECT=${BOINC_PROJECT-"/usr02/pirates"}
export BOINC_PROJECT=$PROJECT

# Figure out the application name from the current directory, which
# should be of the form appname.d  (.d means directory in old SYSV Unix)

HERE=$PWD
# echo "We are here: $HERE"
APP=`basename $HERE .d`
echo Application name is $APP

PROJ_APPS_DIR=$PROJECT/apps/$APP

##
# Extra files to go in the bundle.  These could be image files, libraries, 
# or any other files required by the app.   The list should be on the
# command line.  Don't include app.so graphics thread, as we check for
# that separately below.

EXTRAS="$*"

for FILE in $EXTRAS
do
  if [ ! -f $FILE ]; then 
    echo "File $FILE does not exist."
    exit 1
  fi
done


####################
# Windows Application: find the most recent version here

LIST=`ls -1r ${APP}_5.??_*.exe`
if [ "x$LIST" != "x" ]; then
  set $LIST 
  EXE=$1
  BASE=`basename $EXE .exe `

  # Make the directory

  APP_DIR=${PROJ_APPS_DIR}/$EXE
  mkdir -p $APP_DIR

  # put executable in the directory

  chmod +x $EXE
  cp $EXE $APP_DIR

  # Debugging symbol file, if present

  PDB="${BASE}.pdb"
  if [ -f $PDB ]; then
     cp $PDB $APP_DIR
  fi

  if [ ! -z "$EXTRAS" ]; then 
    cp $EXTRAS $APP_DIR
  fi
fi


####################
# Linux App:

LIST=`ls -1r ${APP}_5.??_*linux-gnu`
if [ "x$LIST" != "x" ]; then
  set $LIST 
  EXE=$1

  # Make the PROJECT/app directory

  APP_DIR=${PROJ_APPS_DIR}/$EXE
  mkdir -p $APP_DIR

  # put executable in the directory

  cp $EXE $APP_DIR

  # Graphics thread as separate dynamical library (.so file)

  DYN="${EXE}.so"
  if [ -f $DYN ]; then
     cp $DYN $APP_DIR
  fi

  # any extra files

  if [ ! -z "$EXTRAS" ]; then 
    cp $EXTRAS $APP_DIR
  fi
fi


####################
# Apple App:

LIST=`ls -1r ${APP}_5.??_powerpc-apple-darwin`
if [ "x$LIST" != "x" ]; then
  set $LIST 
  EXE=$1

  # Make the directory

  APP_DIR=${PROJ_APPS_DIR}/$EXE
  mkdir -p $APP_DIR

  # put executable in the directory

  cp $EXE $APP_DIR

  if [ ! -z "$EXTRAS" ]; then 
    cp $EXTRAS $APP_DIR
  fi
fi

exit

##
