VERS='5.4.66'

echo "Mise a jour de la base de donnees pour Version $VERS"

isql - - <<EOT

database gc;

UPDATE 
  gc_parsys 
SET 
  version='$VERS';

DELETE FROM 
  gc_pochette
WHERE
  poc_num > 999999;
  
DELETE FROM 
  gc_production
WHERE
  poc_num > 999999;

DROP INDEX
  ix_poc1;
  
CREATE INDEX 
  ix1_pochette 
ON
  gc_pochette
  (
    poc_num
  );

DROP INDEX
  ix194_5;
  
CREATE INDEX 
  ix2_pochette 
ON
  gc_pochette
  (
    poc_datdep
  );

DROP INDEX
  ix_pro1;
  
CREATE INDEX 
  ix1_production 
ON
  gc_production
  (
    poc_num
  );

DROP INDEX
  ix1_gc_production;
  
CREATE INDEX 
  ix2_production 
ON
  gc_production
  (
    pro_date
  );

ALTER TABLE 
  gc_pochette
ADD
  (numint INTEGER);

CREATE INDEX 
  ix3_pochette
ON
  gc_pochette
  (
    numint
  );

ALTER TABLE 
  gc_production
ADD
  (numint INTEGER);

CREATE INDEX 
  ix3_production
ON
  gc_production
  (
    numint
  );

RENAME COLUMN
  gc_cptgpoc.poc_num
TO
  numint;

DROP TABLE
  gc_nopoc;
  
CREATE TABLE 
  gc_nopoc
  (
    nopoc INTEGER
  );
  
INSERT INTO 
  gc_nopoc 
SELECT 
  defdep*10000000+9000000
FROM
  gc_soc;

UPDATE 
  gc_pochette
SET
  numint = numdep * 10000000+poc_num;

UPDATE 
  gc_production
SET
  numint = (SELECT 
              numint 
            FROM 
              gc_pochette
            WHERE
              gc_production.poc_num = gc_pochette.poc_num
           )
WHERE
  poc_num 
IN
  (SELECT
    poc_num
   FROM
    gc_pochette
  );

UPDATE 
  gc_production
SET
  numint = numdep * 10000000+poc_num
WHERE
  poc_num
NOT IN
  (SELECT
    poc_num
   FROM
    gc_pochette
  );

DROP TABLE
  gc_nocptgpoc;
  
CREATE TABLE 
  gc_nocptgpoc
  (
    nodos INTEGER
  );
  
ALTER TABLE 
  gc_fac
ADD
  (vndatt char (2));

ALTER TABLE
  gc_vrev
ADD
  (om smallint);

UPDATE
  gc_vrev
SET
  om=octroi;

CLOSE DATABASE;

EOT

chmod 666 $DBPATH/gc.dbs/*.idx
chmod 666 $DBPATH/gc.dbs/*.dat

echo "Mise a jour terminee"
