#!/usr/bin/perl -w

package esmith;

use strict;
use Errno;
use esmith::config;
use esmith::util;
use esmith::db;

my %conf;
tie %conf, 'esmith::config';

my %accounts;
tie %accounts, 'esmith::config', '/home/e-smith/db/proxyusers';

      unless (-e '/etc/proxyusers')
      {
        system("/bin/touch /etc/proxyusers") == 0
          or warn ("Error occurred creating proxyusers.\n");
      }

      #get users
      my @userlist = ();
      foreach (sort keys %accounts)
      {
         push (@userlist, $_)
            if (db_get_type(\%accounts, $_) eq 'user');
      }

      #add to or update list
      foreach my $user (@userlist)
      {
          my $password  = db_get_prop(\%accounts, $user, "password")     || 'null';

          system("/usr/bin/htpasswd -b /etc/proxyusers $user $password") == 0
            or warn ("Error occurred creating user passwd.\n");
      }

      #restart with new group settings if dg is already running
      if (-e '/var/run/dansguardian.pid')
      {
        esmith::util::processTemplate (\%conf, "/etc/dansguardian/lists/filtergroupslist");

        system("/usr/sbin/dansguardian -g") == 0
          or system("/usr/sbin/dansguardian") == 0
            or warn ("Error occurred restarting dansguardian.\n");
      }

exit (0);
