# Command Line, proc might not be mounted
[ -f /proc/cmdline ] || /bin/mount /proc
CMDLINE=""; CMDLINE="$(cat /proc/cmdline)"
# ANSI COLORS
CRE="$(echo -e '\r\033[K')"
NORMAL="$(echo -e '\033[0;39m')"
RED="$(echo -e '\033[1;31m')"
GREEN="$(echo -e '\033[1;32m')"
YELLOW="$(echo -e '\033[1;33m')"
BLUE="$(echo -e '\033[1;34m')"
MAGENTA="$(echo -e '\033[1;35m')"
CYAN="$(echo -e '\033[1;36m')"
WHITE="$(echo -e '\033[1;37m')"

stringinfile(){
case "$(cat $2)" in *$1*) return 0;; esac
return 1
}

stringinstring(){
case "$2" in *$1*) return 0;; esac
return 1
}

getbootparam(){
stringinstring " $1=" "$CMDLINE" || return 1
result="${CMDLINE##*$1=}"
result="${result%%[ 	]*}"
echo "$result"
return 0
}

getparam(){
stringinstring " $1=" "$2" || return 1
result="${2##*$1=}"
result="${result%%[ 	]*}"
echo "$result"
return 0
}

checkbootparam(){
stringinstring " $1" "$CMDLINE"
return "$?"
}

getbasefile(){
BASENAME=`basename $1`
FIELDS=`echo $BASENAME|awk 'BEGIN{ FS="."} {print NF}'`
FIELDS=`expr "$FIELDS" - "$2"`
INFO=`echo $BASENAME|cut -f1-$FIELDS -d.`
echo $INFO
return 0
}

rotdash(){
p=$1
while [ -d /proc/$p ]
do
echo -n '/' ; usleep 100000
echo -n '-' ; usleep 100000
echo -n '\' ; usleep 100000
echo -n '|' ; usleep 100000
done
return 0
}

mounted(){
grep $1 /etc/mtab >/dev/null 2>1&
if [ $? == 0 ]; then return 0; fi
return 1
}

find_mountpoint() {
 MOUNTPOINT=""
 MOUNTED="no"
 D2="$1"
 if [ "$D2" == "nfs" ]; then
    MOUNTPOINT=/mnt/nfs
    MOUNTED="yes"
    return
 fi
 if [ "${D2:0:5}" == "UUID=" ]; then
   D2=`/sbin/blkid -lt $D2 -o device`
   if [ "$?" != 0 ]; then
     MOUNTPOINT=""
     return
   else
     D2="${D2%%:*}"
   fi
 elif [ "${D2:0:6}" == "LABEL=" ]; then
   D2=`/sbin/blkid -lt $D2 -o device`
   if [ "$?" != 0 ]; then
     MOUNTPOINT=""
     return
   else
     D2="${D2%%:*}"
   fi
 else
   D2=/dev/$D2
 fi
 MOUNTPOINT="$(grep -i ^$D2\  /etc/mtab|awk '{print $2}'|head -n 1)"
 if [ -n "$MOUNTPOINT" ]; then
   MOUNTED="yes"
   return
 fi
 
# Special case for virtual disk 
 MOUNTPOINT="$(awk '/\/mnt\/tcvd/{print $2}' /etc/mtab|head -n 1)"
 if [ -n "$MOUNTPOINT" ]; then
   MOUNTED="yes"
   return
 fi

 MOUNTPOINT="$(grep -i ^$D2\  /etc/fstab|awk '{print $2}'|head -n 1)"
}

autoscan(){
FOUND=""
for DEVICE in `grep "/mnt/" /etc/fstab | grep -vf /etc/init.d/tc_noscan.lst | awk 'FS="/" { print $3}'`; do
   find_mountpoint $DEVICE
   if [ -n "$MOUNTPOINT" ]; then
     if [ "$MOUNTED" == "no" ]; then
       mount "$MOUNTPOINT" >/dev/null 2>&1
     fi
     if [ "-$2" "$MOUNTPOINT"/$1 ]; then
       FOUND="yes"
     fi
     if [ "$MOUNTED" == "no" ]; then
       umount "$MOUNTPOINT" >/dev/null 2>&1
     fi
     if [ -n "$FOUND" ]; then 
       echo "$DEVICE"
       return 0
     fi
   fi
done
DEVICE=""
return 1
}

sethostname(){
# Set hostname
# This function contributed by henk.1955 
  HOSTNAME="$(getbootparam host 2>/dev/null)"
  [ -n "$HOSTNAME" ] || HOSTNAME="box"
  echo -n "${BLUE}Setting hostname to ${YELLOW}$HOSTNAME${NORMAL} "
  rm -f /etc/hostname
  echo $HOSTNAME >/etc/hostname
  hostname -F /etc/hostname
  rm -f /etc/hosts
  cat >/etc/hosts <<EOT
127.0.0.1 $HOSTNAME localhost

# The following lines are desirable for IPv6 capable hosts
# (added automatically by netbase upgrade)

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts

EOT

  echo "${BLUE}Done.${NORMAL}"
  return 0
}

getpasswd(){
  OK=0
  until [ "$OK" == 1 ]; do
    PASSWD=""
    CH="."
    echo -n "${BLUE}Enter password for ${YELLOW}$1${NORMAL}: "
    while [ "$CH" != "" ]; do
      read -s -n 1 CH
      PASSWD="$PASSWD$CH"
      echo -n "*"
    done
    PASSWDCK=""
    CH="."
    echo -n "${BLUE} Re-enter${NORMAL}: "
    while [ "$CH" != "" ]; do
      read -s -n 1 CH
      PASSWDCK="$PASSWDCK$CH"
      echo -n "*"
    done
    if [ "$PASSWD" == "$PASSWDCK" ]; then
      OK=1
      echo " ${GREEN}Accepted.${NORMAL}"
    else
      echo " ${RED}Mismatch.${NORMAL}"
    fi
  done
  return 0
}

status() {
  local CHECK=$?
  echo -en "\\033[70G[ "
  if [ $CHECK = 0 ]; then
    echo -en "\\033[1;33mOK"
  else
    echo -en "\\033[1;31mFailed"
  fi
  echo -e "\\033[0;39m ]"
}

usleep_progress() {
  CHAR='.'
  for i in `seq 79-1`
  do
    echo -n "$CHAR"
    usleep 28000
  done
  echo "$CHAR"
}
