#!/bin/sh

#
# NanoGTK on PC X-Windows setup
#

check_dir ()
{
   if [ ! -d $1 ]; then
      echo
      echo "Could not find directory $1"
      echo
      exit 0
   fi
   echo "Found $1"
}

link_file ()
{
   if [ -h $2 ]; then
      rm -f $2
   fi

   if [ -f $1 -o -d $1 ]; then
      echo "ln -s $1 $2"
      ln -s $1 $2
   fi
}

copy_file ()
{
   if [ -f $1 ]; then
      echo "cp -pf $1 $2"
      cp -pf $1 $2
   else
      echo
      echo "Could not find $1"
      echo
      exit 0
   fi
}

exec_cmd ()
{
   echo "$*"
   $*
}


#
# save current working directory
#
THISDIR=`pwd`


#
# locate NanoGTK installation directory
#
if [ "$NANOGTK_HOME" = "" ]; then
   INSTDIR=`dirname \`pwd\``
else
   INSTDIR=$NANOGTK_HOME
fi

check_dir $INSTDIR


#
# set testing directory
#
TESTDIR=$INSTDIR/Testbench

check_dir $TESTDIR


#
# get machine dependent information
#
CONFIG=$TESTDIR/local/etc/platform.conf
if [ -f $TESTDIR/local/etc/platform.conf ]; then
   . $CONFIG
else
   echo
   echo "Could not find $CONFIG"
   echo
   exit 0
fi


echo
echo "<<< Setup NanoGTK Emulation environment for X11 >>>"
exec_cmd cd $TESTDIR


#
# check installed library and application directories
#
check_dir $INSTDIR/EmGdkPixbuf
check_dir $INSTDIR/glib-$GLIB_VER
check_dir $INSTDIR/gtk+-$GTK_VER
check_dir $INSTDIR/microwin
check_dir $INSTDIR/zlib
check_dir $INSTDIR/Application


#
# create various directories
#
exec_cmd cd $TESTDIR

for d in config icons programs
do
   if [ ! -d $d ]; then
      exec_cmd mkdir $d
   else
      echo "directory $d already exists"
   fi
done


#
# create symbolic link to configuration directory
#
exec_cmd cd $HOME

if [ -h config -o ! -a config ]; then
   link_file $TESTDIR/config config
else
   echo "Warning: $HOME/config already exists"
fi

exec_cmd cd $TESTDIR

MCWDIR=$INSTDIR/microwin
APPDIR=../Application
BINDIR=bin-$MARCH


#
# copy nano-X executables
#
copy_file $MCWDIR/$BINDIR/nanowm .
copy_file $MCWDIR/$BINDIR/nano-X .
copy_file $APPDIR/Desktop/$BINDIR/deskman .
copy_file $APPDIR/Desktop/$BINDIR/launcher .
copy_file $APPDIR/EngKeyboard/$BINDIR/nxkbd .
copy_file $APPDIR/CJKKeyboard/$BINDIR/nxcjkkbd .
copy_file $APPDIR/HandWritingPad/$BINDIR/nxhwr .


#
# create symbolic link to config, icon and executable files
#
pdirs=`ls $APPDIR`
appnm=""


#
# handle desktop files
#
if [ -f $APPDIR/Desktop/emlinux/icon/desktop.xpm ]; then
   copy_file $APPDIR/Desktop/emlinux/icon/desktop.xpm icons/
fi

# desktop locale file
files=`ls $APPDIR/Desktop/emlinux/locale/*.mo`

if [ -n "$files" ]; then
   for fn in $files
   do
      if [ -f $fn ]; then
         bn=`basename $fn`
         id=`echo $bn | cut -d '.' -f 2,3`
         odir=local/share/locale/$id/LC_MESSAGES
         exec_cmd install -d $odir
         bn=`echo $bn | cut -d '.' -f 1`
         copy_file $fn $odir/$bn.mo
      fi
   done
fi


#
# Handle other applications
#
for appd in $pdirs
do
   d=$APPDIR/$appd
   e=$d/emlinux

   # do not handle desktop here
   if [ -d $e -a $appd != Desktop ]; then
      if [ -d $e/icon -a -d $e/config ]; then
         #link config file
         files=`ls $e/config/*.cfg`

         if [ -n "$files" ]; then
            for fn in $files
            do
               if [ -f $fn ]; then
                  bn=`basename $fn`
                  copy_file $fn config/$bn
               fi
            done
         else
            echo "warning: no config for $d"
            continue
         fi

         # link icon files
         files=`ls $e/icon/*.gif $e/icon/*.xpm`

         if [ -n "$files" ]; then
            for fn in $files
            do
               if [ -f $fn ]; then
                  bn=`basename $fn`
                  copy_file $fn icons/$bn
               fi
            done
         else
            echo "warning: no icon for $d"
            continue
         fi

         # link locale files
         files=`ls $e/locale/*.mo`

         if [ -n "$files" ]; then
            for fn in $files
            do
               if [ -f $fn ]; then
                  bn=`basename $fn`
                  id=`echo $bn | cut -d '.' -f 2,3`
                  odir=local/share/locale/$id/LC_MESSAGES
                  exec_cmd install -d $odir
                  bn=`echo $bn | cut -d '.' -f 1`
                  copy_file $fn $odir/$bn.mo
               fi
            done
         fi

         # link executables
         files=`ls $d/$BINDIR`

         if [ -n "$files" ]; then
            for fn in $files
            do
               if [ -x $d/$BINDIR/$fn ]; then
                  exec_cmd cd programs
                  link_file ../$d/$BINDIR/$fn $fn
                  exec_cmd cd ..

                  appnm="$appnm $fn"
               fi
            done
         fi

         # application specific installation
         if [ -f $e/install.sh ]; then
            $e/install.sh $INSTDIR

            if [ $? = 0 ]; then
               echo "Application specific installation ok"
            else
               echo "Application specific installation error !!!"
            fi
         fi
      fi
   fi
done


#
# create config file for deskman
#
CONFIG=config/deskman.cfg

if [ ! -f $CONFIG ]; then
cat > $CONFIG << EOF
[icon_layout]
columns=3
rows=4

[display]
start_icon=start.xpm
folder_icon=folder.xpm
manage_folders_icon=manage_folders.xpm
manage_helpers_icon=manage_folders.xpm
desktop_icon=desktop.xpm
background_image_tiled=0
background_color=gray
foreground_color=black
battery_full_color=#008800
battery_empty_color=#CCCCCC

[folder1]
name=Programs
EOF

#
# add program catetory
#
i=0
for app in $appnm
do
i=`expr $i + 1`
cat >> $CONFIG << EOF
program$i=$app
EOF
done

#
# add other categories
#
cat >> $CONFIG << EOF

[folder2]
name=Settings

[folder3]
name=Games
program1=gtktetris
EOF
   echo
   echo "Desktop config file config/deskman.cfg created."
   echo "************************************************"
   cat $CONFIG
   echo "************************************************"
   echo "Please check it and modify it if necessary."
else
   echo
   echo "***************************************************"
   echo "Desktop config file already exists. Please edit the"
   echo "file and add the following names to the program"
   echo "category if they are not already there:"
   echo
   i=0;
   for f in $appnm
   do
      i=`expr $i + 1`
      echo "   $i. $f"
   done
   echo
   echo "Alternatively, you can remove the file and then run"
   echo "this script again."
   echo "***************************************************"
fi

echo

cd $THISDIR

