#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
root_dir="$( cd -P "$( dirname "$SOURCE" )"/.. >/dev/null 2>&1 && pwd )"
# 启用检查更新 for electron-updater
export APPIMAGE=1
sed -i 's#http://localhost:22331##' ~/.config/bilibili/.runtime
if [ "$DEBUG" == '1' ]; then
  echo "Debug Mode"
  ELECTRON_FORCE_IS_PACKAGED=true NODE_OPTIONS=--enable-source-maps "$root_dir/electron/electron" "$root_dir/tmp/bili/resources/app" --no-sandbox "$@"
elif [ "$DEBUG" == '2' ]; then
  echo "Debug Mode"
  if [ ! -d "$root_dir/app/app" ];then
    cd "$root_dir/app"
    asar e app.asar app
    cd "$root_dir/app/extensions"
    rm -rf bilibili
    ln -s ../../dist/extension bilibili
  fi
  cd "$root_dir"
  ELECTRON_FORCE_IS_PACKAGED=true "$root_dir/electron/electron" "$root_dir/app/app" --no-sandbox "$@"
else
  echo "Release Mode"
  ELECTRON_FORCE_IS_PACKAGED=true "$root_dir/electron/electron" "$root_dir/app/app.asar" --no-sandbox "$@"
fi