#!/bin/bash

# Script for special machines (and need root).
 
GEOMETRY="/tmp/.cocon.geometry"

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

# Force Framebuffer driver if boot option contains "forcefb*"
if [ "$COCON_FORCEFB" = "1" ];
then
  ln -s /usr/share/cocon/card-fbdev.conf /etc/X11/xorg.conf.d/card-fbdev.conf
fi

# Force 800x600 16bit color if contains "forcesvga"
if [ "$COCON_FORCESVGA" = "1" ];
then
  ln -s /usr/share/cocon/defaultdepth.conf /etc/X11/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 /usr/share/cocon/geode-800x480.conf /etc/X11/xorg.conf.d/geode-800x480.conf
      ;;
    1024x600)
      ln -sf /usr/share/cocon/geode-1024x600.conf /etc/X11/xorg.conf.d/geode-1024x600.conf
      ;;
    esac
    
  fi

  # KOHJINSHA PM
  if [ "`echo "$MACHINE_NAME" | grep 'PM Series'`" ];
  then
    ln -sf /usr/share/cocon/kohjinsha-pm.conf /etc/X11/xorg.conf.d/kohjinsha-pm.conf

  fi

  # TODO : Touchpanel driver

fi

# Workaroud for SONY PCG-C1 (with neomagic graphics)
# Select 2nd generation PCG-C1. 1st generation can't detect from dmidecode.
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 /usr/share/cocon/sony-pcg-c1.conf /etc/X11/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 /usr/share/cocon/libretto-lx.conf /etc/X11/xorg.conf.d/libretto-lx.conf
fi

# Siliconmotion : Workaroud for SM712
if [ "` lspci | grep -E '126f:0712' `" ];
then
  ln -sf /usr/share/cocon/smi_712.conf /etc/X11/xorg.conf.d/smi_712.conf 
fi


# Poulsbo : Software cursor
if [ "` lsmod | grep -E 'psb_gfx|gma500_gfx'`" ];
then
  ln -sf /usr/share/cocon/poulsbo.conf /etc/X11/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


exit 0
