#!/bin/sh
#
# @package      hubzero-filexfer
# @file         filexfer
# @author       Michael McLennan <mmclennan@purdue.edu>
# @author       Derrick Kearney <dsk@purdue.edu>
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2004-2013 HUBzero Foundation, LLC.
# @license      http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2004-2013 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero(R) Platform for Scientific Collaboration
#
# The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

# ----------------------------------------------------------------------
#\
. /etc/environ.sh
#\
use -e -r rappture
#\
exec wish "$0" ${1+"$@"}
# ----------------------------------------------------------------------
# tclsh executes everything from here on...


if {[catch {package require Tclx}]} {
   proc kill {signal pid} {
      set status [catch {exec kill -$signal $pid} killOut]
   }
}

package require Itk

proc expandPath {args} {
    set path ""
    set dirs [file split [lindex $args 0]]

    while {[llength $dirs] > 0} {
        set d [lindex $dirs 0]
        set dirs [lrange $dirs 1 end]
        if {[catch {file link [file join $path $d]} out] == 0} {
            # directory d is a link, follow it
            set outdirs [file split $out]
            if {[string compare "/" [lindex $outdirs 0]] == 0} {
                # directory leads back to root
                # clear path
                # reset dirs list
                set path ""
                set dirs $outdirs
            } else {
                # relative path for the link
                # prepend directory to dirs list
                set dirs [concat $outdirs $dirs]
            }
        } else {
            set path [file join $path $d]
        }
    }
    return $path
}

proc cleanup {} {
   global uploadPID
   global downloadPID

   if {$uploadPID != 0} {
      kill 1 $uploadPID
      set uploadPID 0
   }
   if {$downloadPID != 0} {
      kill 1 $downloadPID
      set downloadPID 0
   }
}

proc exitProgram {} {
   set status [cleanup]
   exit
}

proc upload_button {} {
   global uploadPID
   global installdir

   if {$uploadPID != 0} {
      kill 1 $uploadPID
      set uploadPID 0
   }
   if {$uploadPID == 0} {
      set status [catch {exec importfile @USE_REMOTE@ > /dev/null &} uploadPID]
   }
}

proc download_button {} {
   global downloadPID
   global installdir

   if {$downloadPID != 0} {
      kill 1 $downloadPID
      set downloadPID 0
   }
   if {$downloadPID == 0} {
      set types {
         {{All Files} * }
      }
      set localFile [tk_getOpenFile -multiple 0 -title "Select Download File" -filetypes $types -initialdir [pwd]]
      if {$localFile != ""} {
         set status [catch {exec exportfile $localFile &} downloadPID]
      }
   }
}

wm protocol . WM_DELETE_WINDOW {
   set status [cleanup]
}

wm protocol . WM_DELETE_WINDOW { exit 0 }
wm title . {Import/Export}
package require Img

set installdir /usr/share/hubzero-filexfer

set doUpload   yes
set doDownload yes
set uploadPID 0
set downloadPID 0

while {[llength $argv] > 0} {
    set opt [lindex $argv 0]
    set argv [lrange $argv 1 end]

    switch -- $opt {
        {} {}
        --noUpload {
            set doUpload no
        }
        --noDownload {
            set doDownload no
        }
        default {
            puts stderr "bad option \"$opt\""
            exit 1
        }
    }
}

set image(upload) [image create photo -file $installdir/earth_upload-64.png]
set image(download) [image create photo -file $installdir/earth_download-64.png]
label .mesg1 -font "Arial 12 bold" \
   -text "Upload or download\nfile or clipboard."
label .mesg2 -font "Arial 10 bold" \
   -text "Popups must be enabled." -fg red3
button .upload   -text "Upload" \
   -font "Arial 8 bold" \
   -command "upload_button" -image $image(upload) -compound top
button .download -text "Download" \
   -font "Arial 8 bold" \
   -command "download_button" -image $image(download) -compound top
button .exit -text "Cancel" \
   -font "Arial 10 bold" \
   -command "exitProgram"

grid .mesg1 -column 0 -row 0 -columnspan 3 -sticky nsew -pady 4
grid .mesg2 -column 0 -row 2 -columnspan 3 -sticky nsew
if {$doUpload && $doDownload} {
    grid .upload -column 0 -row 1
    grid .download -column 1 -row 1
} elseif {$doUpload} {
    grid .upload -column 1 -row 1
} elseif {$doDownload} {
    grid .download -column 1 -row 1
}
grid .exit -column 0 -row 3 -columnspan 3
