# Take Henry's record database from ASCII export files to MySQL.

default: msg

msg:
	@echo \
        "First run './transfer <LATEST_QA4_DIRECTORY>', then run 'make data.'"

data: sanitize post-sanitize

post-sanitize: hide xml mussorgskize sql

# Change from CRLF to LF format, and remove the final ^Z (ASCII 26)
# from each file.  This rule is meant to be run manually.
sanitize:
	@sed -e 's/[\r\d026]$$//g' < records.asc > tmp && mv tmp records.asc
	@sed -e 's/[\r\d026]$$//g' < orch.asc    > tmp && mv tmp orch.asc
	@sed -e 's/[\r\d026]$$//g' < instrum.asc > tmp && mv tmp instrum.asc
	@sed -e 's/[\r\d026]$$//g' < chamber.asc > tmp && mv tmp chamber.asc
	@sed -e 's/[\r\d026]$$//g' < song.asc    > tmp && mv tmp song.asc
	@sed -e 's/[\r\d026]$$//g' < oprec.asc   > tmp && mv tmp oprec.asc
	@sed -e 's/[\r\d026]$$//g' < opera.asc   > tmp && mv tmp opera.asc

# There's one file we don't publish, because it's idiosyncratic and
# incomplete.  We change its name right now, so that oprec.asc (which
# we *do* publish) can become opera.xml without confusing us.
hide: 
	@if [ -f opera.asc ]; then mv -f opera.asc forbidden-opera.asc; fi

upload: song.xml chamber.xml opera.xml orchestral.xml henryrec.sql all-completable-lists.js
	@PKG=henryrec-`date "+%F"`.tar.gz;                              \
        tar zcvf $${PKG} song.xml chamber.xml opera.xml orchestral.xml  \
                  henryrec.sql all-completable-lists.js;                \
        scp $${PKG} www.henrysrecords.org:/home/kfogel/scratch/

# Convert QA ASCII exports to an XML format, for publication.
xml: song.asc oprec.asc chamber.asc records.asc
	@echo "Converting ASCII exports to XML (watch for CHECK warnings)..."
	@./asc-to-xml.pl --song < song.asc > song.xml
	@./asc-to-xml.pl --chamber < chamber.asc > chamber.xml
	@./asc-to-xml.pl --opera < oprec.asc > opera.xml
	@./asc-to-xml.pl --orchestral < records.asc > orchestral.xml
	@echo "Done."

mussorgskize:
	@for name in orchestral chamber song opera; do      \
          sed -e 's/MOUSSORGSKY/Mussorgsky/g'               \
	      < $${name}.xml > tmp && mv tmp $${name}.xml;  \
          sed -e 's/Moussorgsky/Mussorgsky/g'               \
	      < $${name}.xml > tmp && mv tmp $${name}.xml;  \
          sed -e 's/moussorgsky/Mussorgsky/g'               \
	      < $${name}.xml > tmp && mv tmp $${name}.xml;  \
        done

# Convert the XML format to SQL, for searching.
sql: 
	@echo "Converting XML files to SQL..."
	@./make-sql
	@echo "Done."

# Load the SQL into the database.  You'll need to use the real passwords.
db: 
	@echo "Creating the database..."
	@echo "drop table henryrec;" | mysql -u henryrecrw --password=RW_PASSWORD_HERE henryrec
	@cat create_henryrec.sql | mysql -u henryrecrw --password=RW_PASSWORD_HERE henryrec
	@echo "Done."
	@echo "Loading the SQL..."
	@cat henryrec.sql | mysql -u henryrecrw --password=RW_PASSWORD_HERE henryrec
	@echo "Done."