#!/bin/bash

# Script for special machines (and need root).
 
GEOMETRY="/tmp/.cocon.geometry"
SPMACHINE486_CONF="/usr/share/spmachine-486"
XORG_CONF_D="/etc/X11/xorg.conf.d"


# only read cocon.cnf
if [ -e /tmp/cocon.cnf ];
then
  . /tmp/.cocon.cnf
fi

# Force Framebuffer driver if boot option contains "forcefb*"
if [ `cat /proc/cmdline | grep 'forcefb'` ];
then

  # Temporary Dirty Fix : Disable some driver on forcefb
  rm /usr/lib/xorg/modules/drivers/openchrome_drv.so

  ln -s $SPMACHINE486_CONF/card-fbdev.conf $XORG_CONF_D/card-fbdev.conf
fi

# Force 800x600 16bit color if contains "forcesvga"
if [ `cat /proc/cmdline | grep 'forcesvga'` ];
then
  ln -s $SPMACHINE486_CONF/defaultdepth.conf $XORG_CONF_D/defaultdepth.conf
fi


# Machine name
if [ "`which dmidecode`" ];
then
  # dmidecode is only for x86
  MACHINE_MAKER=`dmidecode -s system-manufacturer`
  MACHINE_NAME=`dmidecode -s system-product-name`
  echo "Manufacturer: $MACHINE_MAKER"
  echo "Product name: $MACHINE_NAME"
fi

# Workaroud for KOHJINSHA SA
if [ "`echo "$MACHINE_MAKER" | grep 'KOHJINSHA'`" ];
then

  # KOHJINSHA SA
  if [ "`echo "$MACHINE_NAME" | grep 'SA Series'`" ];
  then
    if [ -z "`echo $COCON_GEOMETRY`" ];
    then
      dialog --menu "KOHJINSHA SA : Select Geometry" 20 60 25 \
        800x480   "800x480 (SA1F, SA5KX, SA5KL)" \
        1024x600  "1024x600 (SA5SX, SA5ST)" \
      2>$GEOMETRY
      COCON_GEOMETRY=`cat $GEOMETRY` 
    fi
    
    case $COCON_GEOMETRY in
    800x480)
      ln -sf $SPMACHINE486_CONF/geode-800x480.conf $XORG_CONF_D/geode-800x480.conf
      ;;
    1024x600)
      ln -sf $SPMACHINE486_CONF/geode-1024x600.conf $XORG_CONF_D/geode-1024x600.conf
      ;;
    esac
    
  fi

  # KOHJINSHA PM
  if [ "`echo "$MACHINE_NAME" | grep 'PM Series'`" ];
  then
    ln -sf $SPMACHINE486_CONF/kohjinsha-pm.conf $XORG_CONF_D/kohjinsha-pm.conf
    export COCON_GALAX="1"
  fi
fi


# Workaround for SHARP
if [ "`echo "$MACHINE_MAKER" | grep 'SHARP Corporation'`" ];
then

  # WILLCOM D4
  if [ "`echo "$MACHINE_NAME" | grep 'PC-UM1 Series'`" ];
  then
    ln -sf $SPMACHINE486_CONF/kohjinsha-pm.conf $XORG_CONF_D/kohjinsha-pm.conf
  fi
fi


# Workaroud for SONY PCG-C1 (with neomagic graphics)
# Select 2nd generation PCG-C1. 1st generation can't detect from dmidecode, then detect from /proc/cmdline.
if [ "`echo "$MACHINE_MAKER" | grep 'Sony Corporation'`" -a "`echo "$MACHINE_NAME" | grep -E 'PCG-C1XG|PCG-C1XE|PCG-C2GPS|PCG-C1XF'`" ];
then
  COCON_PCG_C1=1
  export COCON_PCG_C1
fi

if [ "`cat /proc/cmdline | grep 'pcg-c1'`" ];
then
  COCON_PCG_C1=1
  export COCON_PCG_C1
fi

if [ "`echo $COCON_PCG_C1`" = "1" ];
then
    ln -sf $SPMACHINE486_CONF/sony-pcg-c1.conf $XORG_CONF_D/sony-pcg-c1.conf
fi 

# Workaround for Libretto L1, L2, ...
if [ "`echo "$MACHINE_MAKER" | grep 'TOSHIBA'`" -a "`echo "$MACHINE_NAME" | grep -E 'Libretto L'`" ];
then
#  ln -sf $SPMACHINE486_CONF/libretto-lx.conf $XORG_CONF_D/libretto-lx.conf
  ln -s $SPMACHINE486_CONF/card-fbdev.conf $XORG_CONF_D/card-fbdev.conf
fi

# Siliconmotion : Workaroud for SM712
if [ "` lspci | grep -E '126f:0712' `" ];
then
  modprobe sm712fb
  ln -s $SPMACHINE486_CONF/card-fbdev.conf $XORG_CONF_D/card-fbdev.conf
fi


# Poulsbo : Software cursor
if [ "` lsmod | grep -E 'psb_gfx|gma500_gfx'`" ];
then
  ln -sf $SPMACHINE486_CONF/poulsbo.conf $XORG_CONF_D/poulsbo.conf
fi




# Re-startup b43 device and load firmware
if [ "` lsmod | grep -E 'b43legacy'`" -a -d /lib/firmware/b43legacy ];
then
  modprobe -r b43legacy
  modprobe b43legacy
fi

if [ "` lsmod | grep -E 'b43'`" -a -d /lib/firmware/b43 ];
then
  modprobe -r b43
  modprobe b43
fi

# Re-startup ipw2x00 device and load firmware
if [ "` lsmod | grep -E 'ipw2100'`" -a -r /lib/firmware/ipw2100-1.3.fw ];
then
  modprobe -r ipw2100
  modprobe ipw2100
fi

if [ "` lsmod | grep -E 'ipw2200'`" -a -r /lib/firmware/ipw2200-bss.fw ];
then
  modprobe -r ipw2200
  modprobe ipw2200
fi

# p54
if [ "` lsmod | grep -E 'p54pci'`" -a -r /lib/firmware/isl3886pci ];
then
  modprobe -r p54pci
  modprobe p54pci

  # TODO : p54 is need ifup function, but adhoc...
  /sbin/ifconfig wlan0 up
fi

if [ "` lsmod | grep -E 'p54usb'`" -a -r /lib/firmware/isl3886usb ];
then
  modprobe -r p54usb
  modprobe p54usb

  # TODO : p54 is need ifup function, but adhoc...
  /sbin/ifconfig wlan0 up
fi

# opengalax Touchscreen flag
if [ "`cat /proc/cmdline | grep 'galax'`" -o "`echo $COCON_GALAX`" = "1" ];
then
  mouse_sysfs="/sys` dmesg | grep "input: PS/2 Generic Mouse as" | sed -e "s|input: PS/2 Generic Mouse as ||g" `/device/drvctl"
  echo -n "serio_raw" > $mouse_sysfs

  modprobe uinput
  sleep 2
 
  # $SPMACHINE486_CONF/opengalax.conf propides from opengalax package 
  opengalax &
  ln -sf $SPMACHINE486_CONF/opengalax.conf $XORG_CONF_D/opengalax.conf

fi


exit 0
