#!/bin/sh
# A standard little trick \
exec tcl $0 $@
#
# Copyright 1998, NeoSoft Inc.  All rights reserved.
# Written by Randy Kunkee, NeoSoft Inc. 
#
# Permission to use, modify, and/or redistribute this script for any
# purpose including for financial gain is granted provided the above
# copyright remains in place and due credit is given in accompanying
# documentation to NeoSoft, Inc, Houston, TX.
#
# convertdata from to
#
# This script provides a means of exporting all data and db files
# from one version of NeoWebScript and importing them into another.
# Previous versions of the Neo package (7.6.0 and earlier and
# earlier versions of Neo-8.0.x) use db 2-* versions of the files,
# The data storage is incompatable.
#
# Use this script to perform the move.
# 
# When using the load option, the script checks to make sure that the
# "system" and "user" subdirectories do not already exist (meaning that
# you are about to accidentally slaughter your current files, which is
# probably not a good idea).
#
# Example:
#	convertdata /httpd/neoscript-data \
#		/usr/local/apache-nws-3.0/var/nws-data
#
# HOW IT WORKS:
#
# As the data directory tree is traversed, files which end in
# .db are assumed to be db-1.85 data files.  Db_dump185 is used
# to dump this data, and db_load is used to read the data in.
# Any file that does not end in ".db" is considered a data file,
# and is linked to the new directory.

proc convert {from to} {
    signal trap PIPE {puts stderr "aborted on SIGPIPE"; exit}
    cd $from
    set filesfp [open "|find system users -type f -print"]

    while {[gets $filesfp filename]>0} {
	set dir [file dirname $to/$filename]
	if ![file exists $dir] {
	    mkdir -path $dir
	}
	if [string match *.db $filename] {
	    if [catch {exec db_dump185 -p $filename | db_load $to/$filename} msg] {
		puts "Error converting $filename: $msg"
	    }
	} else {
	    link $filename $to/$filename"
	}
    }

    close $filesfp
}

proc doit {argv} {
    lassign $argv from to
    if {[llength $argv] != 2 || $from == "" || $to == ""} {
	global argv0
	puts stderr "Usage: $argv0 <fromdir> <todir>"
    }
    cd $to
    if {![lempty [glob -nocomplain system/*]] \
	|| ![lempty [glob -nocomplain users/*]]} {
	puts stderr "$to/{system|user}: data already exists there."
	exit 1
    }
    convert $from $to
}

if !$tcl_interactive {doit $argv}
