#!/bin/sh
# (c) Robert Shingledecker 2009

. /etc/init.d/tc-functions

MIRROR=`awk '/Mirror/ {print $2}' /opt/.tcrc`
PROTOCOL=`awk '/Protocol/ {print $2}' /opt/.tcrc`
CORELOC="release/core_elements"

fetch(){
  [ -f "$1" ] && rm -f "$1"
  if [ "$TYPE" == "core" ]; then
    busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$CORELOC"/"$1"
    if [ "$?" != 0 ]; then
      busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$CORELOC"/Xserver/"$1"
    fi
  else
    case $1 in
      *.tce* ) sed -i 's/tcz/tce/g' /opt/.tcrc ;;
      *.tcz* ) sed -i 's/tce/tcz/g' /opt/.tcrc ;;
    esac
    REPOSITORY=`awk '/Repository/ {print $2}' /opt/.tcrc`
    busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$1"
  fi
}   

upgrade(){
    TARGET="$(getbasefile "$F" 2)"
    fetch "$TARGET".md5.txt 2>/dev/null
    if [ "$?" == 0 ]; then
      MYMD5=`cat "$F"`
      REPO=`cat "$TARGET".md5.txt`
      if [ "$MYMD5" != "$REPO" ]; then
        if [ "$QUERY" ]; then
           echo -n " ${YELLOW}$TARGET${NORMAL}"
        else
           echo -n  "${YELLOW}Updating $TARGET${NORMAL} "
           fetch "$TARGET" 2>/dev/null
           busybox md5sum -cs "$TARGET".md5.txt
           if [ "$?" == 0 ]; then
              fetch "$TARGET".dep 2>/dev/null
              [ "$?" == 0 ] && mv /tmp/"$TARGET".dep "$TCEDIR"/.
              mv /tmp/"$TARGET".md5.txt "$TCEDIR"/. 2>/dev/null
              mv /tmp/"$TARGET" "$TCEDIR"/. 2>/dev/null
              echo "${GREEN}OK${NORMAL}"
           else
              echo "${RED}Failed${NORMAL}"
              echo "Error occurred. $TARGET Not updated!" >> "$ERRLIST"
              rm /tmp/"$TARGET".md5.txt
              rm /tmp/"$TARGET"
           fi
        fi
      else
        rm /tmp/"$TARGET".md5.txt
      fi
    else
      echo >> "$ERRLIST"
      echo "Error on fetch of $F" >> "$ERRLIST"
      echo "Could be a network issue or ..." >> "$ERRLIST"
      echo "that this extension has a new name or is no longer supported!" >> "$ERRLIST"
    fi
}
clear
if [ "$USER" != "root" ]; then 
  echo "Need root."
  echo -n "Press Enter key. ";read junk
  exit 1
fi  
echo
cd /tmp
ERRLIST="upgrade_error.lst"
> "$ERRLIST"
for i in `cat /proc/cmdline`; do
  case $i in
    *=*)
    	  case $i in
    	   tce*) TCE=${i#*=} ;;
    	  esac
      ;;
      *)
	  case $i in
	   base) BASE=1 ;;
	   norestore) NORESTORE=1 ;;
	   text) TEXT=1 ;;
	  esac
      ;;
  esac
done
[ "$BASE" ] || MISSING="base"
[ "$NORESTORE" ] || MISSING="$MISSING norestore"
[ "$TEXT" ] || MISSING="$MISSING text"
if [ -n "$MISSING" ]; then
  echo "${GREEN}To ensure a safe batch upgrade of all extensions,"
  echo "the following boot codes are recommended:"
  echo "${YELLOW}base norestore text${NORMAL}"
  echo 
  echo "${GREEN}Best to run when no other extensions are being used.${NORMAL}"
  echo
  echo "${YELLOW}The following boot codes are missing:"
  echo "${RED}$MISSING${NORMAL}"
  echo
  echo "${BLUE}It is recommended to Reboot with the required boot options,"
  echo "and at system prompt enter:${YELLOW} sudo tce-update"
  echo
  echo "${GREEN}At minimum it better to exit from X and at system prompt enter:"
  echo "${YELLOW}sudo tce-update"
  echo
  echo "${YELLOW}To ignore recommendatons and force an update, type ${RED}force${YELLOW}, then press Enter"
  echo
  echo "${BLUE}To query which extensions need to be updated, type ${YELLOW}query${BLUE}, then press Enter"
  echo
  echo -n "${GREEN}Otherwise press Enter key to exit: ${NORMAL}";read answer
  case $answer in
    force) ;;
    query) QUERY=1 ;;
    *) exit 1
  esac
fi

# Next search for tce 
[ -z "$TCE" ] && TCE="$(autoscan 'tce' 'd')"/tce
if [ "$TCE" != "/tce" ]; then
   TCE_DEVICE="${TCE%%/*}"
   TCE_DIR="${TCE#*/}"
   if [ "$TCE_DEVICE" = "$TCE_DIR" ]; then TCE_DIR="tce"; fi
   find_mountpoint "$TCE_DEVICE"
   if [ -n "$MOUNTPOINT" ]; then
      if [ "$MOUNTED" == "yes" ]; then
         TCEDIR="$MOUNTPOINT"/"$TCE_DIR"
      else
         mount "$MOUNTPOINT"
         if [ $? == 0 ]; then
            UMOUNT="yes"
            TCEDIR="$MOUNTPOINT"/"$TCE_DIR"
         fi
      fi
   fi
else
  echo "$TCE not found!"
  exit 1
fi
if [ -n "$TCEDIR" ]; then
# TYPE="core"
# echo -n "${BLUE}Checking Tiny Core Elements in ${NORMAL}"
# echo "$TCEDIR"
# for F in `ls "$TCEDIR"/*.core.*.md5.txt 2>/dev/null`; do
#   upgrade
# done
  TYPE="extensions"
  echo -n "${BLUE}Checking Tiny Core Applications in ${YELLOW}"
  echo "$TCEDIR${NORMAL}"
  [ "$QUERY" ] && echo "${BLUE}The following extensions need to be updated!${NORMAL}"
  for F in `ls "$TCEDIR"/*.md5.txt | grep -v "\.core\."`; do
    upgrade
  done
fi
echo

if [ ! "$QUERY" ]; then
echo "${BLUE}Checking dependencies:${NORMAL}"
for F in `ls "$TCEDIR"/*.dep`; do
  for TARGET in `cat "$F"`; do
    if [ ! -f "$TCEDIR"/"$TARGET" ]; then
      fetch "$TARGET".md5.txt 2>/dev/null
      if [ "$?" == 0 ]; then
        echo -n  "${YELLOW}Fetching required dependency: $TARGET${NORMAL} "
        fetch "$TARGET" 2>/dev/null
        busybox md5sum -cs "$TARGET".md5.txt
        if [ "$?" == 0 ]; then
           mv /tmp/"$TARGET".md5.txt "$TCEDIR"/. 2>/dev/null
           mv /tmp/"$TARGET" "$TCEDIR"/. 2>/dev/null
           echo "${GREEN}OK${NORMAL}"
        else
           echo "${RED}Failed${NORMAL}"
           echo "Error occurred while fetching dependency: $TARGET" >> "$ERRLIST"
           rm /tmp/"$TARGET".md5.txt
           rm /tmp/"$TARGET"
        fi
      fi
    fi 
  done
done
echo
fi

[ -s "$ERRLIST" ] && echo "${RED}Errors occured during upgrade. See /tmp/upgrade_errors.lst${NORMAL}"
[ "$QUERY" ] || echo "Reboot to use new updated extensions."
echo -n "Press Enter key. ";read junk
