--[[

Backup, restore and copy utility

A program intended to run from disk and able to backup turtles do disk and restore/copy to turtles

]]--

local backup_dir = '/disk/backups'

function init()
    if not fs.exists(backup_dir) then 
        fs.makeDir(path)
    end
end

function multiple_choice(choices)
    for i, choice in ipairs(choices) do
        print('('..choice.key..') '..choice.label)
    end
    local running = true
    while running do
        event, char = os.pullEvent('char')
        for i, choice in ipairs(choices) do
            if char == choice.key then
                return i
            end            
        end
        print('Please pick a valid choice...')
    end
end

function get_confirm()
    local running = true
    while running do
        event, char = os.pullEvent('char')
        if char == 'y' then
            return true
        elseif char == 'n' then
            return false
        else
            print('Please press "y" or "n"...')
        end
    end
end

function backup()
    print('Backing up...')
    local label = os.getComputerLabel()
    print('Will be using the label "'..label..'"')
    local target_dir = backup_dir..'/'..label
    if fs.exists(target_dir) then
        print('Target dir already exists, overwrite? (y/n)')
        if not get_confirm() then
            print('Cancelled backup...')
            return
        end
        
    end
end

function main_loop()

    choices = {
        {['label']='Backup', ['key']='b'},
        {['label']='Restore', ['key']='r'},
        {['label']='Quit', ['key']='q'},
    }

    term.clear()
    term.setCursorPos(1,1)
    
    
    print('-=# CopyMaster #=-')
    local running = true
    while running do
        local action = multiple_choice(choices)
        if choices[action].label == 'Quit' then
            running = false
            print('Quitting...')
        elseif choices[action].label == 'Backup' then
            backup()
        else
            error('Got weird result from multichoice')
        end
    end
    
    
end


running_from = shell.getRunningProgram() -- check if I'm running from a diskdrive
if running_from:sub(1,4) == 'disk' then
    print('copymaster running from disk')
    main_loop()    
else
    print('copymaster not running from disk')
end