#!/usr/bin/python
# coding: utf-8
#
#       DESCRIPTION:    install for OS
#
#       SCRIPT NAME:    ri_install.py
#
#       Input:  
#                       [-d|--display display-mode ]   install display (tk or cli)
#                       [-l|--language language]       insatll display language (chinese or english)
#
#       MENTOR:         Li Zhi
#
#       AUTHOR:         Ling Fen
#
#       EMAIL:          fling@linx-info.com
#
#       DATE:           2010-09-20
import sys
import getopt
import ri_oper
import os
import os.path
import ri_data
import dialog
import ri_progress
import thread
import time
from snack import *
from time import sleep

sys.path.append('../new_partition/')
import partition_data as p_d

def install_over(ret):
    if ri_oper.Rate.value == 100 and ret == 0:
        ret_set_task_over = display.set_task_over()
        if ret_set_task_over == 'reboot':
            print "reboot ..."
            os.system("reboot")
        elif ret_set_task_over == 'shutdown':
            print "shutdown ..."
            os.system("sync")
            os.system("eject")
            os.system("echo o >/proc/sysrq-trigger")
        elif ret_set_task_over == 'nothing':
            display.root_destroy()

def show_progress():
    screen = SnackScreen()
    screen.drawRootText(0, 0, 'Rocky Security OS Install')
    screen.pushHelpLine(" ")
    g = GridForm(screen,"Rocky Install",1,2)
    txt = Label("Start Install...")
    progress = Scale(90,100)
    g.add(txt, 0, 0, anchorLeft=1)
    g.add(progress, 0, 1)
    while True:
        if ri_progress.i_percent >= 98 :
            break
            screen.finish()
        progress.set(ri_progress.i_percent)
        txt.setText(ri_progress.i_operation)
        g.draw()
        screen.refresh()
        time.sleep(0.1)
			

def main():
    global has_run
    if has_run: return
    has_run = True
    thread.start_new_thread(show_progress,())
    for instance in oper_list:
        ret = instance.install()
        if ret:
            display.error(instance, ret)
            break
        else:
            ri_oper.display_sub_operation((ri_oper.language=='chinese' \
                        and instance.chinese_name or instance.english_name)+\
                        (ri_oper.language=='chinese' and u' 成功\n' or ' success\n'))
    install_over(ret)

has_run = False

def print_usages(): print 'Usages: %s [-l|--language language] [-d|--display display-mode] [install_xml_file]' %sys.argv[0]

try:
    opts, args = getopt.getopt(sys.argv[1:], "l:d:", ["language=", "display="])
except getopt.GetoptError:
    print_usages()
    sys.exit(1)

if len(args)>0:
    ri_data.install_xml = args[0]

#ri_data.install_xml default value is set in ri_data.py
if os.path.isfile(ri_data.install_xml):
    ri_data.init_from_xml()
else:
    print 'error: install xml(%s) does not exist' %ri_data.install_xml
    sys.exit(1)

ri_oper.language='english'
disp = 'ri_inst_cli'
for opt,arg in opts:
    if opt in ("-l","--language"):
        ri_oper.language=arg.lower()
    elif opt in ("-d", "--display"):
        disp = 'ri_inst_%s' %arg.lower()
exec 'import %s as display' %disp

ri_oper.display_operation     = display.set_task
ri_oper.display_sub_operation = display.set_sub_task
ri_oper.display_scale         = display.set_task_scale

#oper_list = [ri_oper.MakePartitions(5),ri_oper.MakeRaid(3),ri_oper.Format(7),ri_oper.Mount(3),ri_oper.InstallPkg(49),ri_oper.MakeRaidConfigure(2),ri_oper.ConfigureFstab(5),ri_oper.GenerateIssue(3),ri_oper.ConfigureNetwork(2),ri_oper.MakeServiceAutoBoot(5),ri_oper.CopyKernel(7),ri_oper.ConfigureBootloader(5),ri_oper.BootLoader(2),ri_oper.ExecFinishInstall(2)]
oper_list = [ri_oper.MakePartitions(5),
		ri_oper.MakeRaid(4),
		ri_oper.Format(7),
		ri_oper.Mount(4),
		ri_oper.InstallPkg(49),
		ri_oper.MakeRaidConfigure(3),
		ri_oper.ConfigureFstab(5),
		ri_oper.GenerateIssue(4),
		ri_oper.ConfigureNetwork(3),
		ri_oper.MakeServiceAutoBoot(5),
		ri_oper.ConfigureBootloader(5),
		ri_oper.BootLoader(3),
		ri_oper.ExecFinishInstall(3)]

display.start(main)
