#!/bin/sh -e
#
# tor - manage torsocks for the current shell

if command -v torsocks >/dev/null 2>&1; then

  case "$1" in
    # show whether torsocks
    show | '')
      # shellcheck disable=SC2154,SC2250
      case $LD_PRELOAD: in
        */libtorsocks.so:*)
          printf 'on\n'
          ;;
        *)
          printf 'off\n'
          ;;
      esac
      ;;
    # turn torsocks on or off
    on)
      # shellcheck disable=SC2312
      . "$(command -p -- torsocks)" \
        && torsocks on
      ;;
    off)
      exec sh -c ". torsocks off"
      ;;
    # command not found
    *)
      printf >&2 'tor(): %s: Unknown command (try "help")\n' "$1"
      return 2
      ;;
  esac
fi
