#!/bin/bash
VERSION="22.001.03.01.03"
install_exit()
{
    echo "Press any key to exit. " | tee -a ${LOG_PATH}
    read COMMAND
    exit
}

if [ `id | awk '{print $1}'` != "uid=0(root)" ]
then
    echo "You must run the install process by root." | tee -a ${LOG_PATH}
    install_exit
fi

#CURRENTPATH="`pwd`"
CURRENTPATH="$(dirname "$0")"
TEMPWORKPATH=""
INSTALL_PATH=""
LOG_PATH="/tmp/MobileBrServ_autoinstall_log"
PROCESS_NAME="mbbservice"
SYS_KERN_VER=`uname -r`

#if [ "$(pidof mbbservice)" != "" ]
#then 
#    kill -9 "$(pidof mbbservice)"
#fi

#echo "$CURRENTPATH"

if [ "$1" != "" ]
then
#echo "$1"
INSTALL_PATH="/usr/local/MobileBrServ"
else
INSTALL_PATH="/usr/local/MobileBrServ"
fi

ZIP_FILE="/7zr"
TMP_FILE_PATH="/tmp/MobileBrServ_AutoRun"
INSTALL_FILE="/linux_mbb_install"
SYSCONFIG_PATH="/ArConfig.dat"
INSTALL_SHELL="/install"
DATACARD_VERIFY="/DataCard_Verify"
MBB_BIN_FILE="/mbbservice.bin"
MBB_FILE="/mbbservice"
READ_ME_FILE="/readme.txt"
SYSCONFIG_VALUE="value"
SKELETON_NAME="runmbbservice"
START_SKELETON=S99$SKELETON_NAME 
STOP_SKELETON=K10$SKELETON_NAME
udevVer=""


LOG_PATH="/tmp/MobileBrServ_autoinstall_log"

#define the install variable
FIRST_INSTALL="NO"
INSTALL_OR_NOT="NO"

#define the common language
REINSTALL_CONFIRM="do you want to re-install it?"
DIFFERENT_VERSION_CONFIRM="Do you want ot install a different version of the application?"
NEW_VERSION_CONFIRM="DO you want to install a new version of the application?"

copy_local()
{
    echo "begin to copy file... " | tee -a ${LOG_PATH}
    if [ "${CURRENTPATH}" != "${TMP_FILE_PATH}${INSTALL_FILE}" ]
    then
    if [ -d "${TMP_FILE_PATH}${INSTALL_FILE}" ]
    then
    rm -f -R "${TMP_FILE_PATH}" | tee -a ${LOG_PATH}
    fi
    mkdir -p "${TMP_FILE_PATH}${INSTALL_FILE}" | tee -a ${LOG_PATH}
    chmod a+wr -R "${TMP_FILE_PATH}${INSTALL_FILE}"
    
    if [ "${CURRENTPATH}" = "." ]                     #if current path is . the .bin will copy failed
    then 
    cp -f -R "${CURRENTPATH}" "${TMP_FILE_PATH}${INSTALL_FILE}" | tee -a ${LOG_PATH}
    else
    cp -f -R "${CURRENTPATH}" "${TMP_FILE_PATH}" | tee -a ${LOG_PATH}
    fi
    fi

    chmod a+wrx -R "${TMP_FILE_PATH}${INSTALL_FILE}"

    if [ ! -f "${TMP_FILE_PATH}${INSTALL_FILE}${MBB_BIN_FILE}" ]
    then
    {
        echo "copy .bin file failed ..." | tee -a ${LOG_PATH}
        install_exit
    }
    fi
    
    mkdir -p "${TMP_FILE_PATH}${INSTALL_FILE}${MBB_FILE}" | tee -a ${LOG_PATH}
    chmod a+wrx "${TMP_FILE_PATH}${INSTALL_FILE}${MBB_FILE}"
    cd "${TMP_FILE_PATH}${INSTALL_FILE}${MBB_FILE}"
    "${TMP_FILE_PATH}${INSTALL_FILE}${ZIP_FILE}" x -y "${TMP_FILE_PATH}${INSTALL_FILE}${MBB_BIN_FILE}" | tee -a ${LOG_PATH} > /dev/null 2>&1
    echo "copy end ..." | tee -a ${LOG_PATH}
}

check_file()
{
    echo "" | tee -a ${LOG_PATH}
    echo "install begin... " | tee -a ${LOG_PATH}

    if [ ! -f "${CURRENTPATH}${SYSCONFIG_PATH}" ]
    then   
    echo "the ArConfig.dat file is not exist! " | tee -a ${LOG_PATH}
    install_exit     
    fi

    if [ ! -f "${CURRENTPATH}${ZIP_FILE}" ]
    then   
    echo "the 7zr file is not exist! " | tee -a ${LOG_PATH}
    install_exit
    fi

    if [ ! -f "${CURRENTPATH}${MBB_BIN_FILE}" ]
    then   
    echo "the .bin file is not exist! " | tee -a ${LOG_PATH}
    install_exit
    fi
} 

check_first_install()
{
    if [ ! -d "${INSTALL_PATH}" ]
    then
    echo "${INSTALL_PATH} is not exist " | tee -a ${LOG_PATH}
    FIRST_INSTALL="YES"
    return
    fi

    if [ ! -f "${INSTALL_PATH}${MBB_FILE}" -o ! -f "${INSTALL_PATH}${SYSCONFIG_PATH}" ]
    then
    echo "${INSTALL_PATH}${MBB_FILE} '&' ${INSTALL_PATH}${SYSCONFIG_PATH} is not exist " | tee -a ${LOG_PATH}
    FIRST_INSTALL="YES"
    return
    fi
}

yes_or_no()
{
    DEFVAL="Y"
    DEFPMPT="([Y]/N):\c "
    echo -e "${DEFPMPT}"
    read ANS
    while [ "${ANS}" != "Y" -a "${ANS}" != "y" -a "${ANS}" != "N" -a "${ANS}" != "n" -a ! -z "${ANS}" -a "${ANS}" != "\n" ]
    do
        echo -e "${DEFPMPT}"
        read ANS
    done
    : ${ANS:="${DEFVAL}"}
    if [ "${ANS}" != "Y" -a "${ANS}" != "y" ]
    then
        unset ANS
        return 1
    fi
    unset ASN
    return 0
}

verify_version()
{
    echo "the Mobile Broad HL Service has been installed before. " | tee -a ${LOG_PATH}
    echo "now begin to verify version ... " | tee -a ${LOG_PATH}
    INSTALLING_VERSION=$(grep "${SYSCONFIG_VALUE}" "${CURRENTPATH}${SYSCONFIG_PATH}" | head -n1 | cut -d = -f2 | tr -d "\015")
    INSTALLED_VERSION=$(grep "${SYSCONFIG_VALUE}" "${INSTALL_PATH}${SYSCONFIG_PATH}" | head -n1 | cut -d = -f2 | tr -d "\015")
    echo "the INSTALLED version is : ${INSTALLED_VERSION}" | tee -a ${LOG_PATH}   
    echo "the INSTALLING version is : ${INSTALLING_VERSION}" | tee -a ${LOG_PATH}

    VRVERSIONINSTALLED=`echo $INSTALLED_VERSION | cut -d '.' -f1`
    BUILDVERSIONINSTALLED=`echo $INSTALLED_VERSION | cut -d '.' -f2`
    DEVELOPVERSIONINSTALLED=`echo $INSTALLED_VERSION | cut -d '.' -f3`
    SPVERSIONINSTALLED=`echo $INSTALLED_VERSION | cut -d '.' -f4`
    CUSTOMIZEVERSIONINSTALLED=`echo $INSTALLED_VERSION | cut -d '.' -f5`
        
    VRVERSIONINSTALLING=`echo $INSTALLING_VERSION | cut -d '.' -f1`
    BUILDVERSIONINSTALLING=`echo $INSTALLING_VERSION | cut -d '.' -f2`
    DEVELOPVERSIONINSTALLING=`echo $INSTALLING_VERSION | cut -d '.' -f3`
    SPVERSIONINSTALLING=`echo $INSTALLING_VERSION | cut -d '.' -f4`
    CUSTOMIZEVERSIONINSTALLING=`echo $INSTALLING_VERSION | cut -d '.' -f5`
    
        while [ 1 ]
        do
            if [ $CUSTOMIZEVERSIONINSTALLED -ne $CUSTOMIZEVERSIONINSTALLING ]
            then
            echo -e "${DIFFERENT_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
           fi

            if [ $VRVERSIONINSTALLED -gt $VRVERSIONINSTALLING ]
            then
            echo -e "${DIFFERENT_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi

            break
            elif [ $VRVERSIONINSTALLED -lt $VRVERSIONINSTALLING ]
            then
            echo -e "${NEW_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
            
            break
            fi
                       
            if [ $BUILDVERSIONINSTALLED -gt $BUILDVERSIONINSTALLING ]
            then
            echo -e "${DIFFERENT_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi

            break
            elif [ $BUILDVERSIONINSTALLED -lt $BUILDVERSIONINSTALLING ]
            then
            echo -e "${NEW_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
            
            break
            fi 
            
            if [ $DEVELOPVERSIONINSTALLED -gt $DEVELOPVERSIONINSTALLING ]
            then
            echo -e "${DIFFERENT_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
            
            break
            elif [ $DEVELOPVERSIONINSTALLED -lt $DEVELOPVERSIONINSTALLING ]
            then
            echo -e "${NEW_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
            
            break
            fi
            
            if [ $SPVERSIONINSTALLED -gt $SPVERSIONINSTALLING ]
            then
            echo -e "${DIFFERENT_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
            
            break
            elif [ $SPVERSIONINSTALLED -lt $SPVERSIONINSTALLING ]
            then
            echo -e "${NEW_VERSION_CONFIRM}\c" | tee -a ${LOG_PATH}

            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi

            break
            fi

            echo -e  "${REINSTALL_CONFIRM}\c" | tee -a ${LOG_PATH}            
            yes_or_no
            
              if [ ! ${?} -ne 0 ]
              then
              INSTALL_OR_NOT="YES"
              echo "yes/install..." | tee -a ${LOG_PATH}
              #backup_ISO
              #install_mbbservice
              break
              else
              echo "no/exit..." | tee -a ${LOG_PATH}
              break
              fi
        done
}

install_mbbservice()
{
    echo "begin to install ..." | tee -a ${LOG_PATH}
    if [ "$(pidof mbbservice)" != "" ]      
    then
    kill -9 $(pidof mbbservice)
    fi

    if [ -d "${INSTALL_PATH}" ]
    then
    rm -f -R "${INSTALL_PATH}"
    mkdir "${INSTALL_PATH}" | tee -a ${LOG_PATH}
    else
    mkdir "${INSTALL_PATH}" | tee -a ${LOG_PATH}
    fi

    if [ -d "${INSTALL_PATH}" ]
    then
    cp ./* "${INSTALL_PATH}" | tee -a ${LOG_PATH}
    chmod a+x -R "${INSTALL_PATH}"
    else
    echo "mkdir in install path failed " | tee -a ${LOG_PATH}
    fi
 
    if [ "$(which udevadm)" != "" ]
    then
	udevVer=`udevadm version`
    else
	udevVer=`udevinfo -V | awk '{print $3}'`    
    fi

    if [ $udevVer -ge 098 ]    
    then
    echo "udevVer=$udevVer" | tee -a ${LOG_PATH} > /dev/null 2>&1
    cp "${INSTALL_PATH}/10-Huawei-FlashCard.rules" "/etc/udev/rules.d" | tee -a ${LOG_PATH}
    udevVerrule="10-Huawei-FlashCard.rules"
    chmod a+x /etc/udev/rules.d/10-Huawei-FlashCard.rules
    else
    echo "udevVer=$udevVer" | tee -a ${LOG_PATH}
    cp "${INSTALL_PATH}/10-Huawei-FlashCard-old.rules" "/etc/udev/rules.d" | tee -a ${LOG_PATH}
    udevVerrule="10-Huawei-FlashCard-old.rules"   
    chmod a+x /etc/udev/rules.d/10-Huawei-FlashCard-old.rules
    fi

    if [ -d "/lib/udev/rules.d" ]
    then
    cp /etc/udev/rules.d/"${udevVerrule}" /lib/udev/rules.d/ | tee -a ${LOG_PATH}
    fi
   #add by hejinyang on 2011-12-9  如果系统内核在这两个版本之间的,修改rules文件
    if [ "$SYS_KERN_VER" \> "2.6.37" -a "$SYS_KERN_VER" \< "3.0.0" ]
    then 
        if [ "$(cat /lib/udev/rules.d/60-cdrom_id.rules |grep usb:v12D1)" = "" ]
        then
        echo "this OS need to repace rules file " | tee -a ${LOG_PATH}
        mv -f /lib/udev/rules.d/60-cdrom_id.rules /lib/udev/rules.d/60-cdrom_id.rules.bak
        cp -f "${INSTALL_PATH}/60-cdrom_id.rules" /lib/udev/rules.d/.
         fi
        echo "this OS already has rules file " | tee -a ${LOG_PATH}
    fi
   
    
    
    if [ "$(cat /etc/issue |grep Flag)" != "" ]
    then
    start_udev
    fi

    #install the configuration file for auto run in boot
    sed "s#CURPATH#${INSTALL_PATH}#g" "$INSTALL_PATH"/skeleton > "$INSTALL_PATH"/skeleton.bak | tee -a ${LOG_PATH}     

    sed "s#parameter##g" "$INSTALL_PATH"/skeleton.bak > "$INSTALL_PATH"/"$SKELETON_NAME" | tee -a ${LOG_PATH}

    rm -f "$INSTALL_PATH"/skeleton.bak | tee -a ${LOG_PATH}

    if [ -d /etc/init.d ]
    then
    cp -f "$INSTALL_PATH"/"$SKELETON_NAME" /etc/init.d/ | tee -a ${LOG_PATH}
    chmod a+x /etc/init.d/"$SKELETON_NAME"
    CONFPATH=/etc/init.d
    elif [ -d /etc/rc.d ]
    then
    cp -f "$INSTALL_PATH"/"$SKELETON_NAME" /etc/rc.d/ | tee -a ${LOG_PATH}
    chmod a+x /etc/rc.d/"$SKELETON_NAME"
    CONFPATH=/etc/rc.d
fi

#echo "rm -f $CONFPATH/$CONFNAME" >> "$INSTALL_PATH"/driver/Uninstall

#use command
for ADDRUNLEVEL in `find /etc/ -name "rc[2345].d" -type d`;
do
    echo "ADDRUNLEVEL=$ADDRUNLEVEL" | tee -a ${LOG_PATH} > /dev/null 2>&1
    if [ -d $ADDRUNLEVEL ] ; 
    then
        if [ -L "$ADDRUNLEVEL/$START_SKELETON" ] ; 
        then              
            rm -f $ADDRUNLEVEL/$START_SKELETON | tee -a ${LOG_PATH}
            rm -f $ADDRUNLEVEL/$STOP_SKELETON | tee -a ${LOG_PATH}
        fi
        ln -sv $CONFPATH/$SKELETON_NAME $ADDRUNLEVEL/$START_SKELETON | tee -a ${LOG_PATH} > /dev/null 2>&1
        chmod a+x $ADDRUNLEVEL/$START_SKELETON
        ln -sv $CONFPATH/$SKELETON_NAME $ADDRUNLEVEL/$STOP_SKELETON | tee -a ${LOG_PATH} > /dev/null 2>&1
        chmod a+x $ADDRUNLEVEL/$STOP_SKELETON
    
        #echo "rm -f $ADDRUNLEVEL/$START_CONFNAME" >> "$INSTALL_PATH"/driver/Uninstall                     
        #echo "rm -f $ADDRUNLEVEL/$STOP_CONFNAME" >> "$INSTALL_PATH"/driver/Uninstall
    fi
done

if [ -f /etc/init.d/.depend.start ]
then
    rm -f /etc/init.d/.depend.start | tee -a ${LOG_PATH}
    rm -f /etc/init.d/.depend.stop | tee -a ${LOG_PATH}
    chkconfig -a runmbbservice | tee -a ${LOG_PATH}
fi

    rm -rf "${TMP_FILE_PATH}" | tee -a ${LOG_PATH}
    echo "install success..." 
    
}

run_process()
{
    if [ "$(pidof mbbservice)" != "" ]       
    then
    echo "the mbbservice is running "  | tee -a ${LOG_PATH}
    else
    echo "${INSTALL_PATH}${MBB_FILE}" | tee -a ${LOG_PATH}
    #may be there is some question without quote
    ${INSTALL_PATH}${MBB_FILE} & 
    #add by hejinyang on 2011-12-9  begin

    if [ "$SYS_KERN_VER" \> "2.6.37" -a "$SYS_KERN_VER" \< "3.0.0" ]
    then
        if [ -f /sbin/start_udev ]   ##使rules及时生效
        then
       	 /sbin/start_udev
        else
          if [ "$(cat /etc/lsb-release |grep Ubuntu)" != "" -a "$(cat /etc/lsb-release |grep 11.10)" != "" ]  # ubuntu 11.10 may catch a bug we do nothing
          then
          echo "this is unbunto 11.10 we don't  udevadm trigger" | tee -a ${LOG_PATH}
      
          else
          echo "udevadm trigger" | tee -a ${LOG_PATH}
	         udevadm trigger
          fi
       fi
        
    fi
   
  
    #add by hejinyang on 2011-12-9  end
    sleep 2
    fi    
}

check_file
check_first_install

if [ "${FIRST_INSTALL}" = "YES" ]
then
copy_local
else
verify_version
if [ "${INSTALL_OR_NOT}" = "YES" ]
then
copy_local
fi
fi

if [ "${INSTALL_OR_NOT}" = "YES" -o "${FIRST_INSTALL}" = "YES" ]
then
#echo "begin to install..."
install_mbbservice
fi
cd /
#TESTPATH=`pwd`
#echo "${TESTPATH}"
echo "run the process... " | tee -a ${LOG_PATH}
run_process
echo "install end... "
install_exit

#read com

