#!/bin/sh
#(c) Robert Shingledecker 2004-2009
# tcz concept and code from Jason Williams
#
# Example usage: 
# tce-load -i /mnt/hda1/tce/optional/nano.tce   Loads local extension
# tce-load -w -i nano.tce                       Download into tce directory and installs
# tce-load -w nano.tce                          Download only into tce/optional directory
. /etc/init.d/tc-functions
#
[ "$USER" == "root" ] && exit 1
PROG_NAME=$(basename $0)
MYMENU="/usr/local/tce.menu"
unset WGET
unset INSTALL
unset RAMINSTALL
unset FORCE
unset BOOTING
SAVED_DIR=`pwd`

abort(){
   echo  "Usage: ${PROG_NAME} [ -w -i { -r -f } ] extname"
   exit 2
}

abort_to_saved_dir(){
   cd "$SAVED_DIR"
   exit 1
}

while getopts wrifb OPTION
do
  case ${OPTION} in
   w) WGET=TRUE ;;
   i) INSTALL=TRUE ;;
   r) RAMINSTALL=TRUE ;;
   f) FORCE=TRUE ;;
   b) BOOTING=TRUE ;;
   *) abort ;;
  esac
done
shift `expr $OPTIND - 1`
[ -z "$1" ] || ( [ -z "$WGET" ] && [ -z "$INSTALL" ] ) && abort

tcz2ram() {
   [ -d /mnt/test ] || sudo mkdir -p /mnt/test
   sudo mount $1 /mnt/test -o loop
   if [ "$?" == 0 ]; then
      if [ "$(ls -A /mnt/test)" ]; then
         yes n | sudo cp -ai /mnt/test/. / 2>/dev/null
      fi 
      busybox umount -d /mnt/test
   fi
   if [ ! -e "$TCEDIR"/tcz2ram.flg ]; then
     if [ ! -e "$TCEDIR"/tcz2ram.lst ]; then
       echo "${1##/*/}" > "$TCEDIR"/tcz2ram.lst
     else 
       if ! grep -w "${1##/*/}" "$TCEDIR"/tcz2ram.lst; then
         echo "${1##/*/}" >> "$TCEDIR"/tcz2ram.lst
       fi
     fi
   fi
}

update_system() {
  EXT=${1##*.}
  case "$EXT" in 
    *m*)
      if [ ! "$BOOTING" ]; then
        sudo /sbin/depmod -a 2>/dev/null
        sudo /sbin/udevtrigger
      fi
      ;;
   *l*)
      if [ ! "$BOOTING" ]; then
        sudo /sbin/ldconfig 2>/dev/null
      fi
      ;;
  esac 
  if [ -x "$TCEINSTALLED"/$2 ]; then
    if [ "$BOOTING" ] ; then
      echo "$TCEINSTALLED"/$2 >> /tmp/setup.lst
    else
      sudo "$TCEINSTALLED"/$2
    fi
  else
    touch "$TCEINSTALLED"/$2
  fi
}

install(){
  THISAPP="$1"
  APPNAME=$(getbasefile "$THISAPP" 1)
  if [ "$INSTALL" ]; then
    case "$THISAPP" in
       *.tce*)
          sudo tar -C / -zxf $THISAPP;sync
          if [ "$PWD" == "/tmp/tce" ]  && [ `basename "$THISAPP"` == "$THISAPP" ]; then
             rm "$THISAPP"
             rm "$THISAPP".md5.txt
             rm "$THISAPP".dep 2>/dev/null
          fi   
          update_system "$THISAPP" "$APPNAME"
          if [ "$BOOTING" ]; then
             [ "$SHOWAPPS" ] && echo -n "${YELLOW}$APPNAME ${NORMAL}"
          else
             [ -s /etc/sysconfig/desktop ] && desktop.sh "$APPNAME"
          fi  
          ;;
        *.tcz*)
           if [ "$RAMINSTALL" ] || [ -e "$TCEDIR"/tcz2ram.flg ]; then
              tcz2ram "$THISAPP"
	      update_system "$THISAPP" "$APPNAME"
              if [ "$BOOTING" ]; then
                 [ "$SHOWAPPS" ] && echo -n "${YELLOW}$APPNAME ${NORMAL}"
              else
                 [ -s /etc/sysconfig/desktop ] && desktop.sh "$APPNAME"
              fi
           else
              [ -d /tmp/tcloop/"$APPNAME" ] || sudo mkdir -p /tmp/tcloop/"$APPNAME"
              awk -v appname="/tmp/tcloop/$APPNAME" ' { if ( $2 == appname )  exit 1 }' /etc/mtab
              [ "$?" == 1 ] || sudo mount "$THISAPP" /tmp/tcloop/"$APPNAME" -o loop 2>&1 | tee -a /tmp/aberr
              [ -s /tmp/aberr ] && abort_to_saved_dir
              sudo APPNAME=$APPNAME tcz-symlinker --dir > /dev/null 2>&1
              if [ "$FORCE" ]; then
                 sudo APPNAME=$APPNAME tcz-symlinker --force > /dev/null 2>&1
              else
		 sudo APPNAME=$APPNAME tcz-symlinker > /dev/null 2>&1
              fi
	
	      update_system "$THISAPP" "$APPNAME"
	   
 	      if [ -d /tmp/tcloop/"$APPNAME"/usr/local/tce.menu ]; then
 		 if [ `ls -l /tmp/tcloop/"$APPNAME"/usr/local/tce.menu | wc -l` -gt "1" ] ; then    
        		for item in `ls /tmp/tcloop/"$APPNAME"/usr/local/tce.menu`; do
        		[ "$BOOTING" ] || ( [ -s /etc/sysconfig/desktop ] && desktop.sh $item )
        		done
        	 else
			[ "$BOOTING" ] || ( [ -s /etc/sysconfig/desktop ] && desktop.sh "$APPNAME" )
		 fi
	      fi
	      if [ "$BOOTING" ]; then
                 [ "$SHOWAPPS" ] && echo -n "${YELLOW}$APPNAME ${NORMAL}"
              fi
	   fi
       ;;
    esac
  fi
}

# Main
[ -s /opt/.tce_dir ] || exit 1
[ -n "$1" ] || exit 1
[ -f /etc/sysconfig/showapps ] && SHOWAPPS=TRUE
TCEDIR="$(cat /opt/.tce_dir)"
TARGETAPP="$1"
TCEINSTALLED=/usr/local/tce.installed
> /tmp/aberr
APPNAME=$(getbasefile "$TARGETAPP" 1)

#If load local or install then also check if already installed.
#Download only OK.
if [ "$INSTALL" ] && [ ! "$BOOTING" ]; then
  if [ -f /usr/local/tce.installed/"$APPNAME" ]; then
    echo "$APPNAME is already installed!" | tee /tmp/aberr
    exit 1
  fi
fi

if [ "$WGET" ]; then
   MIRROR=`awk '/Mirror/ {print $2}' /opt/.tcrc`
   PROTOCOL=`awk '/Protocol/ {print $2}' /opt/.tcrc`
   REPOSITORY="tcz"
   cd "$TCEDIR"
#  Check for download only    
   if [ -z "$INSTALL" ]; then                                   
      DOWNLOAD_ONLY=1
      [ -d "$TCEDIR"/"optional" ] || mkdir "$TCEDIR"/"optional"                
      cd "optional"                                                             
   fi 
   EXTENSION=`basename "$TARGETAPP"`
   
   DEPLIST="$EXTENSION".dep
   busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$DEPLIST" 2>/dev/null
   if [ "$?" == 0 ]; then 
      for F in `cat "$DEPLIST"`; do
         if ( [ "$DOWNLOAD_ONLY" ] && [ ! -f "$TCEDIR"/optional/"$F" ] ) || ( [ ! "$DOWNLOAD_ONLY" ] && [ ! -f /usr/local/tce.installed/"${F%.*}" ] ); then
            busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$F".md5.txt 2>&1 | tee -a /tmp/aberr
            if [ "$DISPLAY" ] && pgrep -n appbrowser >/dev/null; then
              flwget "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$F" "." 2>&1 | tee -a /tmp/aberr
            else
              busybox wget -c "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$F"
            fi
            (busybox md5sum -c "$F".md5.txt 3>&1 1>&2 2>&3) | tee -a /tmp/aberr
            if [ -s /tmp/aberr ]; then
               echo "on $F" | tee -a /tmp/aberr
               rm -f "$TCEDIR"/"$F"*
               rm -f "$TCEDIR"/"$EXTENSION".dep
               rm -f "$TCEDIR"/"$EXTENSION".md5.txt
               abort_to_saved_dir
            else
               install "$F" 
            fi
         fi
      done
#     rm "$DEPLIST"
   fi
    
   busybox wget -cq "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$EXTENSION".md5.txt 2>&1 | tee -a /tmp/aberr
   if [ "$DISPLAY" ] && pgrep -n appbrowser >/dev/null; then
     flwget "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$EXTENSION" "." 2>&1 | tee -a /tmp/aberr
   else
     busybox wget -c "$PROTOCOL"://"$MIRROR"/"$REPOSITORY"/"$EXTENSION"
   fi
   (busybox md5sum -c "$EXTENSION".md5.txt 3>&1 1>&2 2>&3) | tee -a /tmp/aberr
   if [ -s /tmp/aberr ]; then
      echo "on $EXTENSION" | tee -a /tmp/aberr
      rm -f "$TCEDIR"/"$EXTENSION"*
      abort_to_saved_dir
   else
      install "$EXTENSION"
   fi
fi   
#

# if local install then also check for .dep at the same location.
if [ -z "$WGET" ]; then
   if [ ! -f "$TARGETAPP" ]; then 
      echo "$TARGETAPP not found!"
      abort_to_saved_dir
   fi
   FROMWHERE=`dirname "$TARGETAPP"`
   if [ -f "$TARGETAPP".dep ]; then
      for F in `cat "$TARGETAPP".dep`; do
         DEPFILE="$FROMWHERE"/"$F"
         if [ ! -f /usr/local/tce.installed/"${F%.*}" ]; then
            if [ -f "$DEPFILE" ]; then
               install "$DEPFILE"
            else
               echo "$DEPFILE not found!" | tee -a /tmp/aberr
               abort_to_saved_dir
            fi
         fi
      done
   fi
   install "$TARGETAPP" 
fi

cd "$SAVED_DIR"
[ "$BOOTING" ] || ( [ -f "$MYMENU/$APPNAME" ] && "$DESKTOP"_restart 2>/dev/null )
exit 0
