;ｖｍマシンを作成
;作成者:金忠傑
;key

include 'ladder-com.ttl'

field_index=0
field_count=15
strdim field field_count

cmds_index=0
cmds_count=0
cmds_max=10
strdim cmds cmds_max

svn_index=0
svn_count=0
svn_max=0
strdim svn_name  20
strdim svn_cmds  20



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;=========================================================================
timeout = 3000

; ファイルオープン
fileopen fhandle 'server-list.txt' 0
if fhandle=-1 then
	message="他のeditorで排他モードでVM-list.txtを開いてないかを確認ください"
	messagebox  message 'VM-list.txt読み込みエラー'
	exit
endif
comment_flag=0
while 1
	call read_setting
endwhile

:fclose
call setting_all

; ファイルクローズ
fileclose fhandle
;fileclose flog_handle

exit
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;setting_all
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:setting_all
	call connet_server
	call su_root

	sendln "mkdir -p /mnt/DISK1"
	wait server_prompt

	sendln "mkdir -p /mnt/tools"
	wait server_prompt

:_get_tar
	sendln   'ssh manager@nas-27-59-FD  "cat  /data/svn_bak/tftpboot_bk/raspi_tftp.tar.gz"  | tar zxvf  - -C /'
	Wait "(yes/no)?" "password" "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED"
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "yes"
		Wait "password"
	elseif result=2  then
	elseif result=3  then
		sprintf  'ssh-keygen -R %s'  'nas-27-59-FD'
		sendln inputstr
		Wait '$'
		goto  _get_tar
	else
	endif

	sendln '129921'
	wait server_prompt

	sendln 'mount -v  nas-27-59-FD:/data/tmp/raspbrreyPi/mnt_tools   /mnt/tools'
	wait server_prompt
	
	sendln 'cd  /mnt/tools/raspberryPi'
	wait server_prompt
	
	sendln 'cp -p  smb.conf   /etc/samba/smb.conf'
	wait server_prompt
	
	sendln 'bash dhcp_tftp_setup.sh r'
	wait server_prompt

	sendln 'bash setup_os_samba.sh'
	wait server_prompt
	/*
	_name='svn-win'
	call svn_checkout
	*/
	
	call exit_server

return
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;connet_server
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:connet_server

msg = 'ssh '
strconcat msg server_user
strconcat msg '@'
strconcat msg server_ip

:_login
sendln msg
Wait "(yes/no)?" "password" "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED"
if result=0 then
	call timeout_
elseif result=1  then
	sendln "yes"
	Wait "password"
elseif result=2  then
elseif result=3  then
	sprintf  'ssh-keygen -R %s' server_ip
	sendln inputstr
	Wait '$'
	goto  _login
else
endif

sendln server_password

sendln 'date'
sprintf  '%s@(\S+):~' server_user
waitregex inputstr
if result=0 then
	call timeout_
else
	server_name=groupmatchstr1
	sprintf  '%s@%s' server_user server_name
	server_prompt=inputstr
	sprintf  '%s@%s' server_user server_name
	server_reg_prompt=inputstr
endif
sendln 'not="NOT";exist="EXIST"'
wait server_prompt
sendln
wait server_prompt
sendln
wait server_prompt
pause 1
flushrecv
return

:exit_server

	sendln 'exit'
	Wait server_ip
	Wait 'closed.'
	Wait '$'
return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;su user
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:su_root
	sendln "su - root"
	wait 'Password:'
	sendln '129921'
  
	sendln 'date'
	sprintf  '%s@(\S+):~' "root"
	waitregex inputstr
	if result=0 then
		call timeout_
	else
		server_name=groupmatchstr1
		sprintf  '%s@%s' "root" server_name
		server_prompt=inputstr
		sprintf  '%s@%s' "root" server_name
		server_reg_prompt=inputstr
	endif
	sendln 'not="NOT";exist="EXIST"'
	wait server_prompt
	sendln
	wait server_prompt
	sendln
	wait server_prompt
	pause 1
	flushrecv
return
:exit_su
	sendln 'exit'
	Wait 'logout'
	Wait '$'
return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;svn_checkout
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:svn_checkout
	call get_svn_cmd
	pause 1
	flushrecv
	sendln checkout_url
	Wait "(R)eject, accept (t)emporarily or accept (p)ermanently?" "Authentication realm:" server_prompt
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "p"
		Wait "Authentication realm:"
		Wait "Password for"
		Wait "':"
		sendln server_password

		Wait "Username:"
		sendln svn_user
		Wait "Password for"
		Wait "':"
		sendln svn_password
		
		Wait  "Store password unencrypted (yes/no)?"
		sendln "yes"
		
	elseif result=2  then
		Wait "Password for"
		Wait "':"
		sendln server_password

		Wait "Username:"
		sendln svn_user
		Wait "Password for"
		Wait "':"
		sendln svn_password
		
		Wait  "Store password unencrypted (yes/no)?"
		sendln "yes"
	else
		sendln
	endif

	wait "Please type 'yes' or 'no':" "Store password unencrypted (yes/no)?" server_prompt
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "yes"
	elseif result=2  then
		sendln "yes"
	elseif result=3  then
		sendln
	endif
	wait server_prompt
	
	sendln
	wait server_prompt

return
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;tar zxvf gz
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:tar_zxvf_gz
	pause 1
	flushrecv
	sendln cmd_cat_tar_zxvf
	Wait "(yes/no)?" "password:"
	if result=0 then
		call timeout_
	elseif result=1  then
		sendln "yes"
		Wait "password:"
		sendln "129921"
	elseif result=2  then
		sendln "129921"
	else
	endif
	wait server_prompt
	sendln
	wait server_prompt
return
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;tar zxvf gz
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:get_svn_cmd
	if svn_max>0 then
		for i 0 svn_max-1
			strcompare  _name   svn_name[i]
			if result==0 then
				checkout_url=svn_cmds[2]
				return
			endif
		next
	endif
return
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;tar zxvf gz
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{
:sed_cmd
	if cmds_count>0 then
		for i 0 cmds_count-1
			cmd=cmds[i]
			strlen cmd
			if result !=0 then
				strconcat   cmd  "  " 
				strconcat   cmd  sed_file
				sendln cmd
				Wait server_prompt
			endif
		next
	endif
return
}


; ファイルオープン

;setting_flag:1,2
;read_setting
{
:read_setting

	filereadln fhandle line
	if result goto fclose
	; 一行読み込み

	strtrim line ' 	'
	strlen line
	if result =0 then
		goto read_setting
	endif

	strscan line '#'
	if result=1 goto read_setting
	strscan line '--'
	if result=1 goto read_setting
	strscan line '=='
	if result=1 goto read_setting
	strscan line '**'
	if result=1 goto read_setting
	
	strscan line '*/'
	if result ==1 then
		comment_flag=0
		goto read_setting
	endif

	if comment_flag=1 then
		goto read_setting
	endif

	strscan line '/*'
	if result ==1 then
		comment_flag=1
		goto read_setting
	endif
	
	;messagebox  line '-----line1----'
	call read_line


	;;  match  xxx=yyy
	strmatch field[1] '^svn-.*$'
	if result >0 then
		;pause 5
		svn_name[svn_max]=field[1]
		svn_cmds[svn_max]=field[2]
		svn_max++
		return
	endif

	strmatch field[1] '^(\d+\.\d+\.\d+\.\d+)$'
	if result >0 then
		setting_flag=2
		;messagebox  field1 '---------'
		call init_server_ip
		server_ip=field[1]
		server_user=field[2]
		server_password=field[3]
	endif

return

; ファイルオープン
:init_server_ip
	server_ip=""
	server_user=""
	server_password=""
return
:init_cmd
	for j 0 cmds_max-1
		cmds[j]=""
	next
	cmd_count=0
return
:read_line

	; 読み込んだ行を表示
	strsplit  line ',' 8
	strtrim groupmatchstr1 ' 	'
	field[1]=groupmatchstr1
	strtrim groupmatchstr2 ' 	'
	field[2]=groupmatchstr2
	strtrim groupmatchstr3 ' 	'
	field[3]=groupmatchstr3
	strtrim groupmatchstr4 ' 	'
	field[4]=groupmatchstr4
	strtrim groupmatchstr5 ' 	'
	field[5]=groupmatchstr5
	strtrim groupmatchstr6 ' 	'
	field[6]=groupmatchstr6
	strtrim groupmatchstr7 ' 	'
	field[7]=groupmatchstr7
	strtrim groupmatchstr8 ' 	'
	remaining=groupmatchstr8
	strsplit  remaining ',' 8
	strtrim groupmatchstr1 ' 	'
	field[8]=groupmatchstr1
	strtrim groupmatchstr2 ' 	'
	field[9]=groupmatchstr2
	strtrim groupmatchstr3 ' 	'
	field[10]=groupmatchstr3
	strtrim groupmatchstr4 ' 	'
	field[11]=groupmatchstr4
	strtrim groupmatchstr5 ' 	'
	field[12]=groupmatchstr5
	strtrim groupmatchstr6 ' 	'
	field[13]=groupmatchstr6
	strtrim groupmatchstr7 ' 	'
	field[14]=groupmatchstr7
	strtrim groupmatchstr8 ' 	'
	remaining=groupmatchstr8
return
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;time out 処理
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
{

exit
:timeout_
	sendln
return
:timeout_exit_
	sendln
exit
}

;send #29



