#!/usr/bin/perl -w

#----------------------------------------------------------------------
#
# Copyright (c) 2001 Daniel van Raay
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#----------------------------------------------------------------------

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/accounts';

#---------------------------------------------------------------------------
# clear and re-create all the user panel symlinks
#---------------------------------------------------------------------------

#clear
opendir (DIR, "/etc/e-smith/web/panels/user/cgi-bin/") or
    die "Can't open directory /etc/e-smith/web/panels/user/cgi-bin/\n";
my @symlinks = grep (!/^\./, readdir (DIR));
closedir (DIR);

foreach my $link (@symlinks)
{
    -e "/etc/e-smith/web/panels/user/cgi-bin/$link" && unlink("/etc/e-smith/web/panels/user/cgi-bin/$link");
}

#always link userpanels
my %newsymlinks;
opendir (DIR, "/etc/e-smith/web/functions/") or
    die "Can't open directory /etc/e-smith/web/functions/\n";
foreach my $userpanels ( grep (/^(userpanel-initial|userpanel-navigation|userpanel-noframes|pleasewait)$/, readdir (DIR)) )
{
    $newsymlinks{$userpanels} = 'Yes';
}
closedir (DIR);

#also add needed panels
foreach my $user (sort keys %accounts)
{
    my $userAdminPanels = db_get_prop(\%accounts, $user, "AdminPanels");
    $userAdminPanels = '' if ! defined ($userAdminPanels);

    foreach my $panels (split (/,/, $userAdminPanels))
    {
        $newsymlinks{$panels} = 'Yes';
    }
}

foreach my $link ( sort keys %newsymlinks )
{
    if ( -e "/etc/e-smith/web/functions/$link" &&
         ! -e "/etc/e-smith/web/panels/user/cgi-bin/$link" )
    {
	symlink("../../../functions/wrapper", "/etc/e-smith/web/panels/user/cgi-bin/$link") ||
	#link("/etc/e-smith/web/functions/$link", "/etc/e-smith/web/panels/user/cgi-bin/$link") ||
	    warn "Couldn't link('functions/$link' to '/etc/e-smith/web/panels/user/cgi-bin/$link'): $!\n";
    }
}


exit (0);

