#! /bin/sh -e
## 06_add_compile_time_defaults.dpatch by  <weasel@debian.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Change a few compile time defaults so that Tor is better integrated on a Debian system

if [ $# -lt 1 ]; then
    echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
    exit 1
fi

[ -f debian/patches/00patch-opts ] && . debian/patches/00patch-opts
patch_opts="${patch_opts:--f --no-backup-if-mismatch} ${2:+-d $2}"

case "$1" in
    -patch) patch -p1 ${patch_opts} < $0;;
    -unpatch) patch -R -p1 ${patch_opts} < $0;;
    *)
        echo "`basename $0`: script expects -patch|-unpatch as argument" >&2
        exit 1;;
esac

exit 0

@DPATCH@
diff -urNad tor-trunk~/src/or/config.c tor-trunk/src/or/config.c
--- tor-trunk~/src/or/config.c	2009-01-18 01:47:33.000000000 +0100
+++ tor-trunk/src/or/config.c	2009-02-05 00:25:17.614844812 +0100
@@ -12,6 +12,7 @@
 #define CONFIG_PRIVATE
 
 #include "or.h"
+#include <pwd.h>
 #ifdef MS_WINDOWS
 #include <shlobj.h>
 #endif
@@ -711,6 +712,8 @@
 #if defined(HAVE_EVENT_GET_VERSION) && defined(HAVE_EVENT_GET_METHOD)
 static void check_libevent_version(const char *m, int server);
 #endif
+static int debian_running_as_debiantor();
+static int debian_config_fix_defaults();
 
 /** Magic value for or_options_t. */
 #define OR_OPTIONS_MAGIC 9090909
@@ -3917,6 +3920,9 @@
   char *command_arg = NULL;
   char *errmsg=NULL;
 
+  if (debian_config_fix_defaults() < 0)
+    goto err;
+
   if (argv) { /* first time we're called. save command line args */
     backup_argv = argv;
     backup_argc = argc;
@@ -5307,3 +5313,62 @@
   return 0;
 }
 
+/* Checks whether we are running as the debian-tor user.
+ *   Returns -1 on error, 2 if we are root, 1 if we are debian-tor, 0 if we are any normal user */
+static int
+debian_running_as_debiantor()
+{
+  struct passwd *pw = NULL;
+  int uid;
+
+  uid = getuid();
+  /* If we run as root we also apply our debian defaults. */
+  if (uid == 0)
+    return 2;
+
+  pw = getpwuid(uid);
+  if (!pw) {
+    log(LOG_WARN, LD_GENERAL, "Could not get passwd information for uid %d.", uid);
+    return -1;
+  }
+  assert(pw->pw_name);
+  if (strcmp(pw->pw_name, "debian-tor") == 0)
+    return 1;
+  else
+    return 0;
+}
+
+static int
+debian_config_fix_defaults()
+{
+  config_var_t *var;
+  static int fixed = 0;
+  int running_as_debian;
+
+  if (fixed) return 0;
+  fixed = 1;
+
+  running_as_debian = debian_running_as_debiantor();
+  if (running_as_debian < 0) return -1;
+  if (!running_as_debian) return 0;
+
+  var = config_find_option(&options_format, "DataDirectory");
+  tor_assert(var);
+  var->initvalue = tor_strdup("/var/lib/tor");
+
+  var = config_find_option(&options_format, "PidFile");
+  tor_assert(var);
+  var->initvalue = tor_strdup("/var/run/tor/tor.pid");
+
+  var = config_find_option(&options_format, "RunAsDaemon");
+  tor_assert(var);
+  var->initvalue = tor_strdup("1");
+
+  if (running_as_debian == 2) {
+    var = config_find_option(&options_format, "User");
+    tor_assert(var);
+    var->initvalue = tor_strdup("debian-tor");
+  };
+
+  return 0;
+}
