1#! /usr/bin/env tclkit
2
3# Split starkit/starpack file in two, create name.head and name.tail
4# derived from mkinfo.tcl code
5# Jan 2004, jcw@equi4.com
6
7proc fail {msg} { puts stderr "${::filename}: $msg"; exit 1 }
8
9if {[llength $argv] < 1} {
10  set filename usage
11  fail "$argv0 file ..."
12}
13
14foreach filename $argv {
15  if {![file exists $filename]} {
16    fail "file does not exist"
17  }
18  if {![file isfile $filename]} {
19    fail "this is not a regular file (perhaps mounted as VFS?)"
20  }
21  set end [file size $filename]
22  if {$end < 27} {
23    fail "file too small, cannot be a datafile"
24  }
25
26  set fd [open $filename]
27  fconfigure $fd -translation binary
28  seek $fd -16 end
29  binary scan [read $fd 16] IIII a b c d
30
31 #puts [format %x-%d-%x-%d $a $b $c $d]
32
33  if {($c >> 24) != -128} {
34    fail "this is not a Metakit datafile"
35  }
36
37  # avoid negative sign / overflow issues
38  if {[format %x [expr {$a & 0xffffffff}]] eq "80000000"} {
39    set start [expr {$end - 16 - $b}]
40  } else {
41    # if the file is in commit-progress state, we need to do more
42    fail "this code needs to be finished..."
43  }
44
45  seek $fd $start
46  switch -- [read $fd 2] {
47    JL { set endian little }
48    LJ { set endian big }
49    default { fail "failed to locate file header" }
50  }
51
52  seek $fd 0 end
53  set remainder [expr {[tell $fd] - $start}]
54  seek $fd 0
55  set fn [file tail [file root $filename]]
56
57  set ofd [open $fn.head w]
58  fconfigure $ofd -translation binary
59  fcopy $fd $ofd -size $start
60  close $ofd
61  puts "$fn.head: $start bytes"
62
63  set ofd [open $fn.tail w]
64  fconfigure $ofd -translation binary
65  fcopy $fd $ofd -size $remainder
66  close $ofd
67  puts "$fn.tail: $remainder bytes"
68
69  close $fd
70}
71