#!/bin/sh
# $FreeBSD: ports/emulators/linux-ePSXe/files/epsxe-wrapper.in,v 1.1 2005/07/16 15:05:22 jylefort Exp $

EPSXE=/usr/local/libexec/epsxe
DATADIR=/usr/local/share/epsxe
PSEMUPRO_PLUGINS_DIR=/usr/local/lib/psemupro/plugins
PSEMUPRO_CFGBINS_DIR=/usr/local/libexec/psemupro/cfg
PSEMUPRO_CFGFILES_DIR=/usr/local/share/psemupro/cfg

USERDIR=$HOME/.epsxe

remove_stale_symlinks () {
    dir="$1"
    
    for f in $dir/*; do
	[ "x$f" = "x$dir/*" ] && break

	target=`readlink $f`
	if [ -n "$target" ] && [ ! -e "$target" ]; then
	    rm -f $f
	fi
    done
}

### create $USERDIR hierarchy

for d in bios cfg cheats memcards patches plugins snap sstates; do
    mkdir -p $USERDIR/$d || exit 1
done

### remove stale symlinks

for d in plugins cfg; do
    remove_stale_symlinks $USERDIR/$d || exit 1
done

### populate $USERDIR

ln -sf $EPSXE $USERDIR/epsxe || exit 1

for f in keycodes.lst cheats/breath_of_fire_4_usa.cht \
    cheats/chrono_cross_ntsc.cht cheats/tarzan_pal.cht; do
    ln -sf $DATADIR/$f $USERDIR/$f || exit 1
done

ln -sf $PSEMUPRO_PLUGINS_DIR/* $USERDIR/plugins 2>/dev/null
ln -sf $PSEMUPRO_CFGBINS_DIR/* $USERDIR/cfg 2>/dev/null
cp -n $PSEMUPRO_CFGFILES_DIR/* $USERDIR/cfg 2>/dev/null
chmod u+w $USERDIR/cfg/* 2>/dev/null

### run the emulator

cd $USERDIR || exit 1
exec ./epsxe "$@"
