#!/usr/bin/perl
#
#  Copyright (C) 1998 Marcus Brinkmann. This is derived from vncserver.
#
#  This 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 software 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 software; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#  USA.
#

#
# vncpasswd - wrapper script to start vncpasswd.real.
#

#
# Get the program name
#

$vncPasswdCmd = "/usr/X11R6/lib/vncserver/vncpasswd.real";

($prog) = ($0 =~ m|([^/]+)$|);

if (!defined($ENV{HOME})) {
    die "$prog: The HOME environment variable is not set.\n";
}

die "couldn't find executable $vncPasswdCmd" unless (-x "$vncPasswdCmd");

# Source in configuration files, first the site wide one and then the
# user specific one.

$Config_file = "/etc/vnc.conf";
&ReadConfigFile();
$Config_file = "$ENV{HOME}/.vncrc";
&ReadConfigFile();

if (!$vncUserDir) {
  $vncUserDir = "$ENV{HOME}/.vnc";
}
if (!$vncPasswdFile) {
  $vncPasswdFile = $vncUserDir . "/passwd";
}

&ParseOptions("-help",0);

&Usage() if ($opt{'-help'} || @ARGV > 1);

if ($ARGV[0]) {
    $vncPasswdFile = $ARGV[0];
}

system("$vncPasswdCmd $vncPasswdFile");
if (($? >> 8) != 0) {
    exit 1;
}

exit;

#########################################################################
#
# code submitted from Manoj Srivastava. Thank you, Manoj!
#
# ReadConfigFile reads in a config file and sets variables according to it.
#

sub ReadConfigFile
{
  open(CONFIG, "$Config_file") || return;
  my $lineno = 0;
  while (<CONFIG>) {
      chomp;
      $lineno++;
      s/\#.*//og;
      next if /^\s*$/og;
      $_ .= ";" unless /;\s*$/;
      if (/^\s*([^=]+)\s*=\s*(\S.*)$/o) {
          my $ret = eval {"$1=$2"};
          if ($@) {
              print STDERR "Error parsing config file $Config_file!\n";
              print STDERR "$lineno:$_\n";
          }
      }
  }
}

#
# Usage
#

sub Usage
{
    die("usage: $prog [passwdFile]\n");
}


#
# ParseOptions takes a list of possible options and a boolean indicating
# whether the option has a value following, and sets up an associative array
# %opt of the values of the options given on the command line. It removes all
# the arguments it uses from @ARGV and returns them in @optArgs.
#

sub ParseOptions
{
    local (@optval) = @_;
    local ($opt, @opts, %valFollows, @newargs);

    while (@optval) {
	$opt = shift(@optval);
	push(@opts,$opt);
	$valFollows{$opt} = shift(@optval);
    }

    @optArgs = ();
    %opt = ();

    arg: while ($arg = shift(@ARGV)) {
	foreach $opt (@opts) {
	    if ($arg eq $opt) {
		push(@optArgs, $arg);
		if ($valFollows{$opt}) {
		    if (@ARGV == 0) {
			&Usage();
		    }
		    $opt{$opt} = shift(@ARGV);
		    push(@optArgs, $opt{$opt});
		} else {
		    $opt{$opt} = 1;
		}
		next arg;
	    }
	}
	push(@newargs,$arg);
    }

    @ARGV = @newargs;
}



