#!/bin/sh
# chkconfig: 5 95 05
# description: Saves and restores system entropy pool for \
#              higher quality random number generation.
# version 0.1, ok for Debian
# by K.L Haung, c00hkl00@nchc.gov.tw
# version 0.2, ok for Redhat and fit chkconfig's requirement
# by Steven Shiau, steven@nchc.gov.tw
# version 0.3, ok for Redhat and fit chkconfig's requirement
# only turn on rc5.d 
# by Steven Shiau, steven@nchc.gov.tw

# Source function library.
. /etc/init.d/functions

# function: gen_XF86Config
gen_XF86Config()
{

#
# Date: 2002/06/25
# Note: Modified by Kuo-Lien Huang for NCHC diskless (NFS+NIS+Etherboot+Debian)
#       workstation solution. 
#

if [ -e /etc/init.d/XF86_DEFAULT ]; then 
  . /etc/init.d/XF86_DEFAULT
fi

################################################################################
#
# Build the XF86Config file
#

XF86CONFIG=/etc/X11/XF86Config-4
#if [ -e ${XF86CONFIG} ]; then
#  exit
#fi

echo "## NCHC DRBL (diskless remote boot in Linux) (Etherboot+NFS+NIS) workstation" > ${XF86CONFIG}

#-----------------------------------------------------------------------
#
# Setup the FontPath info
#
if [ "${USE_XFS}" = "Y" ]; then
    cat <<-EOF >>${XF86CONFIG}

Section "Files"
    FontPath "/usr/X11R6/lib/X11/fonts/local"
    FontPath "/usr/X11R6/lib/X11/fonts/misc:unscaled"
    FontPath "/usr/X11R6/lib/X11/fonts/75dpi"
    FontPath "/usr/X11R6/lib/X11/fonts/100dpi"
    FontPath "/usr/X11R6/lib/X11/fonts/Type1"
    FontPath "/usr/X11R6/lib/X11/fonts/Speedo"
    FontPath "/usr/X11R6/lib/X11/fonts/CID"
    FontPath "/usr/X11R6/lib/X11/fonts/TrueType"
    FontPath "unix/:7100"
    #FontPath   "tcp/${XFS_SERVER}:7100"
EndSection

Section "Module"
    Load "freetype"
EOF
else
cat <<-EOF >>${XF86CONFIG}
Section "Files"
    FontPath "/usr/X11R6/lib/X11/fonts/local"
    FontPath "/usr/X11R6/lib/X11/fonts/misc:unscaled"
    FontPath "/usr/X11R6/lib/X11/fonts/75dpi"
    FontPath "/usr/X11R6/lib/X11/fonts/100dpi"
    FontPath "/usr/X11R6/lib/X11/fonts/Type1"
    FontPath "/usr/X11R6/lib/X11/fonts/Speedo"
    FontPath "/usr/X11R6/lib/X11/fonts/CID"
    FontPath "/usr/X11R6/lib/X11/fonts/TrueType"
EndSection

Section "Module"
    Load "xtt"
EOF
fi

#-----------------------------------------------------------------------
#
# Setup module info
#
cat <<-EOF >>${XF86CONFIG}
    Load "extmod"
    Load "bitmap"
    Load "dbe"
    Load "ddc"
    Load "dri"
    Load "glx"
    Load "int10"
    Load "pex5"
    Load "record"
    Load "speedo"
    Load "type1"
    Load "vbe"
    Load "xie"
EndSection
EOF
#-----------------------------------------------------------------------
#
# Setup Keyboard and Mouse info
#
X_MOUSE_PROTOCOL=${X_MOUSE_PROTOCOL}
X_MOUSE_DEVICE=${X_MOUSE_DEVICE}
X_MOUSE_BUTTONS=${X_MOUSE_BUTTONS}
if [ "${X_MOUSE_BUTTONS}" = "3" ]; then
  EMULATE_3_BUTTONS = "false"
fi 

cat <<-EOF >>${XF86CONFIG}

Section "InputDevice"
    Identifier  "Generic Keyboard"
    Driver      "keyboard"
    Option      "CoreKeyboard"
    Option      "XkbRules"  "xfree86"
    Option      "XkbRules"  "pc104"
    Option      "XkbLayout" "us"
EndSection

Section "InputDevice"
    Identifier  "Configured Mouse"
    Driver      "mouse"
    Option      "CorePointer"
    Option      "Device"          "${X_MOUSE_DEVICE:-"/dev/psaux"}"
    Option      "Protocol"        "${X_MOUSE_PROTOCOL:-"PS/2"}"
    Option      "Emulate3Buttons" "${EMULATE_3_BUTTONS:-"true"}"
    Option      "ZAxisMapping"     "4 5"
EndSection
EOF

#-----------------------------------------------------------------------
#
# Setup the Monitor info
#

X_HORZSYNC=${X_HORZSYNC}
X_VERTREFRESH=${X_VERTREFRESH}
X_DRIVER=`/etc/init.d/pci_scan /etc/init.d/vidlist`
#if [ -z "${X_DRIVER}" ]; then
#    X_DRIVER="fbdev"
#fi

cat <<-EOF >>${XF86CONFIG}

Section "Monitor"
        Identifier      "My Monitor"
        VendorName      "Unknown"
        ModelName       "Unknown"
        HorizSync       ${X_HORZSYNC:-"31-62"}
        VertRefresh     ${X_VERTREFRESH:-"55-99"}
EndSection

Section "Device"
        Identifier "My Video Card"
        Driver     "${X_DRIVER}"
EndSection

EOF
#-----------------------------------------------------------------------
#
# Setup the Screen info
#
X_COLOR_DEPTH=${X_COLOR_DEPTH}
MODE[0]=${X_MODE_0}
MODE[1]=${X_MODE_1}
MODE[2]=${X_MODE_2}

if [ -z "${MODE[0]}${MODE[1]}${MODE[2]}" ]; then
    MODE[0]="1024x768"
    MODE[1]="800x600"
    MODE[2]="640x480"
fi

if [ -n "${MODE[0]}" ]; then
    MODE[0]=\"${MODE[0]}\"
fi

if [ -n "${MODE[1]}" ]; then
    MODE[1]=\"${MODE[1]}\"
fi

if [ -n "${MODE[2]}" ]; then
    MODE[2]=\"${MODE[2]}\"
fi

cat <<-EOF >>${XF86CONFIG}
Section "Screen"
        Identifier "Default Screen" 
        Device "My Video Card" 
        Monitor "My Monitor"
        DefaultDepth  ${X_COLOR_DEPTH:-16}
        Subsection "Display"
                Depth ${X_COLOR_DEPTH:-16}
                Modes ${MODE[*]}
        EndSubSection
EndSection

EOF

}

case "$1" in
	start)
		if [ -e /etc/X11/XF86Config-4 ]; then
			mv /etc/X11/XF86Config-4 /etc/X11/XF86Config-4.setupx
		fi
		gen_XF86Config
	;;
	stop)
		if [ -e /etc/X11/XF86Config-4.setupx ]; then
			mv /etc/X11/XF86Config-4.setupx /etc/X11/XF86Config-4
		fi
	;;
	restart)
		$0 stop
		$0 start		
	;;
	*)
	echo "Usage: /etc/init.d/setupx {start|stop|restart}" >&2
	exit 1
	;;
esac	
