my $cConfigText;


sub PreCreateForm ()
{
   my $cDriver;
   my $cDatabase;
   my $cUser;
   my $cHost;
   my $cHostLine = "\n";
   my $cPassword;

   my $cShowText;

   my $cLocaltime = localtime();

   $cDriver   = $oSessionGL->getState("CreateDbRc,DbDriver");
   $cDatabase = $oSessionGL->getState("CreateDbRc,Database");
   $cHost     = $oSessionGL->getState("CreateDbRc,Host");
   $cUser     = $oSessionGL->getState("CreateDbRc,User");
   $cPassword = $oSessionGL->getState("CreateDbRc,Password");

   $cHostLine = "Host      = $cHost\n"
       if $cHost;

   $cConfigText = <<EOF;
# -----------------------------------------------------------------------
# This config-file was generated by WWWdb at $cLocaltime
# to access the Database $cDatabase, using the driver $cDriver
# -----------------------------------------------------------------------

[DB]
Driver    = $cDriver

Database  = $cDatabase
$cHostLine
Username  = $cUser
Password  = $cPassword

# -----------------------------------------------------------------------
EOF

    $cShowText = pre($cConfigText);
    $cShowText =~ s/Password  = (\S+)/Password  = **secret**/;

    &SetField("ConfigText", b(pre($cShowText)));
    &SetField("ConfigFile", $cConfigText);

}


sub Check_DbRc_DriverName ()
{

   &AddError("Database", "Please select a name for the Db-driver!")
       if(!&GetField("Database"));
}



sub MyBtnFinish ()
{

   &CheckRecord();

   if(!&GetField("DriverName"))
   {
      &GenPage();
   }
   else
   {
      my $cDriverName;
      my $cFileName;

      $cFileName = &GetAttr("BaseDir") . "/lib/WWWdb/Db/" .
          &GetField("DriverName") . ".rc";

      # The Record has not been found
      &Error(sprintf(i18n("Sorry, but the file %s already exists"),
                     b($cFileName)))
          if (-f $cFileName);

      # The Record has not been found
#      &Error(sprintf(i18n("Sorry, but the file %s can't be written"),
#                     b($cFileName)))
#          unless (-w $cFileName);

      print MY_LOG "DbDriver written to -> $cFileName\n";

      open DRIVER_CONFIG_FILE, ">$cFileName" or
          &Error(sprintf(i18n("Can't open file %s"),
                         "$cFileName"));

      print DRIVER_CONFIG_FILE &GetField("ConfigFile");

      close DRIVER_CONFIG_FILE;

      $oSessionGL->setState("CreateDbRc,DbDriver", undef);
      $oSessionGL->setState("CreateDbRc,Database", undef);
      $oSessionGL->setState("CreateDbRc,User", undef);
      $oSessionGL->setState("CreateDbRc,Password", undef);

      print &Redirect(&GetAttr("SessionId"), "WWWdb:Index");
   }
}

sub MyBtnPrev ()
{
   my $cDriver;

   print &Redirect(&GetAttr("SessionId"),
                   "WWWdb:Wiz:CreateDbRc_2");
}

1;
