#!/bin/sh

# When binding to privileged ports the tor process needs to start with root
# permissions, then lower the user it's running as afterward.

# checks that we're running as root

if [ "$(id -u)" != "0" ]; then
  printf "This script needs root permissions to run. Try again with \"sudo ${0}\".\n\n"
  exit 1
fi

# Checks that the torrc in this directory has a "User <username>" entry. If 
# they ran the wizard multiple times then we might currently have a torrc
# without it, causing this to run tor as root (... not what we wanted).

torrcLoc=$( dirname "$0" )/torrc
if ! `grep -q "^User " ${torrcLoc}`; then
  printf "The tor configuration file (${torrcLoc}) doesn't lower its\n"
  printf "permissions. You should only be using this script to run tor instances that\n"
  printf "need root permissions to start.\n\n"
  exit 1
fi

# starts the tor process

tor --quiet -f $torrcLoc&

