#!/usr/bin/ruby
# encoding: utf-8

class SambaScreen	
	def initialize(extlayers, button, nwscreen)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@tl = MfsTranslator.new(lang, "SambaScreen.xml")
		@layers = Array.new
		# First panel with warning
		infopane = Gtk::Label.new
		infopane.width_request = 510
		infopane.wrap = true
		infopane.set_markup("<span foreground='white'><b>" + @tl.get_translation("sambahead") + "</b>\n\n" + @tl.get_translation("sambabody") + "</span>")
		fixed = Gtk::Fixed.new
		
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		forwtext = TileHelpers.create_label(@tl.get_translation("gotoshare"), 220)
		
		text4 = Gtk::Label.new
		text4.width_request = 250
		text4.wrap = true
		text4.set_markup("<span color='white'>" + @tl.get_translation("cancel") + "</span>")
		back = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		
		infogreen = Gtk::Image.new("sambagreen.png")
		infored = Gtk::EventBox.new.add Gtk::Image.new("sambagreen.png")
		
		fixed.put(infogreen, 0, 0)
		# fixed.put(infored, 0, 130)
		fixed.put(infopane, 135, 20)
		# fixed.put(cancelpane, 135, 170)
		fixed.put(text4, 605, 338)
		fixed.put(back, 852, 332)
		fixed.put(forw, 853, 282)
		fixed.put(forwtext, 605, 288)
		
		extlayers["sambaquest"] = fixed
		@layers[0] = fixed
		
		#################################
		
		# Second panel stop server
		fixed2 = Gtk::Fixed.new
		stoppane = Gtk::Label.new
		stoppane.width_request = 510
		stoppane.wrap = true
		stoppane.set_markup("<span foreground='white'><b>"+ @tl.get_translation("shareactivehead") + "</b>\n\n" + @tl.get_translation("shareactivebody") + "</span>")
		
		
		
		stopbutton = Gtk::Image.new("sambagreen.png")
		cancelbutton = TileHelpers.place_back(fixed2, extlayers, false)
		
		text5 = Gtk::Label.new
		text5.width_request = 250
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("cancel") +"</span>")
		back2 = Gtk::EventBox.new.add Gtk::Image.new("cancel.png")
		
		fixed2.put(stoppane, 135, 32)
		fixed2.put(stopbutton, 0, 0)
		fixed2.put(text5, 605, 338)
		fixed2.put(back2, 852, 332)
		
		extlayers["sambarunning"] = fixed2
		@layers[1] = fixed2
		
		button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				if nwscreen.test_link == false
					nwscreen.nextscreen = "sambaquest"
					nwscreen.fill_wlan_combo 
					# Check for networks first...
					extlayers["networks"].show_all
				else
					extlayers["sambaquest"].show_all
				end
			end
		}
		infored.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["start"].show_all
			end
		}
		back.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				extlayers["start"].show_all
			end
		}
		cancelbutton.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				puts "Stopping SAMBA"
				extlayers["start"].show_all
			end
		}
		back2.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				puts "Stopping SAMBA"
				system("killall -15 nmbd")
				system("killall -15 smbd")
				sleep 2
				system("killall -9 nmbd")
				system("killall -9 smbd")
				TileHelpers.umount_all 
				extlayers["start"].show_all
			end
		}
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				extlayers.each { |k,v|v.hide_all }
				puts "Starting SAMBA"
				mount_all
				system("nmbd -D")
				system("smbd -D")
				extlayers["sambarunning"].show_all
			end
		}
		stoppane.signal_connect('show') { 
			stoppane.set_markup("<span foreground='white'><b>" + @tl.get_translation("shareactivehead") + "</b>\n\n" + @tl.get_translation("shareactivebody").gsub("JOINEDADRESSES", nwscreen.get_addresses.join(" oder ")) + "</span>")
		}
	end
	
	attr_reader :layers
	
	def mount_all(writeable_part=nil)
		drives = Array.new
		Dir.entries("/sys/block").each { |l|
			drives.push(MfsDiskDrive.new(l, false)) if l =~ /[a-z]$/ 
		}
		drives.each { |d|
			d.partitions.each { |p|
				if p.device == writeable_part
					system("mkdir -p /media/" + @tl.get_translation("backupdir")) 
					p.mount("rw", "/media/" + @tl.get_translation("backupdir"), 1000, 1000)
				else
					system("mkdir -p /media/#{p.device}")
					p.mount("ro", "/media/#{p.device}", 1000, 1000)
				end
			}
		}
	end
	
end