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

class ChntpwScreen	
	def initialize(extlayers, button)
		lang = ENV['LANGUAGE'][0..1]
		lang = ENV['LANG'][0..1] if lang.nil?
		lang = "en" if lang.nil?
		@tl = MfsTranslator.new(lang, "ChntpwScreen.xml")
		@layers = Array.new
		#### Global variables
		# Combobox for selection of partition
		@drivecombo = nil
		@partlist = Array.new
		#### Combobox for selection of Users
		@usercombo = nil
		@userlist = Array.new
		@humanusers = Array.new
		
		###### First layer for selection of windows installation
		
		fixed = Gtk::Fixed.new
		text5 = Gtk::Label.new
		text5.width_request = 230
		text5.wrap = true
		text5.set_markup("<span color='white'>" + @tl.get_translation("next") + "</span>")
		forw  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		
		orangetile = Gtk::Image.new("chntpwgreen.png")
		# reread = Gtk::EventBox.new.add Gtk::Image.new("buttongreen.png")
		# reread = Gtk::Button.new("Neu einlesen")
		# reread.width_request = 120
		# reread.height_request = 32
		@drivecombo = Gtk::ComboBox.new
		# @drivecombo.append_text("Yoyodyne Frobolator SATA Partition 2 500GB (sda2)") 
		# @drivecombo.active = 0
		@drivecombo.height_request = 32
		@drivecombo.width_request = 510
		deltext = TileHelpers.create_label("<b>" + @tl.get_translation("winhead") + "</b>\n\n" + @tl.get_translation("winbody") , 510)
		
		fixed.put(forw, 853, 282)
		fixed.put(text5, 605, 288)
		fixed.put(orangetile, 0, 0)
		fixed.put(deltext, 130, 0)
		# fixed.put(reread, 520, 88)
		fixed.put(@drivecombo, 130, 108)
		TileHelpers.place_back(fixed, extlayers)
		extlayers["chntpwstart"] = fixed
		@layers[0] = fixed
		button.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				# reread drives here!
				@partlist = find_win_sysparts(@drivecombo)
				extlayers.each { |k,v|v.hide_all }
				extlayers["chntpwstart"].show_all
			end
		}
		forw.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				if @partlist.size < 1 
					TileHelpers.error_dialog(@tl.get_translation("nowinbody"), @tl.get_translation("nowinhead"))
				else
					# reread users here!
					@userlist, @humanusers = get_nt_users(@partlist[@drivecombo.active], @usercombo) 
					if @userlist.size < 1
						TileHelpers.error_dialog(@tl.get_translation("nouserbody"), @tl.get_translation("nouserhead"))
					else
						extlayers.each { |k,v|v.hide_all }
						extlayers["chntpwuser"].show_all
					end
				end
			end
		}
		
		###### Second layer for selection of user
		fixeduser = Gtk::Fixed.new
		text6 = Gtk::Label.new
		text6.width_request = 230
		text6.wrap = true
		text6.set_markup("<span color='white'>" + @tl.get_translation("resetnow") + "</span>")
		forw2  = Gtk::EventBox.new.add Gtk::Image.new("forward.png")
		orange2tile = Gtk::EventBox.new.add Gtk::Image.new("chntpwgreen.png")
		usertext = TileHelpers.create_label("<b>" + @tl.get_translation("userhead") + "</b>\n\n" + @tl.get_translation("userbody") , 510)
		TileHelpers.place_back(fixeduser, extlayers)
		@usercombo = Gtk::ComboBox.new
		@usercombo.append_text("Heinz Mustermann") 
		@usercombo.active = 0
		@usercombo.height_request = 32
		@usercombo.width_request = 510
		fixeduser.put(forw2, 853, 282)
		fixeduser.put(text6, 605, 288)
		fixeduser.put(usertext, 130, 0)
		fixeduser.put(orange2tile, 0, 0)
		fixeduser.put(@usercombo, 130, 108)
		
		extlayers["chntpwuser"] = fixeduser
		@layers[1] = fixeduser
		
		forw2.signal_connect('button-release-event') { |x, y|
			if y.button == 1 
				run_chntpw(@partlist[@drivecombo.active], @userlist[@usercombo.active])
				# TileHelpers.success_dialog( @tl.get_translation("successbody").gsub("USERNAME", "Heinz Mustermann").gsub("WINPARTITION", "sda1"), @tl.get_translation("successhead") )
				TileHelpers.success_dialog( @tl.get_translation("successbody").gsub("USERNAME", @humanusers[@usercombo.active]).gsub("WINPARTITION", @partlist[@drivecombo.active].device), @tl.get_translation("successhead") )
				extlayers.each { |k,v|v.hide_all }
				extlayers["start"].show_all
			end
		}
	end
	
	attr_reader :layers
	
	def find_win_sysparts(combobox)
		drives = Array.new
		auxdrives = Array.new
		ntparts = Array.new 
		100.downto(0) { |i| 
			begin
				combobox.remove_text(i) 
			rescue
			end
		}
		Dir.entries("/sys/block").each { |l|
			if l =~ /[a-z]$/ 
				d = MfsDiskDrive.new(l, false)
				auxdrives.push(d) 
			end
		}
		# mountpoint = "/var/run/lesslinux/" + $$.to_s + "/testmount"
		# system("mkdir -p #{mountpoint}") unless  File.exist?(mountpoint)
		auxdrives.each { |d|
			d.partitions.each { |p|
				# p.mount("ro", mountpoint)
				winpart, winvers = p.is_windows
				#if File.exist?("#{mountpoint}/Windows/System32/config/SAM") ||
				#		File.exist?("#{mountpoint}/WINDOWS/system32/config/SAM")
				if winpart == true 
					ntparts.push p
					label = "#{winvers} - #{d.vendor} #{d.model}"
					if d.usb == true
						label = label + " (USB) "
					else
						label = label + " (SATA/eSATA/IDE) "
					end
					label = label + "#{p.device} #{p.human_size}"
					combobox.append_text label
					p.umount 
				end
			}
		}
		# combobox.append_text("Windows 8.1 auf sdb1 (SATA/eSATA/IDE)") 
		combobox.active = 0 
		return ntparts
	end

	def get_nt_users(p, combobox)
		winpart, winvers = p.is_windows
		return nil if winpart == false
		100.downto(0) { |i| 
			begin
				combobox.remove_text(i) 
			rescue
			end
		}
		p.mount("rw")
		samusers = Array.new
		clearusers = Array.new
		
		auxsam = p.samfile
		winusers = Array.new 		
		winusers.push(auxsam.read_users)
		winusers.each { |w|
			w.each { |u|
				samusers.push(u[0])
				clearusers.push(u[1])
				combobox.append_text("#{u[1]} (0x#{u[0]})")
			}
		}
		p.umount
		combobox.active = 0 
		return samusers, clearusers
	end

	def run_chntpw(partition, user)
		puts partition.device
		puts user
		partition.mount("rw") 
		mountpoint = partition.mount_point[0] 
		samfile = ""
		[ "#{mountpoint}/Windows/System32/config/SAM", 
			"#{mountpoint}/WINDOWS/system32/config/SAM" ].each { |s|
			samfile = s if File.exist?(s)
		}
		# valid_reg = system('echo -e "q\n" | chntpw -e ' + reg_file) 
		exstring = "printf \"1\\n\\ny\\n\" | chntpw -u 0x" + user + " " + samfile
		# exstring = 'echo -e "1\ny\n" | chntpw -u 0x' + user + ' ' + samfile
		puts exstring
		retval = system(exstring)
		partition.umount 
		puts retval.to_s
		return retval
	end
	
	
	
end