#!/bin/sh

# 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"
XORG_CONF="/etc/X11/xorg.conf"

set_galax() {
  # opengalax Touchscreen flag
  mouse_device=$( dmesg | grep "input: PS/2 Generic Mouse as" | sed -e "s|input: PS/2 Generic Mouse as ||g" )
  mouse_sysfs="/sys/$mouse_device/device/drvctl"
  echo "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
}

set_forcefb() {
  # 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
}

set_forcesvga() {
  # Force 800x600 16bit color if contains "forcesvga"
  ln -s $SPMACHINE486_CONF/defaultdepth.conf $XORG_CONF_D/defaultdepth.conf
}

spmachine_by_bootparam() {
  # SONY PCG-C1 (1st generation)
  if [ "$1" = "pcg-c1" ];
  then
    ln -sf $SPMACHINE486_CONF/sony-pcg-c1.conf $XORG_CONF_D/sony-pcg-c1.conf
  fi

  # Libretto 50/60
  if [ "$1" = "libretto50" ];
  then
    # force probe PCMCIA driver
    modprobe i82365
  fi

  # Fujitsu NCIII13D
  if [ "$1" = "fujitsu_nciii13d" ];
  then
    # overrride xorg.conf
    ln -sf $SPMACHINE486_CONF/fujitsu-nciii13d.conf $XORG_CONF
  fi
}

echo "=== start spmachine for cocon486,686"

# First, read boot parameter
[ -z "$CMDLINE" ] && CMDLINE=$( cat /proc/cmdline )
for arg in $CMDLINE; do
  optarg=$( expr "x$arg" : 'x[^=]*=\(.*\)' )
  case $arg in
    forcefb)
      set_forcefb
      ;;
    forcesvga)
      set_forcesvga
      ;;
#    forcexdrv)
#      COCON_FORCEXDRV=1
#      ;;
    spmachine=*)
      spmachine_by_bootparam "$optarg"
      ;;
    galax)
      set_galax
      ;;
  esac
done

echo "=== start spmachine for cocon486,686"

# Second, Automatic detect machine

# Force 800x600 16bit color if contains "forcesvga"
if [ ! -r "$XORG_CONF_D/defaultdepth.conf" ];
then
  # Workaround chips for broken X.org driver
  #  (Currently ATI, Savage, Siliconmotion, Neomagic.
  #   Almost these chips can display 24-bit color depth.)
  if lsmod | grep -qE 'radeon|savage|atyfb|sm7xx|neofb|i915'
  then
    ln -s $SPMACHINE486_CONF/depth-24.conf $XORG_CONF_D/depth-24.conf
  fi
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
if echo "$MACHINE_MAKER" | grep -q 'KOHJINSHA'
then

  # KOHJINSHA SA
  if echo "$MACHINE_NAME" | grep -q 'SA Series'
  then
    if [ -z "$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 -q 'PM Series'
  then
    ln -sf $SPMACHINE486_CONF/kohjinsha-pm.conf $XORG_CONF_D/kohjinsha-pm.conf
    set_galax
  fi
fi


# Workaround for SHARP
if echo "$MACHINE_MAKER" | grep -q 'SHARP Corporation'
then
  # WILLCOM D4
  if echo "$MACHINE_NAME" | grep -q '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 -q 'Sony Corporation' && echo "$MACHINE_NAME" | grep -qE 'PCG-C1XG|PCG-C1XE|PCG-C2GPS|PCG-C1XF'
then
  COCON_SPMACHINE="pcg-c1"
fi

if [ "$COCON_SPMACHINE" = "pcg-c1" ];
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 -q 'TOSHIBA' && echo "$MACHINE_NAME" | grep -q 'Libretto L'
then
  ln -sf $SPMACHINE486_CONF/libretto-lx.conf $XORG_CONF_D/libretto-lx.conf
fi

# Siliconmotion : Workaroud for SM712
if lspci | grep -q '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 -qE '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 -q 'b43legacy' && [ -d /lib/firmware/b43legacy ];
then
  modprobe -r b43legacy
  modprobe b43legacy
fi

if lsmod | grep -q 'b43' && [ -d /lib/firmware/b43 ];
then
  modprobe -r b43
  modprobe b43
fi

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

if lsmod | grep -q 'ipw2200' && [ -r /lib/firmware/ipw2200-bss.fw ];
then
  modprobe -r ipw2200
  modprobe ipw2200
fi

# p54
if lsmod | grep -q 'p54pci' && [ -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 -q 'p54usb' && [ -r /lib/firmware/isl3886usb ];
then
  modprobe -r p54usb
  modprobe p54usb

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

# Enable Framebuffer console : needed for i810
#if [ -c /dev/fb0 ];
#then
#  /sbin/modprobe fbcon
#fi

# Lastly, Stop before Xorg start if flag set
for arg in $CMDLINE; do
  optarg=$( expr "x$arg" : 'x[^=]*=\(.*\)' )
  case $arg in
    stopbeforex)
      echo "DEBUG : Stop before Xorg."
      /bin/sh
      ;;
  esac
done

exit 0
