#!/bin/sh
#
# quantumstep installer
# based on xipk-link - symlinker for xipk
# version 0.3
# This script is used to link binaries and directories that were 
# installed to alternate locations other than internal memory such as
# SD card, CF card or the internal MicroDrive.
#
# Copyright (C) 2005 hdluc@yahoo.com
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more detaiLS.
#
######################################################################
VSTRING=`grep "\# version" $0|grep -v "VSTRING"|sed -e "s/#//g"`
IPKINFO=/usr/lib/ipkg/info
IPKSTATUS=/usr/lib/ipkg/status

echo ""
echo "`basename $0` -$VSTRING"
echo ""

if [ "$1" != "" ]; then
  IBASE="$1"
  IPKG="$2" 
  if [ ! -d $IBASE ]; then
    echo "sorry, $IBASE does not exist!"
    echo "syntax: $0 [install base location]"
    exit 1
  fi
else
  IBASE=`dirname $0`
  if [ "$IBASE" = "." ]; then IBASE=`pwd`; fi
  IPKG=`basename $0|cut -d - -f1`
  if [ -f $IBASE/version ]; then 
    $IBASE/version
  else
    echo "syntax: $0 [install location]"
    exit 0 
  fi
fi
if [ "$IPKG" != "" ]; then
  IPKFILES=$IPKINFO/$IPKG.list
else
  IPKFILES=/dev/null
fi

if [ "`id -u`" != "0" ]; then 
  echo "error: you have to be root to run this!"
  exit 1
fi 

### functions ###
updatepkginfo ()
{
APPNAME=$IPKG
if [ "`grep \"Package: $APPNAME\" $IPKSTATUS`" = "" ]; then
for i in `$IBASE/version`
do
  V=$i
done
if [ "`echo $V|grep \.`" = "" ]; then
  APPVERSION=0.1
else
  APPVERSION=$V
fi
cat >> $IPKSTATUS << EOF
Package: $APPNAME
Status: install ok installed
Root: /
Version: $APPVERSION

EOF
else
  IPKFILES=/dev/null
fi
}

copydir ()
{
  DIR=$1
  DESC=$2
  if [ -d $IBASE$DIR ]; then
  echo "...copying $DESC files to $DIR"
    if [ ! -d $DIR ]; then
        mkdir -p $DIR
        echo "$DIR" >> $IPKFILES
    fi
    cd $IBASE$DIR
    for i in `ls -A 2>/dev/null`
    do
      if [ -d $IBASE$DIR/$i ] && [ ! -d $DIR/$i ]; then
        echo "...copying $i to $DIR/$i"
        cp -R $i $DIR
        find . -print|sed -e "s#\.#$DIR#g" >> $IPKFILES
      fi
      if [ -f $IBASE$DIR/$i ] && [ ! -f $DIR/$i ]; then
        echo "...copying $i to $DIR/$i"
        cp $i $DIR
        echo "$DIR/$i" >> $IPKFILES
      fi
    done
  fi
}

linkdir ()
{
  DIR=$1
  if [ -d $IBASE$DIR ]; then
    echo "...linking files to $DIR"
    if [ ! -d $DIR ]; then
        mkdir -p $DIR
        echo "$DIR" >> $IPKFILES
    fi
    cd $IBASE$DIR
    for i in `ls -A 2>/dev/null`
    do
      if [ -f $IBASE$DIR ] && [ ! -f $DIR/$i ]; then
        ln -sf $IBASE$DIR/$i $DIR/$i
        echo "$DIR/$i" >> $IPKFILES
      fi
      if [ -d $IBASE$DIR ] && [ ! -d $DIR/$i ]; then
        ln -sf $IBASE$DIR/$i $DIR/$i
        echo "$DIR/$i" >> $IPKFILES
      fi
    done
  fi
}

### main ###
echo "relinking binaries and directories from $IBASE"
echo "...updating package info"
updatepkginfo

# running on a C3000
if [ "`grep Spitz /proc/cpuinfo`" != "" ]; then
  echo "preparing linked folders for C3000"
  mkdir -p /hdd2/home/myPDA/Users
  echo /hdd2/home/myPDA/Users >> $IPKFILES
  ln -sf /hdd2/home/myPDA /home/myPDA
  echo /hdd2/home/myPDA /home/myPDA >> $IPKFILES
else
  echo "preparing linked folders"
  mkdir -p /home/myPDA/Users
  echo /home/myPDA/Users >> $IPKFILES
fi
ln -sf /hdd3 /home/myPDA/Users/zaurus
echo /home/myPDA/Users/zaurus >> $IPKFILES
ln -s / /mnt/root
echo /mnt/root >> $IPKFILES

echo "creating devices"
rm -f /dev/zapmd; mkfifo /dev/zapmd
rm -f /dev/ets; mkfifo /dev/ets

echo "registering services"
fgrep gdomap /etc/services >/dev/null ||
( 
echo "gdomap      	538/tcp			# GNUStep Distributed Object Mapping"
) >>/etc/services

chown root.root $IBASE/home/myPDA/bin/setpgrp 2>/dev/null
chown root.root $IBASE/home/myPDA/bin/gdomap 2>/dev/null
chown root.root $IBASE/home/myPDA/bin/Xfbdev 2>/dev/null
chown root.root $IBASE/home/myPDA/bin/zapmd 2>/dev/null
chown root.root $IBASE/home/myPDA/bin/ztsd 2>/dev/null
chown -R root.root $IBASE/home/myPDA/lib 2>/dev/null
chown -R root.root $IBASE/home/myPDA/etc 2>/dev/null
chmod u+s $IBASE/home/myPDA/bin/setpgrp 2>/dev/null
chmod u+s $IBASE/home/myPDA/bin/gdomap 2>/dev/null
chmod u+s $IBASE/home/myPDA/bin/Xfbdev 2>/dev/null
chmod u+s $IBASE/home/myPDA/bin/zapmd 2>/dev/null
chmod u+s $IBASE/home/myPDA/bin/ztsd 2>/dev/null

if [ -d $IBASE/opt/QtPalmtop/apps ]; then
  cd $IBASE/opt/QtPalmtop/apps
  for i in `ls -A 2>/dev/null`
  do
    copydir /opt/QtPalmtop/apps/$i "desktop config"
  done
fi
copydir /opt/QtPalmtop/pics icons

echo "...linking files..."
linkdir /opt/QtPalmtop/help/html
linkdir /opt/QtPalmtop/bin
linkdir /opt/QtPalmtop/lib
if [ ! -f /opt/QtPalmtop/pics/Xqt.png ] && [ -f /opt/QtPalmtop/pics144/Xqt.png ]; then
  ln -sf /opt/QtPalmtop/pics144/Xqt.png /opt/QtPalmtop/pics/Xqt.png
fi
linkdir /home/myPDA myPDA

( # insert build and version numbers
echo '<?xml version="1.0" encoding="UTF-8"?>'
echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">'
echo '<plist version="1.0">'
echo "<dict>"
echo "	<key>AppBuild</key>"
echo "	<string>86</string>"
echo "	<key>AppName</key>"
echo "	<string>QuantumSTEP</string>"
echo "	<key>AppVersion</key>"
echo "	<string>DR7</string>"
echo "</dict>"
echo "</plist>"
) >/home/myPDA/System/Library/CoreServices/mySystemUIServer.app/Contents/Resources/Version.plist
echo /home/myPDA/System/Library/CoreServices/mySystemUIServer.app/Contents/Resources/Version.plist >> $IPKFILES

echo "...loading libraries"
/sbin/ldconfig

echo "$IPKG configured."
su zaurus -c "qcop QPE/TaskBar \"updateMenu()\" 2>/dev/null"
su zaurus -c "qcop QPE/TaskBar \"message(QString)\" \"$IPKG installed.\" 2>/dev/null"
