builtin-programs/saving/migrate.folk

When the hold save directory is /holdDirectory/ &\
     the program save directory is /programDirectory/ &\
     saving is ready {

    # HACK: A new migration we put here so it precedes Hold load:
    # remove incompatible old editor data (new editor data will be
    # stored with respect to editor/editor.folk, not editor.folk).
    # Old editor data is incompatible.
    file delete $::env(HOME)/folk-data/hold/editor.folk

    # are we already on the new format?
    if {[llength [glob -nocomplain $::env(HOME)/folk-data/hold/*]] > 0} {
        Claim the migration is complete
        return
    }

    # is this a brand new system?
    if {![file exists "$::env(HOME)/folk-live"] && ![file exists "$::env(HOME)/folk-printed-programs"]} {
        Claim this is a first boot
        Claim the migration is complete
        return
    }

    proc tryReadFile {filename} {
        set contents ""

        try {
            set fd [open $filename rb]
            set contents [read $fd]
            close $fd
        } on error e {}

        return $contents
    }

    proc tryRm {filename} {
        try {
            exec rm $filename
        } on error e {}
    }

    # copy programs to new location
    try {
        exec sh << "cp ~/folk-printed-programs/* ~/folk-data/program/"
    } on error e {}

    # grab geometry before deleting
    set defaultGeom [tryReadFile "$::env(HOME)/folk-data/program/default.folkgeom"]
    if {$defaultGeom != ""} {
        Hold! -save -on calibration -key default-program-geometry \
            Claim the default program geometry is $defaultGeom
    }
    tryRm "$::env(HOME)/folk-data/program/default.folkgeom"

    # also grab next id
    set nextId [tryReadFile "$::env(HOME)/folk-data/program/next-id.txt"]
    if {$nextId != ""} {
        Hold! -save -on builtin-programs/print.folk -key next-id \
            the next program id is [string trim $nextId]
    }
    tryRm "$::env(HOME)/folk-data/program/next-id.txt"

    # transfer calibration
    set calibrationPoses [tryReadFile "$::env(HOME)/folk-live/folk-calibration-poses.txt"]
    set calibration [tryReadFile "$::env(HOME)/folk-live/folk-calibration-output.txt"]
    if {$calibrationPoses != "" && $calibration != ""} {
        Hold! -save -on calibration -key calibration \
            Claim the calibration is $calibration
        Hold! -save -on calibration -key poses \
            Claim the calibration poses are $calibrationPoses
    }

    puts "\n\n\n### MIGRATION SUCCESSFUL ###\n"
    puts "Note: folk-printed-programs/ and folk-live/ are no longer needed. \
Feel free to delete them once you've confirmed that everything works!"
    puts "\n############################\n\n\n"

    Claim the migration is complete
}