// TfuConvert.bcpl -- converts KD from version 1 to version 2
// Copyright Xerox Corporation 1979, 1980

//	Last modified June 14, 1980  3:06 PM by Taft

get "altofilesys.d"
get "disks.d"
get "tfs.d"
get "tfsoldkd.d"
get "streams.d"

external
[
//outgoing procedures
TFUConvert

//incoming procedures
TFSInit; TFSClose
OpenFile; Closes; ReadBlock; WriteBlock; Resets; Ws
MoveBlock; Zero

//incoming statics
z
]

//---------------------------------------------------------------------------
let TFUConvert(drive) be
//---------------------------------------------------------------------------
[
let disk = TFSInit(z, false, drive, 0, true)
if disk eq 0 then [ Ws("Cannot operate the Trident disk.*n"); return ]
let dds = OpenFile("DiskDescriptor", 0, 0, verLatest,
 disk>>DSK.fpDiskDescriptor, 0, z, 0, disk)
if dds eq 0 then
   [ Ws("Cannot find DiskDescriptor.*n"); TFSClose(disk); return ]
let okd, nkd = vec lTFSKDHeader, vec lTFSKDHeader
Zero(okd, lTFSKDHeader)  //in case of shorter (very old) KD
ReadBlock(dds, okd, lTFSKDHeader)
if okd>>TFSKD.version eq TFSKDversion then
   [
   Ws("DiskDescriptor is already in new format.*n")
   Closes(dds); TFSClose(disk); return
   ]
if okd>>OTFSKD.version gr OTFSKDversion then
   [
   Ws("DiskDescriptor is in neither old nor new format.*n")
   Closes(dds); TFSClose(disk); return
   ]
MoveBlock(nkd, okd, lKDHeader)
Zero(nkd+lKDHeader, lTFSKDHeader-lKDHeader)
nkd>>TFSKD.version = TFSKDversion
nkd>>TFSKD.model = 80
nkd>>TFSKD.packID = okd>>OTFSKD.packID
MoveBlock(lv nkd>>TFSKD.VDAdiskDD, lv okd>>OTFSKD.VDAdiskDD, 4)
nkd>>TFSKD.nVTracks = okd>>OTFSKD.nTracks
MoveBlock(lv nkd>>TFSKD.nTransfers, lv okd>>OTFSKD.nTransfers, 12)
nkd>>TFSKD.lastPageAlloc = okd>>OTFSKD.lastPageAlloc
Resets(dds)
WriteBlock(dds, nkd, lTFSKDHeader)
Closes(dds)
TFSClose(disk)
Ws("DiskDescriptor conversion completed successfully.*n")
]