#!/usr/bin/env bash

line=$(pidof hl2_linux)
arr=($line)
inst=$1
if [ $# == 0 ]; then
  inst=0
fi

if [ ${#arr[@]} == 0 ]; then
  echo tf2 isn\'t running!
  exit
fi

if [ $inst -gt ${#arr[@]} ] || [ $inst == ${#arr[@]} ]; then
  echo wrong index!
  exit
fi

proc=${arr[$inst]}

echo Running instances: "${arr[@]}"
echo Attaching to "$proc"

sudo ./detach $inst bin/libcathook.so

if grep -q "$(realpath bin/libcathook.so)" /proc/"$proc"/maps; then
  echo already loaded
  exit
fi

echo loading "$(realpath bin/libcathook.so)" to "$proc"
gdb -n -q -batch \
  -ex "attach $proc" \
  -ex "set \$dlopen = (void*(*)(char*, int)) dlopen" \
  -ex "call \$dlopen(\"$(realpath bin/libcathook.so)\", 1)" \
  -ex "call dlerror()" \
  -ex 'print (char *) $2' \
  -ex "catch syscall exit exit_group" \
  -ex "continue" \
  -ex "backtrace"
