echo "Database Version $version" 1>&2

rdschemas="SELECT schemata.schema_name FROM information_schema.schemata where schema_name like 'mne_%' AND NOT schema_name='mne_application' AND NOT schema_name='mne_catalog'"
rdtables="SELECT tables.table_name FROM information_schema.tables WHERE tables.table_schema = 'par1' AND table_type = 'BASE TABLE'"

echo "--Tabellen überprüfen"

schemas=`get_checkdata "$rdschemas"`
schemas="$schemas"

tables=`get_checkdata "$rdtables" public`
for t in $table
do
    echo 'DROP TABLE public.'$t' CASCADE;'
done

tables=`get_checkdata "$rdtables" mne_catalog`

for t in $tables
do
    case $t in
       id_count) ;;
       *) echo 'TRUNCATE TABLE ' mne_catalog.$t ' CASCADE;'; ;;
    esac;
done

tables=`get_checkdata "$rdtables" mne_application`

for t in $tables
do
    case $t in
      usertables) ;;
      yearday) ;;
      tableconstraintmessages) ;;
      tablecolnames) echo "DELETE from mne_application.tablecolnames tt0 WHERE EXISTS ( SELECT t0."schema", t0.tab, t0.colname FROM mne_application.tablecolnames t0 LEFT JOIN information_schema.columns t1 ON ( t0."schema" = t1.table_schema AND t0.tab = t1.table_name AND t0.colname = t1.column_name ) WHERE t1.table_schema IS NULL AND t0.tab != '' AND t0."schema" = tt0."schema" AND t0.tab = tt0.tab AND t0.colname = tt0.colname );";;
      year) ;;
      customerfunctions) ;;
      update)    echo "delete from mne_application.$t; INSERT INTO mne_application.$t ( updateid, version, updatehost ) VALUES ( '0', '"$version"', 'update.nelson-it.ch' );" ;;
      userpref ) echo "delete from mne_application.$t where username != 'admindb'; update mne_application.$t set startweblet = 'user_settings';"; ;;
      folder )   echo 'TRUNCATE TABLE ' mne_application.$t ' CASCADE;'; ;;
      server )   echo 'TRUNCATE TABLE ' mne_application.$t ' CASCADE;'; ;;

    esac;

    case $t in
      update) ;;
           *) echo "update mne_application.$t set createuser='admindb', modifyuser='admindb';" ;;
    esac;
done

ystart=`date +%Y`
let yend=ystart
let ystart=ystart-1;
let yend=yend+1;

echo "update mne_application.customerfunctions set funcschema = '', func = '';"

echo "update mne_application.year set yearmax = "$yend", yearmin = "$ystart";"
echo 'delete from mne_application.yearday;'
echo 'insert into mne_application.yearday select * from mne_catalog.yearday();'