#!/bin/bash -e

# CONFIG:
# comment out if you don't want asan
export CONF_ASAN="-Db_sanitize=address"
# comment out to disable wl support
export CONF_WL="-Dwl=true"
# comment out to disable drm support (needed for drm support)
export CONF_DRM="-Ddrm=true"

# change this arch
export A="x86_64-linux-gnu"
if test -d /usr/lib/$A; then
 echo ""
else
 unset A
fi

# where to put efl+e
export P="/opt/e"
export CFLAGS="-O0 -g3"
export PATH="$P/bin:$PATH"

if test -n "$A"; then
 export LD_LIBRARY_PATH="$P/lib:$P/lib/$A:/usr/lib:/usr/lib/$A:/lib:/lib/$A:$LD_LIBRARY_PATH"
 export PKG_CONFIG_PATH="$P/lib/pkgconfig:$P/lib/$A/pkgconfig:/usr/lib/pkgconfig:/usr/lib/$A/pkgconfig:/lib/pkgconfig:/lib/$A/pkgconfig:$PKG_CONFIG_PATH"
else
 export LD_LIBRARY_PATH="$P/lib:/usr/lib:/lib:$LD_LIBRARY_PATH"
 export PKG_CONFIG_PATH="$P/lib/pkgconfig:/usr/lib/pkgconfig:/lib/pkgconfig:$PKG_CONFIG_PATH"
fi

# download or update efl and e
function dogit() {
 URLBASE=$1
 NAME=$2
 echo "Clone/update $NAME ..."
 if test -d $NAME; then
  cd $NAME
  git pull --rebase
  cd ..
 else
  git clone $URLBASE/$NAME.git
 fi
}

dogit https://git.enlightenment.org/enlightenment efl
dogit https://git.enlightenment.org/enlightenment enlightenment

echo "Fix /etc/ld.so.conf ..."
# add lib diir to ldconfig if needed
X=`grep $P/lib /etc/ld.so.conf || true`
if test -z "$X"; then
 echo "$P/lib" | sudo tee -a /etc/ld.so.conf
 if test -n "$A"; then
  echo "$P/lib/$A" | sudo tee -a /etc/ld.so.conf
 fi
fi

function build() {
 NAME=$1
 shift
 echo "Build $NAME ..."
 export ASAN_OPTIONS=detect_odr_violation=0:detect_leaks=0:abort_on_error=1::new_delete_type_mismatch=0
 cd $NAME
  rm -rf build
  meson --prefix=$P $CONF_ASAN $@ . build
  ninja -C build
  sudo ninja -C build install
 cd ..
}

build efl \
 -Decore-imf-loaders-disabler=ibus -Devas-loaders-disabler=json \
 -Dfb=true $CONFG_WL $CONF_DRM -Dnetwork-backend=connman -Dbindings=
sudo ldconfig

build enlightenment \
 $CONF_WL

function linkin() {
  FILE=$1
  sudo rm -f /usr/$FILE
  sudo ln -s $P/$FILE /usr/$FILE
}

# make enlightenment visible to login managers
linkin share/xsessions/enlightenment.desktop
linkin share/wayland-sessions/enlightenment-wayland.desktop

echo ""
echo ""
if test -n "$CONF_ASAN"; then
 echo "#######################################################################"
 echo "Add this to your ~.,xsession or ~/.xinitrc or to ~/.bashrc etc."
 echo "if you want to run this asan build in a normal session"
 echo ""
 echo "export ASAN_OPTIONS=detect_odr_violation=0:detect_leaks=0:abort_on_error=1::new_delete_type_mismatch=0"
 echo ""
 echo "#######################################################################"
fi

echo "#######################################################################"
echo ""
echo "To debug Enlightenment - Options"
echo ""
echo "Option 1: X in a window:"
echo " xephyr 1 1280x720x32 &"
echo " export DISPLAY=:1"
echo " export E_START=1"
echo " gdb $P/bin/enlightenment"
echo ""
echo "Option 2: X in a full X session:"
echo " sudo X -ac :1 &"
echo " export DISPLAY=:1"
echo " export E_START=1"
echo " gdb $P/bin/enlightenment"
echo ""
echo "In the above gdb session type 'run' to run the command in the debugger"
echo "If something goes wrong, asan will print out a lot of information. Also"
echo "You can inspect the code where things went wrong with some commands:"
echo ""
echo " l         Lists the code around the line where it went wrong"
echo " bt        Show a backtrace of ther current thread"
echo " p VAR     Print out the value of variable P"
echo " p A->B->C Print out struct A pointing to struct B pointing to var C"
echo " p *VAR    Follow variable VAr if it's a pointer and de-reference it"
echo ""
echo "Option 3: Log into normal session and rely on log+crashdump"
echo "If enlightenment crashes, you will get the normal crash Guru and then"
echo "enlightenment will append the latest bactrace to the end of the"
echo "~/.e-crashdump.txt file - you might want to delete this file to"
echo "remove old traces before a debugging session. Also asan output will"
echo "Go to ~/.e-log.log while e runs. If You restart e after a crash, the"
echo "previous log with the asan output will be ~/.e-log.log.old"
echo ""
echo "#######################################################################"
