if [ "$SAGE_ROOT" = "" ]; then
   echo "SAGE_ROOT undefined ... exiting";
   echo "Maybe run 'sage -sh'?"
   exit 1
fi

SPKGDIR=`pwd`
echo "Temporary package directory: $SPKGDIR"

# Patches.  Comment out this section if there are none.
if [ ! -d "$SPKGDIR"/patches ]; then
   echo "Error finding patches directory. Exiting."
   exit 1
fi

cd "$SPKGDIR/src"

#pure javascript for web and java applet in separate /share/jsmol directory.
if [ ! -d "$SAGE_LOCAL"/share/jsmol ]; then
    echo "Directory "$SAGE_LOCAL"/share/jsmol does not exist.  Creating directory..."
    mkdir -p "$SAGE_LOCAL"/share/jsmol
else
    echo "Deleting all files from "$SAGE_LOCAL"/share/jsmol/..."
    rm -r "$SAGE_LOCAL"/share/jsmol/*
    echo "Replacing jsmol directory and contents..."
fi

echo "Installing the JSmol files..."
cp -r jsmol/* "$SAGE_LOCAL"/share/jsmol/
if [ $? -ne 0 ]; then
   echo "Error installing jmol files. Exiting."
   exit 1
fi

# "install" the Jmol files...
if [ ! -d "$SAGE_LOCAL"/share/jmol ]; then
    echo "Directory "$SAGE_LOCAL"/share/jmol does not exist.  Creating directory..."
    mkdir -p "$SAGE_LOCAL/share/jmol"
else
    echo "Deleting all files from "$SAGE_LOCAL"/share/jmol/..."
    rm -r "$SAGE_LOCAL"/share/jmol/*
    echo "Replacing jmol directory and contents..."
fi

# Jsmol is stuck inside the jmol directory, remove
rm -r "jsmol"
if [ $? -ne 0 ]; then
   echo "Error removing the contained jsmol files. Exiting."
   exit 1
fi

echo "Installing the Jmol files..."
cp -r * "$SAGE_LOCAL"/share/jmol/
if [ $? -ne 0 ]; then
   echo "Error installing jmol files. Exiting."
   exit 1
fi

echo "Install the Jmol launcher to bin/..."
cp -f jmol "$SAGE_LOCAL"/bin/jmol && chmod ugo+x "$SAGE_LOCAL"/bin/jmol
if [ $? -ne 0 ]; then
   echo "Error installing the jmol launcher. Exiting."
   exit 1
fi

echo "Installing applet web directory"
mkdir -p "$SAGE_LOCAL"/share/jmol/appletweb
cp -r "$SPKGDIR"/patches/appletweb/* "$SAGE_LOCAL"/share/jmol/appletweb/
if [ $? -ne 0 ]; then
   echo "Error installing the applet web directory"
   exit 1
fi

if [ $? -ne 0 ]; then
   echo "Error installing Jmol."
   exit 1
else
   echo "New Jmol installed successfully."
fi
