#!/bin/bash

# Mostly (C) fbriere.net
# Some parts (C) 2005 Ryan Schultz

# PCSX expects to be installed in the user's home directory, so we build
# a nice little doghouse under ~/.pcsx for it to live in.
#
# This script will populate ~/.pcsx with symlinks to the plugins
# installed on the machine.  You can add other files in there, but do not
# meddle with the symlinks; if you replace a symlink with a file, it will
# eventually get overwritten.  See README.Debian for more details.

set -e

# Store keyboard repeat settings that PCSX may break
# will equal 'on' or 'off'
REPEAT=`xset q | grep 'auto repeat' | head -n 1 | awk '{print $3}'`

# Create the doghouse
mkdir --parents ~/.pcsx
cd ~/.pcsx
mkdir --parents Bios Langs cfg memcards Plugin sstates .pixmaps

# Flush dangling symlinks
find Bios Plugin cfg .pixmaps -type l -exec test ! -r '{}' \; \
	-exec echo Deleting '{}' \; \
	-exec rm -f '{}' \;

# Populate the doghouse
lndir -silent /usr/lib/games/psemu/lib		Plugin
lndir -silent /usr/lib/games/psemu/config	cfg

# Add any local plugins
[ -d /usr/local/share/games/psx-bios ] && \
	lndir -silent /usr/local/share/games/psx-bios		Bios
[ -d /usr/local/lib/games/psemu/lib ] && \
	lndir -silent /usr/local/lib/games/psemu/lib	Plugin
[ -d /usr/local/lib/games/psemu-plugins/config ] && \
	lndir -silent /usr/local/lib/games/psemu/config	cfg

# Copy the pixmaps in the About dialog
lndir -silent /usr/share/games/pcsx/pixmaps	.pixmaps

# Copy the languages for the Language menu
lndir -silent /usr/share/games/pcsx/langs	Langs

# Now we're ready to launch PCSX
/usr/games/pcsx.real "$@"

# Restore keyboard repeat settings
if [[ "$REPEAT"=="on" ]]; then
	xset r on
else
	xset r off
fi

