git @ Cat's Eye Technologies Funicular / 0.1 script / funicular
0.1

Tree @0.1 (Download .tar.gz)

funicular @0.1raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env lua

TOOLSHELF = os.getenv("TOOLSHELF")

--[[========================================================================]]--

--[[ Emulators ]]--

E_UAE = {
    name = 'E-UAE',
    source = 'http://www.rcdrummond.net/uae/e-uae-0.8.29-WIP4/e-uae-0.8.29-WIP4.tar.bz2'
}

QEMU = {
    name = 'QEMU',
    source = 'http://wiki.qemu-project.org/download/qemu-1.6.2.tar.bz2'
}

VICE = {
    name = 'VICE',
    source = 'http://downloads.sourceforge.net/project/vice-emu/releases/vice-2.4.tar.gz',
}

--[[ Emulator Modes ]]--

E_UAE_Amiga_500 = {
    name = 'E-UAE/Amiga 500',
    emulator = E_UAE,
    start_command = 'uae -s bogomem_size=4 -s cpu_speed=max -s floppy_speed=800 -s "filesystem2=rw,DH0:Funicular:${SYSTEM_IMAGE},1"',
    dist_command = 'uae -s bogomem_size=4 -s cpu_speed=max -s floppy_speed=800 -s "floppy0=${DIST_IMAGE}" -s "filesystem2=rw,DH0:Funicular:${SYSTEM_IMAGE},1"',
    distboot_command = 'uae -s bogomem_size=4 -s cpu_speed=max -s floppy_speed=800 -s "floppy0=${DIST_IMAGE}"',
    setup_command = 'uae -s bogomem_size=4 -s cpu_speed=max -s floppy_speed=800 -s "filesystem2=rw,DH0:Funicular:${SYSTEM_IMAGE},1" -s "floppy0=${SETUP_IMAGE}"',
    install_command = 'echo "No install step.  Installation will happen during setup."',
}

QEMU_i386 = {
    name = 'QEMU/i386',
    emulator = QEMU,
    start_command = 'qemu-system-i386 -hda "${SYSTEM_IMAGE}"',
    dist_command = 'qemu-system-i386 -hda "${SYSTEM_IMAGE}" -fda "${DIST_IMAGE}"',
    distboot_command = 'qemu-system-i386 -fda "${DIST_IMAGE}" -boot order=a',
    setup_command = 'qemu-system-i386 -hda "${SYSTEM_IMAGE}" -cdrom "${SETUP_IMAGE}"',
    install_command = 'qemu-system-i386 -hda "${SYSTEM_IMAGE}" -cdrom "${INSTALL_IMAGE}" -boot order=d',
}

VICE_x64 = {
    name = 'VICE/x64',
    emulator = VICE,
    start_command = 'x64 -config vicerc -iecdevice8 -device8 1 -fs8 ${SYSTEM_IMAGE}',
    setup_command = 'x64 -config vicerc -iecdevice8 -device8 1 -fs8 ${SYSTEM_IMAGE}',
    install_command = 'echo "No install step.  Installation will happen during setup."',
}

VICE_xvic = {
    name = 'VICE/xvic',
    emulator = VICE,
    start_command = 'xvic -config vicerc -iecdevice8 -device8 1 -fs8 ${SYSTEM_IMAGE}',
    setup_command = 'xvic -config vicerc -iecdevice8 -device8 1 -fs8 ${SYSTEM_IMAGE}',
    install_command = 'echo "No install step.  Installation will happen during setup."',
}

--[[ Architectures ]]--

Amiga_500 = {
    name = "Amiga 500",
    emulator_mode = E_UAE_Amiga_500,
    create_system_image = function(funicular, size)
        execute(funicular, "mkdir -p ${SYSTEM_IMAGE}")
    end,
    create_dist_image = function(funicular, size)
        -- TODO: this is a little restrictive
        if size ~= '880' then
            print "Only supported sizes: 880"
            os.exit(1)
        end
        execute(funicular, "dd if=/dev/zero of=${DIST_IMAGE} bs=1K count=" .. size)
    end
}

i386 = {
    name = "i386",
    emulator_mode = QEMU_i386,
    create_system_image = function(funicular, size)
        execute(funicular, "dd if=/dev/zero of=${SYSTEM_IMAGE} bs=1M count=" .. size)
    end,
    create_dist_image = function(funicular, size)
        -- TODO: this is a little restrictive
        if size ~= '360' and size ~= '720' and size ~= '1440' then
            print "Only supported sizes: 360, 720 and 1440"
            os.exit(1)
        end
        execute(funicular, "dd if=/dev/zero of=${DIST_IMAGE} bs=1K count=" .. size)
    end
}

Commodore_64 = {
    name = "Commodore 64",
    emulator_mode = VICE_x64,
    create_system_image = function(funicular, size)
        execute(funicular, "mkdir -p ${SYSTEM_IMAGE}")
    end,
    create_dist_image = function(funicular, size)
        execute(funicular, "dd if=/dev/zero of=${DIST_IMAGE} bs=256 count=683")
    end
}

VIC_20 = {
    name = "VIC 20",
    emulator_mode = VICE_xvic,
    create_system_image = function(funicular, size)
        execute(funicular, "mkdir -p ${SYSTEM_IMAGE}")
    end,
    create_dist_image = function(funicular, size)
        execute(funicular, "dd if=/dev/zero of=${DIST_IMAGE} bs=256 count=683")
    end
}

--[[ Platforms ]]--

Commodore_64_Platform = {
    name = "Commodore 64 Platform",
    architecture = Commodore_64,
    install_image = 'blank.d64',
    setup_command = "sh ./populate.sh",
}

VIC_20_Platform = {
    name = "VIC 20 Platform",
    architecture = VIC_20,
    install_image = 'blank.d64',
    setup_command = "sh ./populate.sh",
}

AmigaDOS_1_3 = {
    name = "AmigaDOS 1.3",
    architecture = Amiga_500,
    install_image = 'Workbench_1.3.adf',
    setup_command = "sh ./populate.sh",
}

NetBSD_6 = {
    name = "NetBSD 6.1.4",
    architecture = i386,
    install_image = 'NetBSD-6.1.4-i386.iso',
    setup_command = "genisoimage -R -J -D -joliet-long -o ${SETUP_IMAGE} staging_area",
}

FreeDOS_1_1 = {
    name = "FreeDOS 1.1",
    architecture = i386,
    install_image = 'fd11src.iso',
    install_image_url = 'http://www.freedos.org/download/download/fd11src.iso',
    setup_command = "genisoimage -R -J -D -joliet-long -o ${SETUP_IMAGE} staging_area",
}

--[[========================================================================]]--

dump_table = function(prefix, t)
    local k, v
    for k, v in pairs(t) do
        if type(v) == 'table' then
            dump_table(prefix .. k .. '.', v)
        elseif type(v) == 'function' then
            print(prefix .. k .. ' = function()')
        else
            print(prefix .. k .. ' = ' .. v)
        end
    end
end


function exists(filename)
   local file = io.open(filename, "r")
   if file == nil then
       return false
   end
   io.close(file)
   return true
end


function basename(filename)
    return filename:match("^.+/(.-)$")
end


function execute(funicular, command)
    command = command:gsub("${SETUP_IMAGE}", funicular.setup_image or '')
    command = command:gsub("${SYSTEM_IMAGE}", funicular.system_image or '')
    command = command:gsub("${INSTALL_IMAGE}", funicular.install_image or
                           funicular.platform.install_image or '')
    command = command:gsub("${DIST_IMAGE}", funicular.dist_image or '')
    print(command)
    local exit_code = os.execute(command)
    if exit_code ~= 0 then
        os.exit(1)
    end
end


function fetch_distfile(funicular, url)
    filename = 'distfiles/' .. basename(url)
    if not exists(filename) then
        execute(funicular, 'wget ' .. url .. ' -O ' .. filename)
    end
end


function is_docked(funicular, url)
    local command = 'toolshelf.py resolve ' .. url
    print(command)
    local exit_code = os.execute(command)
    return (exit_code == 0)
end


function checkout_hg_repo(funicular, url)
    filename = 'distrepos/' .. basename(url)
    if not exists(filename) then
        if TOOLSHELF ~= nil then
            if not is_docked(funicular, url) then
                execute(funicular, 'toolshelf.py tether ' .. url)
            end
            th = '`toolshelf.py resolve ' .. url .. '`'
            execute(funicular, 'hg clone ' .. th .. ' ' .. filename)
        else
            execute(funicular, 'hg clone ' .. url .. ' ' .. filename)
        end
    else
        if TOOLSHELF ~= nil then
            execute(funicular, 'cd ' .. filename .. ' && hg pull -u')
        end
    end
end


function checkout_git_repo(funicular, url)
    filename = 'distrepos/' .. basename(url)
    if not exists(filename) then
        execute(funicular, 'git clone ' .. url .. ' ' .. filename)
    end
end


commands = {
    dump = function(funicular, arg)
        dump_table('', funicular)
    end,

    init = function(funicular, arg)
        size = arg[2]
        if size == nil then
            print "Usage: funicular init <size-in-megabytes>"
            os.exit(1)
        end
        if exists(funicular.system_image) then
            print(funicular.system_image .. " already exists!  Delete it first.")
            return
        end
        print("init... size: " .. size)
        funicular.platform.architecture.create_system_image(funicular, size)
    end,

    initdist = function(funicular, arg)
        size = arg[2]
        if size == nil then
            print "Usage: funicular initdist <size-in-kilobytes>"
            os.exit(1)
        end
        if exists(funicular.dist_image) then
            print(funicular.dist_image .. " already exists!  Delete it first.")
            return
        end
        print("initdist... size: " .. size)
        funicular.platform.architecture.create_dist_image(funicular, size)
    end,

    install = function(funicular, arg)
        print("installing " .. funicular.platform.name .. " onto system image...")
        if funicular.install_instructions then
            print [[

=========================
INSTALLATION INSTRUCTIONS
=========================
]]
            print(funicular.install_instructions)
        end
        execute(funicular, funicular.platform.architecture.emulator_mode.install_command)
    end,

    setup = function(funicular, arg)
        print("creating setup image for " .. funicular.platform.name .. "...")

        execute(funicular, 'mkdir -p distfiles')
        for url in string.gmatch(funicular.distfiles or "", "[^%s]+") do
            fetch_distfile(funicular, url)
        end

        execute(funicular, 'mkdir -p distrepos')
        for url in string.gmatch(funicular.distrepos_hg or "", "[^%s]+") do
           checkout_hg_repo(funicular, url)
        end
        for url in string.gmatch(funicular.distrepos_git or "", "[^%s]+") do
            checkout_git_repo(funicular, url)
        end

        execute(funicular, 'rm -rf staging_area')
        execute(funicular, 'mkdir -p staging_area')
        for url in string.gmatch(funicular.distfiles or "", "[^%s]+") do
            local source_name = basename(url)
            local dest_name = source_name
            if funicular.name_map and funicular.name_map[source_name] then
                dest_name = funicular.name_map[source_name]
            end
            execute(funicular, 'cp -p distfiles/' .. source_name .. ' staging_area/' .. dest_name)
        end
        for url in string.gmatch(funicular.distrepos_hg or "", "[^%s]+") do
            if funicular.toolshelf_inside then
                execute(funicular, 'cd distrepos && tar zcf ../staging_area/bitbucket.org,catseye,' ..
                        basename(url) .. '-master.tar.gz ' .. basename(url))
            else
                local source_name = basename(url)
                local dest_name = source_name
                if funicular.name_map and funicular.name_map[source_name] then
                    dest_name = funicular.name_map[source_name]
                end
                execute(funicular, 'cd distrepos && tar zcf ../staging_area/' ..
                        dest_name .. '.tgz ' .. source_name)
            end
        end
        for url in string.gmatch(funicular.distrepos_git or "", "[^%s]+") do
            if funicular.toolshelf_inside then
                execute(funicular, 'cd distrepos && tar zcf ../staging_area/github.com,catseye,' ..
                        basename(url) .. '-master.tar.gz ' .. basename(url))
            else
                execute(funicular, 'cd distrepos && tar zcf ../staging_area/' ..
                        basename(url) .. '.tgz ' .. basename(url))
            end
        end

        if exists('skel') then
            execute(funicular, 'cp -Rp skel/* staging_area/')
            -- execute(funicular, 'chmod 755 staging_area/*.sh')
        end

        execute(funicular, funicular.platform.setup_command or 'echo "no platform setup command"')
        if funicular.setup_instructions then
            print [[

==================
SETUP INSTRUCTIONS
==================
]]
            print(funicular.setup_instructions)
        end
        execute(funicular, funicular.platform.architecture.emulator_mode.setup_command)
    end,

    start = function(funicular, arg)
        print("starting " .. funicular.platform.architecture.emulator_mode.emulator.name .. "...")
        if funicular.dist_image and exists(funicular.dist_image) then
            if funicular.dist_instructions then
                 print [[

=========================
DISTRIBUTION INSTRUCTIONS
=========================
]]
                 print(funicular.dist_instructions)
            end
            execute(funicular, funicular.platform.architecture.emulator_mode.dist_command)
        else
            execute(funicular, funicular.platform.architecture.emulator_mode.start_command)
        end
    end,

    distboot = function(funicular, arg)
        if not funicular.dist_image or not exists(funicular.dist_image) then
            print "No distribution image defined or created."
            return
        end
        print("distbooting " .. funicular.platform.architecture.emulator_mode.emulator.name .. "...")
        execute(funicular, funicular.platform.architecture.emulator_mode.distboot_command)
    end,

    backup = function(funicular, arg)
        bup = arg[2]
        if bup == nil then
            print "Usage: funicular backup <backup-basename>"
            os.exit(1)
        end

        bup_img_gz = bup .. '.img.gz'
        if exists(bup_img_gz) then
            print(bup_img_gz .. ' already exists!  Delete it or pick a different name.')
            os.exit(1)
        end

        -- special case is special
        if funicular.system_image == 'dh0' then
            bup_img_gz = bup .. '.tar.gz'
            execute(funicular, "tar zcf " .. bup_img_gz .. " ${SYSTEM_IMAGE}")
        else
            execute(funicular, "ls -lah ${SYSTEM_IMAGE}")
            execute(funicular, "cp ${SYSTEM_IMAGE} " .. bup .. ".img")
            execute(funicular, "gzip " .. bup .. ".img")
        end
        execute(funicular, "ls -lah " .. bup_img_gz)
    end,

    restore = function(funicular, arg)
        local force = false
        bup = arg[2]
        if bup == '-f' then
            force = true
            bup = arg[3]
        end
        if bup == nil then
            print "Usage: funicular restore [-f] <backup-basename>"
            os.exit(1)
        end

        bup = bup:gsub("^(.-)\.img\.gz$", "%1")

        bup_img_gz = bup .. '.img.gz'
        -- special case is special
        if funicular.system_image == 'dh0' then
            bup_img_gz = bup .. '.tar.gz'
        end

        if not exists(bup_img_gz) then
            print(bup_img_gz .. ' does not exist.')
            os.exit(1)
        end

        if force then
            execute(funicular, "rm -rf ${SYSTEM_IMAGE}")
        end

        if exists(funicular.system_image) then
            print(funicular.system_image .. ' still exists, please delete it first.')
            os.exit(1)
        end

        -- special case is special
        if funicular.system_image == 'dh0' then
            execute(funicular, "tar zxf " .. bup_img_gz)
        else
            execute(funicular, "ls -lah " .. bup_img_gz)
            execute(funicular, "cp " .. bup_img_gz .. " ${SYSTEM_IMAGE}.gz")
            execute(funicular, "gunzip ${SYSTEM_IMAGE}.gz")
        end
    end,
}

--[[-- MAIN --]]--

local command = arg[1]
if command == nil or commands[command] == nil then
    print [[Usage: funicular <command>
where <command> is one of:
    init
    install
    setup
    start
    initdist
    distboot
    backup
    restore]]
    os.exit(1)
end

local result, error = loadfile("Funicularfile")
if result == nil then
    print("No valid Funicularfile found in this directory.")
    os.exit(1)
end
local funicular = result()

command = commands[command]
command(funicular, arg)