# colorful messages
[ -z "$SETCOLOR_SUCCESS" ] && SETCOLOR_SUCCESS="echo -en \\033[1;32m"
[ -z "$SETCOLOR_FAILURE" ] && SETCOLOR_FAILURE="echo -en \\033[1;31m"
[ -z "$SETCOLOR_WARNING" ] && SETCOLOR_WARNING="echo -en \\033[1;33m"
[ -z "$SETCOLOR_NORMAL"  ] && SETCOLOR_NORMAL="echo -en \\033[0;39m"

# Turn on color flag
BOOTUP="color"

#
parse_cmdline_option() {
  # The reason we have this function is some boot parameter is like xxx="-k -x", therefore we can not just use 
  # case $param in
  # xxx=*) ...
  # method, since it won't catch the right xxx. It will just catch xxx="-k.
  # //NOTE// This function won't work for those boot parameter can not be a varialble, e.g. live-boot-media (with -, it's not a legal variable), since if we assign it by ". live-boot-media=/live-hd", it will failed.
  local param_ parse_tmp cmdl_file
  #
  while [ $# -gt 0 ]; do
    case "$1" in
      -c|--cmdline-file)
         shift
         if [ -z "$(echo $1 |grep ^-.)" ]; then
           # skip the -xx option, in case 
           cmdl_file="$1"
           shift
         fi
         [ -z "$cmdl_file" ] && echo "-c is used, but no cmdl_file assigned." && exit 1
         ;;
      -*) echo "${0}: ${1}: invalid option" >&2
          exit 2 ;;
      *)  break ;;
    esac
  done
  param_="$1"
  parse_tmp="$(mktemp /tmp/cmdtmp.XXXXXX)"

  [ -z "$cmdl_file" ] && cmdl_file="/proc/cmdline"
  if [ ! -e "$cmdl_file" ]; then
    echo "cmdl_file ($cmdl_file) does _NOT_ exist!"
    exit 1
  fi

  for ik in $param_; do
    # the parameter maybe like: xxx=xyz, xxx="xyz", xxx="-k -x"
    if LC_ALL=C grep -Eq "$ik=\"" $cmdl_file; then
      # This is for case xxx="-k -x" or xxx="xyz", it allows spaces between " "
      # Possible complicated commands:
      # ocs_prerun="sshfs -o nonempty -p 22 root@myhost.mymachine:/home/partimag/ /home/partimag"
      # ocs_prerun="mount -t cifs -o username=user,password=pass //127.0.0.1/test /home/partimag"
      # Therefore we have to add "/" ":" "=" "," "@"
      LC_ALL=C grep -oE -- "$ik=\"([[:space:]]|[[:alnum:]]|_|-|\.|\/|:|=|,|@)*\"([[:space:]]|$)+" $cmdl_file > $parse_tmp
    else
      # This is for case xxx=xyz, no space in its assignment
      LC_ALL=C grep -oE -- "$ik=([[:alnum:]]|_|-|\.|\/|:)*([[:space:]]|$)+" $cmdl_file > $parse_tmp
    fi
    # now we can get the variables
    . $parse_tmp
  done
  [ -f "$parse_tmp" ] && rm -f $parse_tmp
} # parse_cmdline_option
#
get_gui_dialog() {
  if type gdialog &>/dev/null; then
    DIALOG=gdialog
  elif type Xdialog &>/dev/null; then
    DIALOG=Xdialog
  else
    echo "No gdialog or Xdialog was found!"
    echo "Program terminated!!!"
    exit 1
  fi
} # end of get_gui_dialog
#
get_live_autologin_account(){
  # This function is only for used in Clonezilla live.
  # Find the account with NOPASSWD in sudoers:
  # e.g.  user  ALL=(ALL) NOPASSWD: ALL
  live_autologin_account="$(LC_ALL=C grep -iE "^[^#].*ALL=\(ALL\)[[:space:]]*NOPASSWD:[[:space:]]*ALL" /etc/sudoers | awk -F" " '{print $1}')"
} # end of get_live_autologin_account
#
get_live_auto_login_id_home() {
  # This function is only for used in Clonezilla live.
  live_auto_login_id_home="$(LC_ALL=C bash -c "echo ~$live_autologin_account")"
} # end of get_live_auto_login_id_home
# Check if root or not
check_if_root() {
   if [ ! "$UID" = "0" ]; then
     echo
     echo "[$LOGNAME] You need to run this script \"`basename $0`\" as root."
     echo
     exit 1
   fi
} # end of check_if_root
