#!/bin/sh
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin

if [ -z "$LOGNAME" ]; then
if grep -w -q xlogin /proc/cmdline ; then
cat > /etc/X11/xorg.conf << "EOF"
Section "Module"
    Load        "extmod"
    SubSection  "extmod"
    Option      "omit xfree86-dga"
    EndSubSection
EndSection

Section "Files"
	RgbPath      "/usr/share/X11/rgb"
	ModulePath   "/usr/lib/xorg/modules"
	FontPath     "/usr/share/fonts/TTF"
	FontPath     "/usr/share/fonts/misc"
        FontPath     "/usr/share/fonts/75dpi"
EndSection

Section "ServerFlags"
EndSection

Section "Monitor"
    Identifier  "My Monitor"
    Option	"DPMS"
EOF

ddcxinfo -monitor -modes 2>/dev/null > /tmp/ddcxinfo
if ! grep -q -w HorizSync /tmp/ddcxinfo ; then
echo "        HorizSync  28.0 - 68.0
        VertRefresh  60.0 - 85.0
EndSection" >> /etc/X11/xorg.conf
Modes='Modes "1024x768"'
else
grep -w HorizSync /tmp/ddcxinfo >> /etc/X11/xorg.conf
echo "        VertRefresh  60.0 - 85.0" >> /etc/X11/xorg.conf
echo "EndSection" >> /etc/X11/xorg.conf
if grep -q -w Modes /tmp/ddcxinfo ; then
Modes="`grep -w Modes /tmp/ddcxinfo`"
else
Modes='Modes "1024x768"'
fi
fi

cat >> /etc/X11/xorg.conf << EOF

Section "InputDevice"
    Identifier	"Keyboard1"
    Driver	"kbd"
    Option	"AutoRepeat"	"250 30"
EndSection

Section "InputDevice"
    Identifier	    "Mouse1"
    Driver	    "mouse"
    Option	    "Protocol" "Auto"
EOF

mouse_probe() {
mouse="`mdetect -n -x 2>/dev/null | head -n 1`"
if [ ! "$mouse" = "" ]; then
if [ -r "$mouse" ]; then
if [ "$mouse" = "/dev/input/mouse0" -a -r "/dev/input/mice" ]; then
echo '    Option      "Device"        "/dev/input/mice"' >> /etc/X11/xorg.conf
else
echo '    Option      "Device"        "'$mouse'"' >> /etc/X11/xorg.conf
fi
else
if [ -r /dev/input/mouse0 -a -r /dev/input/mice ]; then
echo '    Option      "Device"        "/dev/input/mice"' >> /etc/X11/xorg.conf
elif [ -r /dev/psaux ]; then
echo '    Option      "Device"        "/dev/psaux"' >> /etc/X11/xorg.conf
elif [ -r /dev/mouse ]; then
echo '    Option      "Device"        "/dev/mouse"' >> /etc/X11/xorg.conf
fi
fi
else
if [ -r /dev/input/mouse0 -a -r /dev/input/mice ]; then
echo '    Option      "Device"        "/dev/input/mice"' >> /etc/X11/xorg.conf
elif [ -r /dev/psaux ]; then
echo '    Option      "Device"        "/dev/psaux"' >> /etc/X11/xorg.conf
elif [ -r /dev/mouse ]; then
echo '    Option      "Device"        "/dev/mouse"' >> /etc/X11/xorg.conf
fi
fi
}

mouse_probe

cat >> /etc/X11/xorg.conf << EOF
    Option          "ZAxisMapping" "4 5"
EndSection
EOF

if grep -q Synaptics /proc/bus/input/devices ; then
cat >> /etc/X11/xorg.conf << EOF

Section "InputDevice"
    Identifier	"Synaptics Touchpad"
    Driver	"synaptics"
    Option	"Protocol"	"auto-dev"
EOF

mouse_probe

cat >> /etc/X11/xorg.conf << EOF
    Option	"LeftEdge"      "1700"
    Option	"RightEdge"     "5300"
    Option	"TopEdge"       "1700"
    Option	"BottomEdge"    "4200"
    Option	"FingerLow"	"25"
    Option	"FingerHigh"	"30"
    Option	"MaxTapTime"	"180"
    Option	"MaxTapMove"	"220"
    Option	"VertScrollDelta" "100"
    Option	"MinSpeed"	"0.09"
    Option	"MaxSpeed"	"0.18"
    Option	"AccelFactor"	"0.0015"
    Option      "SHMConfig"     "on"
EndSection
EOF
elif grep -q ALPS /proc/bus/input/devices ; then
cat >> /etc/X11/xorg.conf << EOF

Section "InputDevice"
    Identifier	"Alps Touchpad"
    Driver	"synaptics"
    Option	"Protocol"		"auto-dev"
EOF

mouse_probe

cat >> /etc/X11/xorg.conf << EOF
    Option	"LeftEdge"		"120"
    Option	"RightEdge"		"830"
    Option	"TopEdge"		"120"
    Option	"BottomEdge"		"650"
    Option	"FingerLow"		"14"
    Option	"FingerHigh"		"15"
    Option	"MaxTapTime"		"180"
    Option	"MaxTapMove"		"110"
    Option	"EmulateMidButtonTime"	"75"
    Option	"VertScrollDelta"	"20"
    Option	"HorizScrollDelta"	"20"
    Option	"MinSpeed"		"0.3"
    Option	"MaxSpeed"		"0.75"
    Option	"AccelFactor"		"0.015"
    Option	"EdgeMotionMinSpeed"	"200"
    Option	"EdgeMotionMaxSpeed"	"200"
    Option	"UpDownScrolling"	"1"
    Option	"CircularScrolling"	"1"
    Option	"CircScrollDelta"	"0.1"
    Option	"CircScrollTrigger"	"2"
EndSection
EOF
fi

cat >> /etc/X11/xorg.conf << EOF

Section "Device"
    Identifier  "Unknown Monitor"
    Driver      "vesa"
EndSection

Section "Screen"
    Identifier  "Screen 1"
    Device      "Unknown Monitor"
    Monitor     "My Monitor"
    DefaultDepth 16
    Subsection "Display"
        Depth       24
        $Modes
    EndSubsection
    Subsection "Display"
        Depth       16
        $Modes
    EndSubsection
EndSection

Section "ServerLayout"
    Identifier  "Simple Layout"
    Screen "Screen 1"
    InputDevice "Mouse1" "CorePointer"
EOF

if grep -q Synaptics /proc/bus/input/devices ; then
cat >> /etc/X11/xorg.conf << EOF
    InputDevice "Synaptics Touchpad" "SendCoreEvents"
EOF
elif grep -q ALPS /proc/bus/input/devices ; then
cat >> /etc/X11/xorg.conf << EOF
    InputDevice "Alps Touchpad" "SendCoreEvents"
EOF
fi

cat >> /etc/X11/xorg.conf << EOF
    InputDevice "Keyboard1" "CoreKeyboard"
EndSection
EOF

cat > /root/.xinitrc << EOF
#!/bin/sh
/usr/bin/xsetroot -solid "#266294"
echo "Don't delete this file!" > /tmp/.success
/usr/bin/xset s off
exec /usr/bin/fluxbox
EOF
chmod a+rwx /root/.xinitrc

echo "exec /usr/bin/Xorg -dpms -s 240 -bestRefresh -nolisten tcp" >/usr/bin/X
chmod a+rwx /usr/bin/X

#if grep -w -q xlogin /proc/cmdline ; then
  #echo "*** Running DHCPCD to setup network connection..."
  #/sbin/dhcpcd -t 30 1>/dev/null 2>/dev/null || /sbin/pump 1>/dev/null 2>/dev/null || (echo "*** ERROR: DHCPCD FAILED!!!" ; sleep 3)
  #echo "*** Running Alsaconf to setup sound..."
  #/usr/sbin/alsaconf -a 1>/dev/null 2>/dev/null || (echo "*** ERROR: SOUND SETUP FAILED!!!" ; sleep 3)
#fi

echo 'startx 2>/dev/null
#if [ ! -f /tmp/.success ]; then
#sed "s/DefaultDepth 24/DefaultDepth 16/g" /etc/X11/xorg.conf >/tmp/xorg.conf.new
#mv /tmp/xorg.conf.new /etc/X11/xorg.conf && startx 2>/dev/null
if [ ! -f /tmp/.success ]; then
echo '"'"'*** ERROR: Run "xsetup"? If X doesnt start, you
    probably have to forget about it!'"'"'
exit
fi
#fi
reset
#DELETE' >/root/.bash_login

  echo '[startup] {echo -e "Right-click mouse for menu, and move cursor\nto bottom of screen to reveal toolbar.\n\nYou can also press Alt-F9 for menu, and\nAlt-F10 to exit menu.\n\nChoose Setup from the menu to setup network connection,\ndownload Flashplayer for FireFOX, and setup sound.\n\nPress Alt-F11 for sound mixer." | xmessage -center -file -}' >/root/.fluxbox/apps
  if lsmod | grep -q soundcore ; then
    echo '[startup] {echo -e "Right-click mouse for menu, and move cursor\nto bottom of screen to reveal toolbar.\n\nYou can also press Alt-F9 for menu, and\nAlt-F10 to exit menu.\n\nChoose Setup from the menu to setup network connection,\ndownload Flashplayer for FireFOX, and setup sound.\n\nPress Alt-F11 for sound mixer.\n\nNOTE: Sound is probably already setup, because Kmod\nautomatically loads the sound modules at boot time.\nThere'"'"'s no harm in doing it again." | xmessage -center -file -}' >/root/.fluxbox/apps
  fi
  echo "*** Starting X..."
  
  exec /bin/login -f root

elif grep -w -q autologin /proc/cmdline ; then
  exec /bin/login -f root
else
  exec /bin/login
fi
fi
