#!/usr/bin/env ruby
#--
# Copyright (C) 2008 Harald Sitter <apachelogger@ubuntu.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License or (at your option) version 3 or any later version
# accepted by the membership of KDE e.V. (or its successor approved
# by the membership of KDE e.V.), which shall act as a proxy
# defined in Section 14 of version 3 of the license.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#--

require 'bat'
package_require 'net/ssh', 'libnet-ssh-ruby'
package_require 'net/sftp', 'libnet-sftp-ruby'

PKG = File.basename(Dir.pwd)

FILES = ["#{PKG}*changes", "#{PKG}*dsc", "#{PKG}*diff.gz", "deb.diff", "build/BUILDLOG"]

if $*[0] != nil and $*[0].include?(BD)
    @path = "#{REMOTEPATH}/#{BD}/#{PKG}/"
else
    @path = "#{REMOTEPATH}/#{PKG}/"
end

begin
    session = Net::SSH.start(HOST.split("@")[1], 22, HOST.split("@")[0])
    @sftp = session.sftp.connect
rescue Net::SSH::AuthenticationFailed
    err "I am very sorry, but the authentification on the remote host failed :-("
end

def put(f)
    if (file = avail_n_single(f))
        pt(file)
        @sftp.put_file(file,@path+File.basename(file))
    end
end

%x[ssh #{HOST} mkdir -p #{@path}]

for file in FILES
    put(file)
end

%x[ssh #{HOST} chmod 600 #{@path}/*changes]
%x[ssh #{HOST} cp #{REMOTETARPATH}/#{PKG}*.tar.gz #{@path}]

session.close # close ssh session and thus every channel (hopefully)

pt "#{PKG} done"
