#!/usr/bin/perl

# Settings
my $DEBUG = "off";	# Set to "yes" to turn it on
my $EXTERNFILE = "/sbbs/ctrl/xtrn.ini";	# The external list file
my $EDITOR = $ENV{EDITOR};
if ($EDITOR eq "")
{
	$EDITOR = "nano";
}

# ----------- No changes below here
my $VERS = "1.0";

my $num_args = $#ARGV + 1;

# Default settings
my $DOOR_PROG_SECT = "";
my $DOOR_NAME = "DOORNAME";
my $DOOR_INTERNAL_CODE = "DOORCODE";
my $DOOR_STARTUPDIR = "";
my $DOOR_COMMAND_LINE = "COMMAND LINE";
my $DOOR_CLEANUP = "";
my $DOOR_EXEC_COST = "";
my $DOOR_ACCESS_REQ = "";
my $DOOR_EXEC_REQ = "";
my $DOOR_MULTI_CON = "YES";
my $DOOR_IO_METHOD = "IOMETHOD";
my $DOOR_IO_METHOD = "YES";
my $DOOR_USE_SHELL = "NO";
my $DOOR_MODIFY_USER = "NO";
my $DOOR_EXECUTE_EVENT = "NO";
my $DOOR_PAUSE_EXEC = "YES";
my $DOOR_DISABLE_LOCAL = "NO";
my $DOOR_DROP_FILE_TYPE = "NONE";
my $DOOR_PLACE_DROP = "DROPTYPE";
my $DOOR_TIME_OPTIONS = "";

sub MyUsage
{
        print "Invalid args - Execute the command as:\n";
        print "adddoor create \<doorfile\>\n";
        print "adddoor edit \<doorfile\>\n";
        print "adddoor install \<doorfile\>\n";
        print "adddoor uninstall \<doorfile\>\n";
}

sub PrintDebug
{
	if ($DEBUG="on")
	{
		print "@_\n";
	}
}

print "adddoor v$VERS - Add doors to your Synchronet BBS\n";
print "===================================================\n";
if ($num_args != 2)
{
	MyUsage();
	exit 0;
}

if (($ARGV[0] ne "create") && ($ARGV[0] ne "install") && ($ARGV[0] ne "edit") && ($ARGV[0] ne "uninstall"))
{
	MyUsage();
	exit;
}

# The template for the door config
my $TemplateFile = <<'FILE_END';
# Door Section
DOOR_PROG_SECT = ""

# Enter the door name
DOOR_NAME = "DOORNAME"

# Enter internal code
DOOR_INTERNAL_CODE = "DOORCODE"

# Startup directory - Defaults to blank
DOOR_STARTUPDIR = ""

# Command Line
DOOR_COMMAND_LINE = "COMMAND LINE"

# Cleanup command - Defaults to blank
DOOR_CLEANUP = ""

# Execution Cost - Defaults to blank
DOOR_EXEC_COST = ""

# Access Requirements - Defaults to blank
DOOR_ACCESS_REQ = ""

# Execution Requirements - Defaults to blank
DOOR_EXEC_REQ = ""

# Multiple Concurrent Users
DOOR_MULTI_CON = "YES"

# I/O Method
DOOR_IO_METHOD = "IOMETHOD"

# Native Executable
DOOR_IO_METHOD = "YES"

# Use Shell
DOOR_USE_SHELL = "NO"

# Modify User Data
DOOR_MODIFY_USER = "NO"

# Execute On Event
DOOR_EXECUTE_EVENT = "NO"

# Pause After Execution
DOOR_PAUSE_EXEC = "YES"

# Disable Local Display
DOOR_DISABLE_LOCAL = "NO"

# BBS Drop File Type
DOOR_DROP_FILE_TYPE = "NONE"

# Place Drop File
DOOR_PLACE_DROP = "DROPTYPE"

# Time Options - Defaults to empty
DOOR_TIME_OPTIONS = ""

FILE_END

sub parseline
{
	my $InputLine = @_[0];
	DebugPrint("Inputline is $InputLine\n");
	my ($Command, $InputArgument) = split / = /, $InputLine;
	$InputArgument = substr($InputArgument, 1);
	$InputArgument = substr($InputArgument, 0, length($InputArgument) - 1);
	DebugPrint("Saw Parseline $InputArgument\n");
	return($InputArgument);
}

sub ImportFile
{
	if (!-f $ARGV[1])
	{
		print "File '$ARGV[1]' doesn't exist\n";
		exit 0;
	}
	open(my $fh, '<', $ARGV[1]) or die "Could not open file '$ARGV[1]' $!";
	while (my $row = <$fh>)
	{
		chop($row);
		if (substr($row, 0, 1) eq "#")
		{
			next;
		}
		DebugPrint("Saw $row\n");
		if ($row eq "")
		{
			next;
		}
		if (substr($row, 0, 4) ne "DOOR")
		{
			print "Saw invalid line: '$row' - skipping\n";
			next;
		}
		if (substr($row, 0, 9) eq "DOOR_NAME")
		{
			$DOOR_NAME = parseline($row);
		}
		elsif (substr($row, 0, 18) eq "DOOR_INTERNAL_CODE")
		{
			$DOOR_INTERNAL_CODE = parseline($row);
		}
		elsif (substr($row, 0, 15) eq "DOOR_STARTUPDIR")
		{
			$DOOR_STARTUPDIR = parseline($row);
		}
		elsif (substr($row, 0, 17) eq "DOOR_COMMAND_LINE")
		{
			$DOOR_COMMAND_LINE = parseline($row);
		}
		elsif (substr($row, 0, 12) eq "DOOR_CLEANUP")
		{
			$DOOR_CLEANUP = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "DOOR_EXEC_COST")
		{
			$DOOR_EXEC_COST = parseline($row);
		}
		elsif (substr($row, 0, 15) eq "DOOR_ACCESS_REQ")
		{
			$DOOR_ACCESS_REQ = parseline($row);
		}
		elsif (substr($row, 0, 13) eq "DOOR_EXEC_REQ")
		{
			$DOOR_EXEC_REQ = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "DOOR_MULTI_CON")
		{
			$DOOR_MULTI_CON = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "DOOR_IO_METHOD")
		{
			$DOOR_IO_METHOD = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "DOOR_IO_METHOD")
		{
			$DOOR_IO_METHOD = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "DOOR_USE_SHELL")
		{
			$DOOR_USE_SHELL = parseline($row);
		}
		elsif (substr($row, 0, 16) eq "DOOR_MODIFY_USER")
		{
			$DOOR_MODIFY_USER = parseline($row);
		}
		elsif (substr($row, 0, 18) eq "DOOR_EXECUTE_EVENT")
		{
			$DOOR_EXECUTE_EVENT = parseline($row);
		}
		elsif (substr($row, 0, 15) eq "DOOR_PAUSE_EXEC")
		{
			$DOOR_PAUSE_EXEC = parseline($row);
		}
		elsif (substr($row, 0, 15) eq "DOOR_PAUSE_EXEC")
		{
			$DOOR_PAUSE_EXEC = parseline($row);
		}
		elsif (substr($row, 0, 18) eq "DOOR_DISABLE_LOCAL")
		{
			$DOOR_DISABLE_LOCAL = parseline($row);
		}
		elsif (substr($row, 0, 19) eq "DOOR_DROP_FILE_TYPE")
		{
			$DOOR_DROP_FILE_TYPE = parseline($row);
		}
		elsif (substr($row, 0, 15) eq "DOOR_PLACE_DROP")
		{
			$DOOR_PLACE_DROP = parseline($row);
		}
		elsif (substr($row, 0, 17) eq "DOOR_TIME_OPTIONS")
		{
			$DOOR_TIME_OPTIONS = parseline($row);
		}
		elsif (substr($row, 0, 14) eq "$DOOR_PROG_SECT")
		{
			$DOOR_PROG_SECT = parseline($row);
		}
	}
	close ($fh);
}

sub CreateFile
{
	print "Creating door file '$ARGV[1]'\n";
	open(my $fh, ">$ARGV[1]");
	print $fh $TemplateFile;
	close($fh);
	system ("$EDITOR $ARGV[1]");
	print "File created\n";
	exit 0;
}

sub EditFile
{
	if (-f $ARGV[1])
	{
		print "Editing door file '$ARGV[1]'\n";
		system ("$EDITOR $ARGV[1]");
	}
	else
	{
		print "File '$ARGV[1]' doesn't exist\n";
	}
}

sub InstallFile
{
	# First import the file settings
	print "Installing '$ARGV[1]':\n";
	ImportFile();

	# Now open extern file for append
	open(my $fh, '>>', $EXTERNFILE) or die "Could not open file '$EXTERNFILE' for appending $!";

	print ($fh "\[prog:$DOOR_PROG_SECT:$DOOR_INTERNAL_CODE\]\n");
	print ($fh "name=$DOOR_NAME\n");
	print ($fh "ars=$ACCESS_REQ\n");
	print ($fh "execution_ars=$EXEC_REQ\n");
	print ($fh "cmd=$DOOR_COMMAND_LINE\n");
	print ($fh "clean_cmd=$DOOR_CLEANUP\n");
	print ($fh "startup_dir=$DOOR_STARTUPDIR\n");
	print ($fh "cost=$DOOR_EXEC_COST\n");
	print ($fh "max_time=$DOOR_TIME_OPTIONS\n");

$DOOR_MULTI_CON = "YES";
$DOOR_IO_METHOD = "IOMETHOD";
$DOOR_IO_METHOD = "YES";
$DOOR_USE_SHELL = "NO";
$DOOR_MODIFY_USER = "NO";
$DOOR_EXECUTE_EVENT = "NO";
$DOOR_PAUSE_EXEC = "YES";
$DOOR_DISABLE_LOCAL = "NO";
$DOOR_DROP_FILE_TYPE = "NONE";
$DOOR_PLACE_DROP = "DROPTYPE";
$DOOR_PROG_SECT = "";

	close($fh);
}

sub UninstallFile
{
	print "Uninstall not implemented yet\n";
}

if (($ARGV[0] eq "create") && (-f $ARGV[1]))
{
	print "File '$ARGV[1]' already exists - overwrite\? y/N:";
        my $entered = <STDIN>;
	$entered = lc($entered);
        chop($entered);
	if ($entered ne "y")
	{
		print "Aborting...\n";
		exit 0;
	}
}
if ($ARGV[0] eq "create")
{
	CreateFile();
}
elsif ($ARGV[0] eq "edit")
{
	EditFile();
	exit 0;
}
elsif ($ARGV[0] eq "install")
{
	InstallFile();
	exit 0;
}
elsif ($ARGV[0] eq "uninstall")
{
	UninstallFile();
	exit 0;
}
exit 0;
