#!/bin/bash - 
#
#===================================================================================
#
#     Copyright Beijing Linx Technology Co., Ltd. 2010年09月13日
#
#      Filename:  setup
#
#         USAGE:  ./setup 
#
#   Description:  Setup startup script
#
#       Version:  0.1
#       Created:  2010年09月13日
#
#        Author:  Hu Gang
#
#     ChangeLog:  1. 2010年09月13日  Create.
#
#===================================================================================
#
#===================================================================================
#Set variables
#===================================================================================
workdir="/usr/lib/new_install/text"
exec_file='xinitrc_setup'
work_write_dir='/var/install'
configfile="../xml/install_cfg.xml"
install_step_list_file="install_step_list.py"
#===================================================================================
#Performance function
#===================================================================================
usage(){
    echo "usage: $0 [-s|--style style][-g|--graph][-l|--language language][-n|--noXserver][-p|--skip-partition][-h|--help]"
    echo "style [text|graph]"
    echo "language [english|chinese]"
    echo "skip-partition text install but skip partition"
    echo "default text install"
}

config_execfile(){
    if test "$1" == "text"
    then
        echo "#!/bin/bash" > ${work_write_dir}/${exec_file}
        echo "cd ${workdir}" >> ${work_write_dir}/${exec_file}
        echo "python ri_main.py" >> ${work_write_dir}/${exec_file}
    else
        echo "#!/bin/bash" > ${work_write_dir}/${exec_file}
        echo "twm &" >> ${work_write_dir}/${exec_file}
        echo "cd ${workdir}" >> ${work_write_dir}/${exec_file}
        if [ -f ${work_write_dir}/install.xml ]; then 
        echo "python check_oldconf.py -l $1 " >> ${work_write_dir}/${exec_file}
        fi
        echo "python ri_main.py -l $2 -c $configfile" >> ${work_write_dir}/${exec_file}
    fi

    echo "#!/usr/bin/env python" > ${work_write_dir}/${install_step_list_file}

    if test "$skip_partition" == "yes"
    then
	echo "list = ['welcome','mountpoint','serialnumber','network','group','service','finishstate','information']" >> ${work_write_dir}/${install_step_list_file}
    else
	echo "list = ['welcome','partition','raid','mountpoint','serialnumber','network','group','service','finishstate','information']" >> ${work_write_dir}/${install_step_list_file}
    fi
}
      
main(){
    local style='text'
    local language='chinese'
    local xstart='no'
    local skip_partition='no'
    #local dialog=''

    #tmp=$(getopt -o c:dsl:nh --long style:,configfile:,language:,noXserver,help -- "$@")
    tmp=$(getopt -o c:sgl:nph --long configfile:,style,graph,language:,noXserver,skip-partition,help -- "$@")
    eval set -- "$tmp"

    mkdir -p $work_write_dir
    
    while true;do
	case "$1" in
        '-c'|'--configfile') configfile="$2" ; shift 2;;
        '-g'|'--graph') xstart='yes';style="graph" ;workdir="/usr/lib/new_install/interface";shift ;;
        '-s'|'--style') xstart='no';style="text" ;workdir="/usr/lib/new_install/text";shift ;;
	    '-l'|'--language') language="$2" ;shift 2;;
	    '-n'|'--noXserver') xstart='no' ;shift ;;
	    '-p'|'--skip-partition') skip_partition='yes' ;shift ;;
	    '-h'|'--help') usage ; exit 0 ;;
	    '--') shift; break ;;
	    *) usage ; exit 1 ;;
	esac
    done
    
    if [ $style = 'graph' ]; then
	      config_execfile  "$style" "$language"
    else
	      config_execfile  "$style"
    fi

    if [ $xstart = 'yes' ]; then
        X -configure
        startx ${work_write_dir}/${exec_file}  -- -config ~/xorg.conf.new
	ret=$?
	if [ $ret -ne 0 ];then
		xstart='no';style="text" ;workdir="/usr/lib/new_install/text";
		config_execfile  "$style"
		bash ${work_write_dir}/${exec_file}
	fi
    else
	    bash ${work_write_dir}/${exec_file} 
    fi
}

#===================================================================================
#Execution
#===================================================================================
main "$@"

