#!/bin/sh
FILES="teTeX pdaXqtrom-base pdaXqtrom-apps zgcc2-95-3 java dictionaries openoffice games"
echo "X/Qt super jumbo installer - version 0.3.6.4 -- modified by Jan Poland in order to add teTeX"

if [ "`id -u`" != "0" ]; then 
  echo "error: you have to be root to run this!"
  exit 1
fi 
if [ "`which awk`" = "" ]; then
  echo "error: awk not found! please install it first."
  exit 1
fi
if [ "`which wc`" = "" ]; then
  echo "warning: wc not found!"
fi
if [ "`which mount`" = "" ]; then
  echo "warning: mount not found!"
fi

unzip_image()
{
  if [ -f $1.zip ] && [ ! -f $1 ]; then
    echo "...found $1.zip"
    if [ "`which unzip`" = "" ]; then
      echo "error: unzip not found!"
      exit 1
    else
      echo "...uncompressing"
      unzip $1.zip
    fi
  fi
}

ungzip_image()
{
  if [ -f $1.gz ] && [ ! -f $1 ]; then
    echo "...found $1.gz"
    if [ "`which gzip`" = "" ]; then
      echo "error: gzip not found!"
      exit 1
    else
      echo "...uncompressing"
      gzip -d $1.gz
    fi
  fi
}

untgz_image()
{
  if [ -f $1.tgz ] && [ ! -f $1 ]; then
    echo "...found $1.tgz"
    if [ "`which zcat`" = "" ] || [ "`which tar`" = "" ]; then
      echo "error: zcat or tar not found!"
      exit 1
    else
      echo "...uncompressing"
      zcat $1.tgz|tar xf -
    fi
  fi
}

untargz_image()
{
  if [ -f $1.tar.gz ] && [ ! -f $1 ]; then
    echo "...found $1.tar.gz"
    if [ "`which zcat`" = "" ] || [ "`which tar`" = "" ]; then
      echo "error: zcat or tar not found!"
      exit 1
    else
      echo "...uncompressing"
      zcat $1.tar.gz|tar xf -
    fi
  fi
}


MOUNTBASE=/mnt
if [ -f /etc/rc.d/init.d/mntloop ]; then
  echo "automounter is installed."
  echo "stopping loop devices..."
  if [ "`which automounter`" != "" ]; then
    automounter stop
  else
     echo "old version of automounter detected, please update"
    /etc/rc.d/init.d/mntloop stop
  fi
else
  echo "automounter is not installed."
  echo "it is recommended to install automounter or something similar"
fi

IMAGES=0
for i in $FILES
do                 
  CHECK=`ls ${i}* 2>/dev/null| wc -l`
  if [ $CHECK -ne 0 ]; then
    IMAGES=`echo $IMAGES|awk '{print $1+1}'`
  fi
done

LOOPS=`ls /dev/loop* -l|wc -l|awk '{print $1}'`
echo "The system has $LOOPS loop devices."
echo "There are $IMAGES cramfs/squashfs images."
if [ $LOOPS -lt $IMAGES ]; then
  if [ "`which mknod`" = "" ]; then
    echo "error: mknod not found!"
    exit 1
  else 
    echo ""
    echo "...creating additional loop devices"
    for i in 2 3 4 5 6
    do
      if [ ! -e /dev/loop$i ]; then
        echo "  mknod /dev/loop$i b 7 $i"
        mknod /dev/loop$i b 7 $i 2>/dev/null
      fi
    done    
    echo ""
    echo "You will need to recreate the additional loop devices after each reboot!"
  fi
fi

if [ -d /etc/X11 ]; then
  echo "...purging /etc/X11"
  rm -r /etc/X11
fi
if [ -f /home/root/.xinitrc ]; then
  rm /home/root/.xinitrc
fi
if [ -f /home/zaurus/.xinitrc ]; then
  rm /home/zaurus/.xinitrc
fi

if [ ! -f /home/zaurus/.profile ]; then
  touch /home/zaurus/.profile
  chown zaurus:qpe /home/zaurus/.profile
fi
if [ "`grep LC_ALL /home/zaurus/.profile`" = "" ]; then
  echo "export LC_ALL=C" >> /home/zaurus/.profile
fi
if [ "`grep LANGUAGE /home/zaurus/.profile`" = "" ]; then
  echo "export LANGUAGE=C" >> /home/zaurus/.profile
fi
if [ "`grep LC_ALL /home/root/.profile`" = "" ]; then
  echo "export LC_ALL=C" >> /home/root/.profile
  export LC_ALL=C
fi
if [ "`grep LANGUAGE /home/root/.profile`" = "" ]; then
  echo "export LANGUAGE=C" >> /home/root/.profile
  export LANGUAGE=C
fi
mkdir -p /usr/share/stardict 2>/dev/null
mkdir -p /usr/local/share/stardict 2>/dev/null

for i in $FILES
do
  XQTJUMBO=$i
  XQTCRAM=$XQTJUMBO.cramfs
  XQTSQUASH=$XQTJUMBO.squashfs
  XQTMOUNT=$MOUNTBASE/$XQTJUMBO
  if [ "`echo $XQTJUMBO|grep zgcc`" != "" ]; then
    XQTMOUNT=/mnt/zgcc
  fi

  echo "...checking for $XQTJUMBO"

  unzip_image $XQTCRAM
  unzip_image $XQTSQUASH
  #untgz_image $XQTCRAM
  #untgz_image $XQTSQUASH
  #untargz_image $XQTCRAM
  #untargz_image $XQTSQUASH
  ungzip_image $XQTCRAM
  ungzip_image $XQTSQUASH

  if [ -f $XQTSQUASH ] && [ -f $XQTCRAM ]; then
    echo "error: $XQTSQUASH and $XQTCRAM found! cannot determine which one to install."
  fi

  if [ ! -f $XQTSQUASH ] && [ -f $XQTCRAM ]; then
    echo "...installing $XQTJUMBO to `pwd`"
    if [ ! -d $XQTMOUNT ]; then
      mkdir -p $XQTMOUNT 
    fi
    if [ "`grep $XQTCRAM /etc/fstab`" = "" ]; then
      echo "`pwd`/$XQTCRAM $XQTMOUNT cramfs loop 0 0" >> /etc/fstab
    fi
    CRAM=`basename $XQTCRAM`
    if [ "`grep $CRAM /etc/fstab|grep -v '#'|wc -l|awk '{print $1}'`" != "1" ]; then
      echo "Warning: ambiguous entry for $CRAM found in /etc/fstab"
    fi
    if [ "`mount|grep $XQTMOUNT`" = "" ]; then
      mount `pwd`/$XQTCRAM
    fi
    for i in `ls $XQTMOUNT/*-setup 2>/dev/null`
    do 
      SETUP=$i
      if [ "$SETUP" != "" ] && [ -f $SETUP ]; then
        $SETUP
      fi
    done
  fi

  if [ ! -f $XQTCRAM ] && [ -f $XQTSQUASH ]; then
    echo "...installing $XQTJUMBO to `pwd`"
    if [ ! -d $XQTMOUNT ]; then
      mkdir -p $XQTMOUNT 
    fi
    if [ "`grep $XQTSQUASH /etc/fstab`" = "" ]; then
      echo "`pwd`/$XQTSQUASH $XQTMOUNT squashfs loop 0 0" >> /etc/fstab
    fi
    SQUASHB=`basename $XQTSQUASH`
    if [ "`grep $SQUASHB /etc/fstab|grep -v '#'|wc -l|awk '{print $1}'`" != "1" ]; then
      echo "Warning: ambiguous entry for $SQUASHB found in /etc/fstab"
    fi
    if [ "`mount|grep $XQTMOUNT`" = "" ]; then
      mount `pwd`/$XQTSQUASH
    fi
    for i in `ls $XQTMOUNT/*-setup 2>/dev/null`
    do 
      SETUP=$i
      if [ "$SETUP" != "" ] && [ -f $SETUP ]; then
        $SETUP
      fi
    done
  fi

  if [ ! -f $XQTCRAM ] && [ ! -f $XQTSQUASH ]; then
    if [ -f $XQTJUMBO.tar.gz ] || [ -f $XQTJUMBO.tgz ]; then
      if [ -f /etc/xipk.conf ] && [ -d "`cat /etc/xipk.conf`" ]; then
        XQTINST=`cat /etc/xipk.conf`
        echo "...installing and extracting $XQTJUMBO to $XQTINST"
        touch $XQTINST/ltest
        if [ "$?" != "0" ]; then
          echo "error: read-only filesystem!"
          exit 1 
        fi
        ln -s $XQTINST/ltest $XQTINST/lltest
        if [ "$?" != "0" ]; then
          rm $XQTINST/ltest 2>/dev/null 
          echo "error: not a linux filesystem!"
          exit 1 
        else
          rm $XQTINST/lltest 2>/dev/null 
          rm $XQTINST/ltest 2>/dev/null 
        fi
        if [ -f $XQTJUMBO.tar.gz ]; then
          echo "...extracting $XQTJUMBO.tar.gz"
          zcat `pwd`/$XQTJUMBO.tar.gz|tar xvf - -C $XQTINST
        fi
        if [ -f $XQTJUMBO.tgz ]; then
          echo "...extracting $XQTJUMBO.tgz"
          zcat `pwd`/$XQTJUMBO.tgz|tar xvf - -C $XQTINST
        fi
        for i in `ls $XQTINST/*-setup 2>/dev/null`
        do 
          SETUP=$i
          if [ "$SETUP" != "" ] && [ -f $SETUP ] && [ "$XQTINST" != "/" ]; then
            echo "...running $SETUP"
            $SETUP
          fi
        done
      else
          echo "error: installation directory not specified in /etc/xipk.conf"
          exit 1
      fi
    else
      echo "$XQTJUMBO not found!"
    fi
  fi

done

if [ "`which automounter`" != "" ]; then
  echo "remounting loop devices..."
  automounter start
else
  if [ -f /etc/rc.d/init.d/mntloop ]; then
    /etc/rc.d/init.d/mntloop start
  fi
fi
echo ""
echo "X/Qt installed and configured (if found)."
echo "use xlauncher or the StartX icon to start it."
echo ""

