#!/bin/sh
# You may change these default locations for your system by editing this file
# as root/sudo

# Location where you want olex2 to live per user
OLEX2_DEFAULT_DIR=$HOME/olex2

# Location where you installed CCTBX
OLEX2_CCTBX_DEFAULT_DIR=/usr/local/cctbx

# Location where the CCTBX library is kept
CCTBX_LIBRARY_PATH=/usr/local/cctbx/cctbx_build/lib

# Location where the CCTBX binary directory is kept
CCTBX_BIN_PATH=/usr/local/cctbx/cctbx_build/bin

# You shouldn't need to edit again below here.
# --------------------------------------------------------------------------- #
# SETUP CCTBX SETTINGS
# N.B. from cctbx_build/dispatcher_head.sh
if [ -n "$LD_LIBRARY_PATH" ]; then
	export LD_LIBRARY_PATH="$CCTBX_LIBRARY_PATH:$LD_LIBRARY_PATH"
else
	export LD_LIBRARY_PATH=$CCTBX_LIBRARY_PATH
fi
if [ -n "$PATH" ]; then
	PATH="$CCTBX_BIN_PATH:$PATH"
	export PATH
else
	PATH="$CCTBX_BIN_PATH"
	export PATH
fi

# --------------------------------------------------------------------------- #
# SETUP OLEX2 SETTINGS
# Set olex home directory - assuming user $home
if [ -n "$OLEX2_DIR" ]; then
        echo "OLEX2_DIR already defined:" $OLEX2_DIR
else
# Assuming defualt setup and setting to user home directory
	export OLEX2_DIR=$OLEX2_DEFAULT_DIR
        echo "Using defaults, Setting OLEX2_DIR:" $OLEX2_DIR
fi

if [ -n "$PATH" ]; then
	export PATH="$PATH:$OLEX2_DIR"
else
	export PATH="$OLEX2_DIR"
fi

# Move local usettings.dat
if test -d "$OLEX2_DIR"; then
	echo "$OLEX2_DIR olex2 home present"
else
	echo "No olex2 dir, creating here: $OLEX2_DIR"
	mkdir $OLEX2_DIR
	cp -ru /usr/share/olex2/* $OLEX2_DIR
fi
if test -a "$OLEX2_DIR/usettings.dat"; then
	echo "$OLEX2_DIR/usettings.dat present"
else
	echo "Moving default usettings.dat to: $OLEX2_DIR"
        cp /usr/share/olex2/usettings.dat $OLEX2_DIR
fi

# This defines the location of cctbx -again needs some automation to allow setting from env
if [ -n "$OLEX2_CCTBX_DIR" ]; then
        echo "OLEX2_CCTBX_DIR already defined:" $OLEX2_CCTBX_DIR
else
# Assuming setup using /usr/local
# Note this should be replaced with a locate possibly soon
	export OLEX2_CCTBX_DIR=$OLEX2_CCTBX_DEFAULT_DIR
fi

# Actually get around to running something!
# Unirun checks that the GUI files are up2date with the usettings.dat defined
# server and the local install directory defined as OLEX2_DIR
if `zenity --window-icon=/usr/share/icons/olex2.xpm --question --title  "Downloading Files" --text='Do You want Olex2 to check for GUI Updates?'`; then
	echo "Checking for updates"
	/usr/libexec/olex2/unirun $OLEX2_DIR | zenity --width 620 --window-icon=/usr/share/icons/olex2.xpm --text-info --title='Downloading Files - Hit Close When FINISHED Appears'
else
	echo "Updates skipped on user request"
# Now we've checked all is up2date and informed the user that we did we can
# can now actually run olex2
fi
/usr/libexec/olex2/olex2 &

