#! /bin/bash

# http://software.broadinstitute.org/software/igv/

set -ex

PKG=igv
VERSION=2.5.2
VERSION2=$(echo $VERSION | cut -d. -f1,2)
BUILD=0

PREFIX=/pkg/$PKG-$VERSION-$BUILD
if [ -n "$TESTING" ]; then PREFIX=/dev/shm/$PKG-$VERSION-$BUILD ; fi

mkdir -p $PREFIX
cd $PREFIX
mkdir -p attic

if test -e attic/IGV_Linux_$VERSION.zip ; then
  rm -vfr bin
  mv attic/IGV_Linux_$VERSION.zip .
  rm -vfr attic/*
else
  wget http://data.broadinstitute.org/igv/projects/downloads/$VERSION2/IGV_Linux_$VERSION.zip
fi

unzip IGV_Linux_$VERSION.zip

mkdir -p attic
mv IGV_Linux_$VERSION.zip attic

mv IGV_Linux_$VERSION bin

mv bin/readme.txt attic
mv bin/igv* attic

# trash the bundled jdk ("AdoptOpenJDK"/JAVA_VERSION="11.0.2")
rm -fr bin/jdk-11

cat >$PREFIX/profile <<-EOF
	. /pkg/openjdk-11.0.3.2-0/profile
	PATH=$PREFIX/bin:\$PATH
	if [ -d $PREFIX/.compatlibs ]; then export LD_LIBRARY_PATH=$PREFIX/.compatlibs\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH} ; fi
EOF

######################################################################
# Setup our own startup scripts, and try to be smarter with the memory

#############################  bin/igv  ##############################
cat >bin/igv <<-'EOF'
#!/bin/sh

#-Xmx4g indicates 4 gb of memory, adjust number up or down as needed
#Script must be in the same directory as igv.jar
#Add the flag -Ddevelopment = true to use features still in development

MEM_KB=$(grep 'MemTotal:' /proc/meminfo | head -1 | rev | cut -d' ' -f2 | rev)
MEM_GB=$(( $MEM_KB/1024/1024 ))

CONSIDER="#   Consider using 'igvxmem XYZg' to start igv with proper memory setup."

if test $MEM_GB -lt 4; then
  MSG="#   This is fairly below the suitable amount of 4 GB.\n$CONSIDER"
elif test $MEM_GB -gt 9 ; then
  MSG="$CONSIDER\n#   To gain better performance you may increase the memory in sane steps."
fi

if test -n "$MSG"; then
  echo '# NOTE:'
  echo "#   found approx. $MEM_GB GB of Memory on your host."
  echo '#'
  echo -e "$MSG"
  echo '#'
  echo '# igv with default settings will start in 5 seconds ...'
  sleep 5
fi

prefix=`dirname \$(readlink \$0 || echo \$0)`
exec java --module-path="${prefix}/lib" -Xmx4g \
    @"${prefix}/igv.args" \
    -Dsun.java2d.uiScale=1 \
    -Dapple.laf.useScreenMenuBar=true \
    -Djava.net.preferIPv4Stack=true \
    --module=org.igv/org.broad.igv.ui.Main "$@"

EOF

chmod 755 bin/igv

############################  bin/igvxmem  ###########################
cat >bin/igvxmem <<-'EOF'
#!/bin/sh

#-Xmx4g indicates 4 gb of memory, adjust number up or down as needed
#Script must be in the same directory as igv.jar
#Add the flag -Ddevelopment = true to use features still in development

MEM=4g

case $1 in 
  [1-9]g          |\
  [1-9][0-9]g     |\
  [1-2][0-9][0-9]g)
                     MEM=$1
                     shift
  ;;

  *)
     echo "# Error: need a proper value for memory to use, eg. 8g"
     exit 1
  ;;
esac

prefix=`dirname \$(readlink \$0 || echo \$0)`
exec java --module-path="${prefix}/lib" -Xmx$MEM \
    @"${prefix}/igv.args" \
    -Dsun.java2d.uiScale=1 \
    -Dapple.laf.useScreenMenuBar=true \
    -Djava.net.preferIPv4Stack=true \
    --module=org.igv/org.broad.igv.ui.Main "$@"

EOF

chmod 755 bin/igvxmem

############################  bin/igv.args  ##########################
cat >bin/igv.args <<-'EOF'
# Arguments to support JIDE on Java 11
  --add-exports java.desktop/javax.swing.plaf.synth=jide.common
  --add-exports java.desktop/sun.swing=jide.common
  --add-exports java.desktop/sun.awt=jide.common
  --add-exports java.desktop/sun.awt.image=jide.common
  --add-exports java.desktop/sun.awt.shell=jide.common
  --add-exports java.desktop/sun.awt.dnd=jide.common
  --add-exports java.base/sun.security.action=jide.common

# Disable a JAXB optimization that causes illegal access exceptions on Java 11
  -Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize

EOF

chmod 644 bin/igv.args

#############################   "HiDpi"   ###########################
sed -e 's/uiScale=1/uiScale=2/' bin/igv > bin/igv_hidpi
sed -e 's/uiScale=1/uiScale=2/' bin/igvxmem > bin/igvxmem_hidpi

chmod 755 bin/igv_hidpi
chmod 755 bin/igvxmem_hidpi

exit