#!/bin/sh
#
# VMTux 2.0
#
# /bin/vmt-install
#
#
. /etc/vmt/vmt-colors
. /etc/vmt/vmt-conf

PATH=/bin:/sbin

umask 022

if [ "$(id -u)" -eq 0 ]; then
  echo "${BRed}Please run as user.${Reset}" 
  exit 1; 
fi

if [ ! -d $PKG ]; then
  echo "${BRed}No packages directory found.${Reset}" 
  exit 1; 
fi

if [ -z "$1" ]; then echo "Usage: $(basename $0) [--version=2.0] package-name(s)|-boot"; exit 1; fi

DEPLIST=dep.lst

cd $PKG

function GetDependencies()
{
  if [ -z "$1" ]; then return ; fi

  APP="${1%.squ}"

  sed "/^${APP}$/d" -i ${DEPLIST} ; echo ${APP} >> ${DEPLIST}
  
  SFS="${APP}.squ"  
  MD5="${SFS}.md5.txt"
  mirror="${MIRROR}/${ACTIVERELEASE}/packages"
  
  if [ ! -f ${MD5} ]; then
    wget -q "${mirror}/${MD5}" 2>/dev/null
  fi
    
  if [ ! -f ${MD5} ]; then
    echo "${BRed}Package ${BYellow}${APP}${BRed} does not exist${Reset}"
    return
  fi
  
  if [ ! -f ${SFS} ]; then
    echo -n "${Cyan}"
    wget "${mirror}/${SFS}"
    echo -n "${Reset}"
  fi

  if [ ! -f ${SFS} ]; then
    echo "${BRed}Error downloading ${BYellow}${APP}${Reset}"
    return
  fi
  
  md5sum -s -c ${MD5}

  if [ "$?" != 0 ]; then
    echo "${BRed} Checksum ${BYellow}${APP}${BRed} incorrect${Reset}"
    return
  fi

  DEP=${SFS}.dep
  
  if [ ! -f ${DEP} ]; then
    wget -q "${mirror}/${DEP}" 2>/dev/null
  fi
  
  if [ -s ${DEP} ]; then
    DEP=$(cat ${DEP})
    echo "${Cyan}Dependencies ${BYellow}${APP}${Cyan}"
    for app in ${DEP}; do GetDependencies ${app} ; done
    echo "${Cyan}Dependencies ready${Reset}"
  fi
}

function Install()
{
  APP="${1%.squ}"
  APP="${APP%.tcz}"
  
  BASE=${LOOP}/${APP}
  LOCAL=${BASE}/usr/local
  
  if [ -d $BASE ]; then 
    echo "${BBlue}Package ${BYellow}${APP}${BGreen} existing${Reset}"
    return
  fi

  echo -n "${BBlue}Install ${BYellow}${APP}${Reset} "
  
  if [ -f ${APP}.squ ]; then
    SFS="${APP}.squ"
  else
    if [ -f ${APP}.tcz ]; then
      SFS="${APP}.tcz"
    else
      echo
      echo "${BRed}Package ${BYellow}${APP}${BRed} not found.${Reset}"
      return
    fi
  fi

  sudo mkdir -p "${BASE}"
  sudo mount ${SFS} "${BASE}" -t squashfs -o loop,ro
  sudo cp -as "${BASE}"/* / 2>/dev/null

  if [[ -d "${LOCAL}/lib" || -d "${BASE}/usr/lib" ]]; then sudo ldconfig ; fi

  if [[ -d "${LOCAL}/lib/modules" || -d "${BASE}/usr/lib/modules" ]]; then sudo depmod ; fi

  afterburner="${LOCAL}/vmt-afterburner/${APP}"
  if [ ! -x "$afterburner" ]; then afterburner="${LOCAL}/tce.installed/${APP}" ; fi
  if [ -x "$afterburner" ]; then
    PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin
    RESULT=$(sudo /bin/sh "$afterburner" 2>&1)
    PATH=/bin:/sbin
    if [ -n "$RESULT" ]; then
      echo
      echo "${BRed}${RESULT}${Reset}"
      echo -n "${BBlue}Install ${BYellow}${APP}${Reset} "
    fi
  fi
 
  echo "${BGreen}done.${Reset}"
}

function GetDepsAndInstall()
{
  APP="${1%.squ}"
  APP="${APP%.tcz}"
  echo -n > ${DEPLIST}
  GetDependencies ${APP}
  for SFS in $(tac ${DEPLIST}); do Install ${SFS} ; done
  rm ${DEPLIST}
}

# -------

if [ "$1" == "--version" ]; then
  export ACTIVERELEASE="$2"
  shift 2
else
  export ACTIVERELEASE="${VMTRELEASE}"
fi

if [ "$1" == "-boot" ]; then
  echo -n >$LOGINSTALL

  echo -n "${BBlue}Loading packages... ${Yellow}"

  (if [ -f $BOOTLIST ]; then
    while read -r APP
    do
      GetDepsAndInstall ${APP} >>$LOGINSTALL 2>&1
    done < $BOOTLIST
  fi) & rotdash $!

  echo "${BGreen}done.${Reset}"

else

  for APP in "$@"; do
    apps=$(wget -qO- ${SEARCHURL}/${APP}/${ACTIVERELEASE})
    if [ -z "$apps" ]; then
      echo "${BRed}Package ${BYellow}${APP}${BRed} not found.${Reset}"
    else
      if [ "$(echo $apps | wc -w)" -gt "1" ]; then echo "${BGreen}available (${ACTIVERELEASE}): ${BYellow}${apps}${Reset}" ; exit 1 ; fi
      APP="${apps}"
      GetDepsAndInstall ${APP}
      sed "/^${APP}$/d" -i ${BOOTLIST} ; echo ${APP} >> ${BOOTLIST}
    fi
  done

fi

exit 0
