#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# -----------------------------------------------------------------------------
# Programm-Daten
# -----------------------------------------------------------------------------
# Projekt  : Tools
# System   : Linux
# Programm : ImportInDB.pl
# Modul    : $RCSfile: ConvertVersion.pl,v $
# Version  : $Revision: 1.2 $
# Stand von: $Date: 2002/02/27 08:34:39 $
# Status   : $State: Exp $
#
# Description:
# This Script is used, to maintain your WWWdb-database-tables
#
# -----------------------------------------------------------------------------
# $Log: ConvertVersion.pl,v $
# Revision 1.2  2002/02/27 08:34:39  k_reger
# - no more "use lib"
#
# Revision 1.1  2001/01/25 09:39:20  k_reger
# - Upgraded for 0.8.1
#
# -----------------------------------------------------------------------------


use Getopt::Std;
use File::Basename;

use vars qw($opt_b $opt_d $opt_f $opt_h $opt_t
            $opt_D $opt_U $opt_P $opt_H $opt_V);
use strict;

$main::cVersionMGL = '$Date: 2002/02/27 08:34:39 $$Revision: 1.2 $ ';
$main::cProgramMGL = basename($0);
@main::lValidVersions = ("0.0.7", "0.8.0", "0.8.1");


getopts("b:df:ht:D:H:P:U:V") || Usage();

# Print version
if ($opt_V)
{
    print "$0: $main::cVersionMGL\n";
    exit;
}

Usage() if $opt_h;

Main();

# --- show the usage of the script  ---------------------------------------------
sub Usage()
{
    print <<EOF;
    Call:
        $main::cProgramMGL
            -D <database> -U <user>
            [-P <password>] [-H <host>]
            [-b Oracle|Pg|mysql|InterBase|ODBC]

    Switches:
        -b <database-driver>
               at the moment, the following drivers are implemented:
               Pg        Postgres
               mysql     MySql
               Oracle    Oracle
               InterBase InterBase
               ODBC     Generic ODBC-driver

        -D     database
        -U     user
        -P     password
        -H     host (localhost)

        -f <version>
               from which version to convert from (Format: x.y.z)
        -t <version>
               to which version to convert to (Format: x.y.z)

    Description:
        $main::cProgramMGL is used to upgrade the database-structures
        to the correct values of the actual version

EOF
    exit 0;
}

sub Main()
{
   my $bInvalidFromVersion = 1;
   my $bInvalidToVersion = 1;

   if (!$opt_D || !$opt_U)
   {
      print STDERR (" $main::cProgramMGL Database (-D) and username (-U) are needed\n");
      exit(-1);
   }

   foreach (@main::lValidVersions)
   {
      $bInvalidFromVersion = 0
          if ($opt_f eq $_);

      $bInvalidToVersion = 0
          if ($opt_t eq $_);
   }

   if ($bInvalidToVersion || $bInvalidFromVersion)
   {
      printf STDERR (" $main::cProgramMGL:\nPlease use a valid version. The following are supported: \n\t%s\n",
                     join ", ", @main::lValidVersions);
      exit(-1);
   }

   # 0.0.7 -> 0.8.0
   if($opt_f eq "0.0.7")
   {
      if($opt_t gt "0.0.7")
      {
         # Do currently nothing
         $opt_f = "0.8.0";
      }
   }

   # 0.8.0 -> 0.8.1
   if($opt_f eq "0.8.0")
   {
      if($opt_t gt "0.8.0")
      {
         Conv_0_8_0_to_0_8_1();
         $opt_f = "0.8.1";
      }
   }


}


sub Conv_0_8_0_to_0_8_1()
{

   # text renamed to document, because some db's have a reserved word "TEXT"
   &DumpOut("wwwdb_doc",
            "id_doc,lang,category,sub_category,description,text AS document,sort_nr,last_change");
   &ReCreate("wwwdb_doc");

   # link renamed to ref_link, because some db's have a reserved word "LINK"
   &DumpOut("wwwdb_object",
            "id_object,link AS ref_link,descript,descript_txt_id,sort_nr,attribs,last_change");
   &ReCreate("wwwdb_object");

}



sub DumpOut($$)
{
   my $cTablePI  = shift;
   my $cFieldsPI = shift;

   my $cCmd;

   $cCmd = sprintf ("InstallDB.pl -b '%s' -D '%s' -U '%s' %s %s %s -e -F '%s' %s",
                    $opt_b,
                    $opt_D,
                    $opt_U,
                    $opt_P? "-P $opt_P": "",
                    $opt_H? "-H $opt_H": "",
                    $opt_d? "-d": "",
                    $cFieldsPI,
                    $cTablePI);

   if ($opt_d)
   {
      print STDERR "$cCmd\n";
   }
   else
   {
      system $cCmd;
   }
}


sub ReCreate($$)
{
   my $cTablePI  = shift;

   my $cCmd;


   $cCmd = sprintf ("InstallDB.pl -b '%s' -D '%s' -U '%s' %s %s %s -cif %s.dbdef %s.csv",
                    $opt_b,
                    $opt_D,
                    $opt_U,
                    $opt_P? "-P $opt_P": "",
                    $opt_H? "-H $opt_H": "",
                    $opt_d? "-d": "",
                    $cTablePI,
                    $cTablePI);

   if ($opt_d)
   {
      print STDERR "$cCmd\n";
   }
   else
   {
      system $cCmd;
   }
}


