#
######################################################
#
#  SaVi by Robert Thurman (thurman@geom.umn.edu) and
#          Patrick Worfolk (worfolk@alum.mit.edu).
#
#  Copyright (c) 1997 by The Geometry Center.
#  This file is part of SaVi.  SaVi is free software;
#  you can redistribute it and/or modify it only under
#  the terms given in the file COPYRIGHT which you should
#  have received along with this file.  SaVi may be
#  obtained from:
#  http://savi.sourceforge.net/
#  http://www.geom.uiuc.edu/locate/SaVi
#
######################################################
#
# save.tcl
#
# $Id: save.tcl,v 1.8 2005/02/04 17:58:52 lloydwood Exp $

proc save(build) {} {
    global Save last_filename

    set types {
        {"SaVi simulation scripts"  {.tcl} }
    }

    set filename [tk_getSaveFile -filetypes $types \
	          -title "SaVi: save satellites" ]

    if {"$filename" == ""} return

    set length [string length "$filename"]
    set last4 [string range "$filename" [expr $length - 4] $length]

    if {[string compare $last4 ".tcl"] != 0} {
	set filename "$filename.tcl"
    }

    # open file
    set f [open "$filename" w]
    if {$f == ""} {
	puts stderr "SaVi: couldn't save satellites to $filename"
	return
    }

    puts $f "\# SaVi saved satellites"
    puts $f "\# http://savi.sourceforge.net/"

    # put a wrapper around satellite creation
    puts $f "satellites GV_BEGIN"

    # write satellites
    set n [.main.cmd.lb size]
    for {set i 0} {$i < $n} {incr i} {
	set noe [satellites GET $i]
	puts -nonewline $f "satellites LOAD "
	puts $f $noe
    }

    # close file
    puts $f "satellites GV_END"
    close $f

    puts stderr "SaVi: saved satellites to $filename"

    set last_filename "$filename"

    main(title) "$filename"
}
