SPKGDIR=`pwd`

cd "$SPKGDIR/src"

# Cleanup of of previous installation
if [ -d "$SAGE_LOCAL"/share/jsmol ]; then
    echo "Deleting all files from $SAGE_LOCAL/share/jsmol/..."
    rm -rf "$SAGE_LOCAL"/share/jsmol/*
    echo "Replacing jsmol directory and contents..."
fi

if [ -d "$SAGE_LOCAL"/share/jmol ]; then
    echo "Deleting all files from $SAGE_LOCAL/share/jmol/..."
    rm -rf "$SAGE_LOCAL"/share/jmol/*
    echo "Replacing jmol directory and contents..."
fi

SAGE_DESTDIR_SHARE="$SAGE_DESTDIR_LOCAL/share"

# Installation
# pure javascript for web and java applet in separate /share/jsmol directory.
if [ ! -d "$SAGE_DESTDIR_SHARE"/jsmol ]; then
    echo -n "Directory $SAGE_DESTDIR_SHARE/jsmol does not exist.  "
    echo "Creating directory..."
    mkdir -p "$SAGE_DESTDIR_SHARE"/jsmol
fi

echo "Installing the JSmol files..."
cp -r jsmol/* "$SAGE_DESTDIR_SHARE"/jsmol/ || \
    sdh_die "Error installing jmol files. Exiting."
#
# Jsmol is stuck inside the jmol directory, remove
rm -r "jsmol" || sdh_die "Error removing the contained jsmol files. Exiting."

# "install" the Jmol files...
if [ ! -d "$SAGE_DESTDIR_SHARE"/jmol ]; then
    echo -n "Directory $SAGE_DESTDIR_SHARE/jmol does not exist.  "
    echo "Creating directory..."
    mkdir -p "$SAGE_DESTDIR_SHARE/jmol"
fi

echo "Installing the Jmol files..."
cp -r * "$SAGE_DESTDIR_SHARE"/jmol/ || \
    sdh_die "Error installing jmol files. Exiting."

if [ ! -d "$SAGE_DESTDIR_LOCAL"/bin ]; then
    mkdir -p "$SAGE_DESTDIR_LOCAL"/bin
fi

echo "Install the Jmol launcher to bin/..."
cp -f "$SPKGDIR"/jmol "$SAGE_DESTDIR_LOCAL"/bin/jmol && \
    chmod ugo+x "$SAGE_DESTDIR_LOCAL"/bin/jmol || \
    sdh_die "Error installing the jmol launcher. Exiting."

echo "Installing applet web directory"
mkdir -p "$SAGE_DESTDIR_SHARE"/jmol/appletweb
cp -r "$SPKGDIR"/appletweb/* "$SAGE_DESTDIR_SHARE"/jmol/appletweb/ || \
    sdh_die "Error installing the applet web directory"

echo "New Jmol installed successfully."
