#!/bin/bash

KOHAUSER=kohaadmin
KOHAPASS=katikoan
KOHADB=koha

SQL="mysql -u$KOHAUSER -p$KOHAPASS $KOHADB"

$SQL -e "delete from marc_subfield_structure; delete from marc_tag_structure; delete from biblio_framework" ; 

$SQL < installer/data/mysql/en/marcflavour/marc21/mandatory/marc21_framework_DEFAULT.sql ; 
$SQL < installer/data/mysql/en/marcflavour/marc21/optional/marc21_simple_bib_frameworks.sql ; 
$SQL < installer/data/mysql/en/marcflavour/marc21/optional/marc21_fastadd_framework.sql ; 

for fc in $( $SQL -N -e "select frameworkcode from biblio_framework" ); do 
  for tag in $( $SQL -N -e "select tagfield from marc_tag_structure where frameworkcode = '$fc'" ); do 
    $SQL -N -e "select frameworkcode,tagfield,liblibrarian \
                from marc_tag_structure \
                where tagfield = '$tag' and frameworkcode = '$fc' \
                and liblibrarian <> ( select liblibrarian from marc_tag_structure where tagfield = '$tag' and frameworkcode = '' )"; 
  done; 
done | tee differences.txt

for fc in $( $SQL -N -e "select frameworkcode from biblio_framework order by frameworkcode" ); do 
  for tag in $( $SQL -N -e "select distinct tagfield from marc_subfield_structure where frameworkcode = '$fc' order by tagfield" ); do 
    for sub in $( $SQL -N -e "select tagsubfield from marc_subfield_structure where frameworkcode = '$fc' and tagfield = '$tag' order by tagsubfield" ); do 
      $SQL -N -e "select frameworkcode,tagfield,tagsubfield,liblibrarian \
                  from marc_subfield_structure \
                  where tagfield = '$tag' and tagsubfield = '$sub' and frameworkcode = '$fc' \
                  and liblibrarian <> ( select liblibrarian from marc_subfield_structure where tagfield = '$tag' and tagsubfield = '$sub' and frameworkcode = '' )"; 
    done; 
  done; 
done | tee -a differences.txt

