#!/bin/bash

appname=ugenecl

# Check if '-ui' parameter is present.
# If it does exclude it from the list of parameters and use ugeneui instead of ugenecl

params=()

while [[ $# -gt 0 ]]; do
  if [ "$1" = "-ui" ]; then
    appname=ugeneui
  else
    params+=("$1")
  fi
  shift
done

dirname=$(dirname "$(readlink -f "$0")")

tmp="${dirname#?}"

if [ "${dirname%$tmp}" != "/" ]; then
  dirname="$PWD/$dirname"
fi

# Check compatibility OS and UGENE (32-bit or 64-bit)
system_i686=false

case "$(uname -m | grep -c 64 | tr -d ' ')" in
0*) system_i686=true ;;
esac

ugene_i686=false
ugene_x86_64=false

case "$(file -b "$dirname/$appname" | cut -d "," -f1 | grep -c 64 | tr -d ' ')" in
0*) ugene_i686=true ;;
1*) ugene_x86_64=true ;;
esac

if $system_i686; then
  if $ugene_x86_64; then
    echo "Warning: You have a 32-bit operating system but you are trying to launch a 64-bit version of the UGENE package. You have to download the appropriate UGENE version instead."
  fi
else
  if $ugene_i686; then
    echo "Warning: You have a 64-bit operating system but you are trying to launch a 32-bit version of the UGENE package. You have to download the appropriate UGENE version instead."
  fi
fi

"$dirname"/"$appname" "${params[@]}"