#!/usr/bin/env bash

BIN=LightTable
HERE=`dirname $(readlink -f $0)`

LIBUDEV_0=libudev.so.0
LIBUDEV_1=libudev.so.1

add_udev_symlinks() {
  # look for libudev.so.0, and if that can't be found link it to libudev.so.1
  # detect 64-bit or default to 32-bit
  if [ $(getconf LONG_BIT) == "64" ]; then
    FOLDERS="/lib64 /lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu /usr/lib64 /usr/lib /lib"
  else
    FOLDERS="/lib /lib/i386-linux-gnu /usr/lib /usr/lib/i386-linux-gnu"
  fi

  for folder in $FOLDERS; do
    if [ -f "${folder}/${LIBUDEV_0}" ]; then
      return 0
    fi
  done

  for folder in $FOLDERS; do
    if [ -f "${folder}/${LIBUDEV_1}" ]; then
      ln -snf "${folder}/${LIBUDEV_1}" "${HERE}/${LIBUDEV_0}"
      return 0
    fi
  done

  echo "${LIBUDEV_0} or ${LIBUDEV_1} not found in any of ${FOLDERS}".
  exit 1
}

add_udev_symlinks

LD_LIBRARY_PATH="$HERE:$LD_LIBRARY_PATH" $HERE/$BIN "$@" &
