#!/bin/bash 

# 3.09.2010 Enlik <sn at ubucentrum.net>
# Some fixes for sabutil script from app-misc/sabayonlive-tools-2.2-r5.
# Feel free to remove this notice, any part of it or any line I made.

USER=$(users)
HOMEDIR=/home/`cat /etc/passwd | grep 1000 | cut -d: -f1`/
echo "$HOMEDIR"

SYS=/sbin
echo "$SYS"
 
 function menu
{
     clear
     selection=""
     until  [[ "$selection" = "0" ]] ; do
	 echo ""
         echo "Current Operating System:" $(cat /etc/sabayon-release)
         echo "Current Edition:" $(cat /etc/sabayon-edition)
         echo
	 echo "$myname"
         
 
	 echo ""
	 echo "1 - Backup & edit xorg.conf "
	 echo "2 - Restore original xorg.conf"
	 echo "3 - Regenerate Sabayon xorg.conf"
	 echo "4 - Backup & edit grub.cfg"
	 echo "5 - Restore original grub.cfg"
	 echo "6 - Package Management."
         echo "----------------------------------------"
         echo "View and AutoPaste:"
         echo " "
         echo "01 - Pastebin Xorg.0.log"
         echo "02 - Pastebin .xsession-errors"
         echo "03 - Pastebin dmesg"
         echo "04 - Pastebin messages"
         echo "05 - Pastebin rc.log"
         echo "06 - Pastebin grub.cfg" 
         echo "07 - Pastebin fdisk -l"
         echo "08 - Pastebin kdm.log"
         echo "09 - Pastebin Hardware Info"
         echo ""
	 echo "0 - Exit"
	 echo ""
	 echo -n "Enter selection: "
	 read selection
	 echo ""
 
 
	 case "$selection" in
	     1 ) xorg_backup ; press_enter ; nano /etc/X11/xorg.conf; press_enter ;;
	     2 ) xorg_restore ; press_enter ;;
	     3 ) xorg_regen ; press_enter ;;
	     4 ) grub_backup ; press_enter ; nano /boot/grub/grub.cfg; press_enter ;;
	     5 ) grub_restore ; press_enter ;;
	     6 ) packages ; press_enter ;;
             01 ) xorglog ; press_enter ;;
	     02 ) xsessionerrors ; press_enter ;;
             03 ) dmesg ; press_enter ;;
             04 ) messages ; press_enter ;;
             05 ) rclog ; press_enter ;;
             06 ) grub ; press_enter ;;
             07 ) fdisk ; press_enter ;;
             08 ) kdm ; press_enter ;;
             09 ) system ; press_enter ;;


             0 ) exit ;;
	     * ) echo "Please be sensible - choose a number that exists in the menu"; press_enter
	 esac
     done
 
 }
 
 
 
 function press_enter
{ 
     echo ""
     echo -n "Press Enter to continue"
     read
     clear
 }
 
 function xorg_regen
{
    if [[ ! -f "/etc/X11/xorg.conf.sabayon" ]]; then
	cat > /etc/X11/xorg.conf.sabayon <<EOF
Section "Module"
    SubSection  "extmod"
       Option    "omit xfree86-dga"
    EndSubSection
    Load        "i2c"
    Load        "ddc"
    Load        "synaptics"
    Load        "vbe"
#   Load        "dri"
EndSection


Section "ServerFlags"
     Option     "AllowMouseOpenFail" "true"
EndSection

Section "InputDevice"
    Identifier          "Synaptics1"
    Driver              "synaptics"
    Option              "SendCoreEvents"        "true"
    Option              "Device"                "/dev/psaux"
    Option              "Protocol"              "auto-dev"
    Option              "HorizScrollDelta"      "0"
    Option              "SHMConfig"             "on"
    # For ALPS/MacBook TouchPads
    #Option             "MaxSpeed"              "0.7"
    #Option             "MinSpeed"              "0.18"
    #Option             "AccelFactor"           "0.08"
    #Option             "TopEdge"               "120"
    #Option             "LeftEdge"              "120"
    #Option             "BottomEdge"            "830"
    #Option             "RightEdge"             "650"
    #Option             "FingerLow"             "25"
    #Option             "FingerHigh"            "30"
    # MacBook touchpad
    #Option             "MaxTapTime"            "180"
    #Option             "MaxTapMove"            "220"
    #Option             "MaxDoubleTapTime"      "180"
    #Option             "VertScrollDelta"       "20"
    #Option             "HorizScrollDelta"      "50"
    #Option             "TapButton2"            "3"
    #Option             "TapButton3"            "2"
    #Option             "VertTwoFingerScroll"   "1"

    # Do you keep moving the mouse while typing? Try this trick.
    #synclient TouchpadOff=1 disable your synaptics touchpad
    #synclient TouchpadOff=0 enable your synaptics touchpad
EndSection


# **********************************************************************
# Core Pointer's InputDevice section
# **********************************************************************

Section "InputDevice"
    Identifier  "Mouse1"
    Driver      "mouse"
    Option      "Device"        "/dev/psaux"
EndSection

# **********************************************************************
# Monitor section
# **********************************************************************

# Any number of monitor sections may be present

Section "Monitor"
    Identifier  "Generic Monitor"
    VertRefresh 43 - 60
    HorizSync   28 - 80
EndSection

# **********************************************************************
# Graphics device section
# **********************************************************************

# Any number of graphics device sections may be present

Section "Device"
    Identifier  "VESA"
    Driver      "vesa" # do not remove vesa
    #Option "RenderAccel" "on"
    #Option "XAANoOffscreenPixmaps"
    #Option "BusType" "PCI"
    #Option "ColorTiling" "on"
    #Option "EnablePageFlip" "on"
    Option "UseEvents" "True"
EndSection


# **********************************************************************
# Screen sections.
# **********************************************************************

Section "Screen"

# The Identifier, Device and Monitor lines must be present

    Identifier  "Screen 1"
    Device      "VESA"
    Monitor     "Generic Monitor"
    #Option "AddARGBGLXVisuals" "true"

# The favoured Depth and/or Bpp may be specified here

    DefaultDepth 24

    SubSection "Display"
        Depth           8
        ViewPort        0 0
        #Modes          "1024x768" "800x600" "640x480"
    EndSubsection

    SubSection "Display"
        Depth           16
        ViewPort        0 0
        #Modes          "1024x768" "800x600" "640x480"
    EndSubsection

    SubSection "Display"
        Depth           24
        ViewPort        0 0
        #Modes          "1024x768" "800x600" "640x480"
    EndSubsection


EndSection


Section "ServerLayout"
# The Identifier line must be present

    Identifier  "Main Layout"
    Screen 0    "Screen 1"
    InputDevice "Mouse1" "CorePointer"
    #InputDevice "Synaptics1" "SendCoreEvents"

EndSection

Section "DRI"
    Mode 0666
EndSection

Section "Extensions"
   #Option "Composite" "Enable"
EndSection
EOF
    fi
	
	mv -f /etc/X11/xorg.conf /etc/X11/xorg.conf.old
	/usr/sbin/x-setup-configuration

}
 
 function xorg_backup
 { 
     if [[ -f /etc/X11/xorg.conf.BKUP ]]; then 
	 echo "xorg.conf backup already exists, so the new one wasn't made."
	 echo "Now off we go to edit the file...."
     else
	 cp /etc/X11/xorg.conf /etc/X11/xorg.conf.BKUP
	 echo "Making backup of xorg.conf as /etc/X11/xorg.conf.BKUP"
	 echo ""
	 echo "INTEL graphics users please read the url below before proceding"
	 echo ""
	 echo "http://gentoo-wiki.com/HOWTO_Intel_Onboard_Graphics_Notebooks_Native_Resolution"
	 
     fi
 
 }
 
 
 function xorg_restore
 { 
     if [[ -f /etc/X11/xorg.conf.BKUP ]]; then
	 echo "xorg.conf backup (/etc/X11/xorg.conf.BKUP) exists, whew!"
	 cp /etc/X11/xorg.conf.BKUP /etc/X11/xorg.conf
	 echo ""
	 echo "The original xorg.conf has been restored"
     else
         echo "UH OH!! The backup /etc/X11/xorg.conf.BKUP hasn't been made."
	 echo "Don't panic, you can copy the file from the Live DVD/CD."
     fi
 
 }
 
 
 
 function grub_backup
 {
     if [[ -f /boot/grub/grub.cfg.BKUP ]]; then
	 echo "grub.cfg backup already exists, so the new one wasn't made."
	 echo "Now off we go to edit the file...."
     else
	 cp /boot/grub/grub.cfg /boot/grub/grub.cfg.BKUP
	 echo "Making backup of grub.cfg as /boot/grub/grub.cfg.BKUP"
	 echo ""
     fi
 
 }
 
 
 function grub_restore
 {
     if [[ -f /boot/grub/grub.cfg.BKUP ]]
     then
	 echo "grub.cfg backup (/boot/grub/grub.cfg.BKUP) exists, whew!"
	 cp /boot/grub/grub.cfg.BKUP /boot/grub/grub.cfg
	 echo ""
	 echo "The original grub.cfg has been restored"
     else
	 echo "UH OH!! The backup script /boot/grub/grub.cfg.BKUP hasn't been made."
	 echo "Don't panic, you can use the Sabayon installer to repair GRUB."
     fi
 
 }

 function horner
 {
 
 # Quick system info gatherer written for Sabayon GNU/Linux
 #(http://sabayonlinux.org)
 # Copyright 2008 Richard Edward Horner
 # Last modified 2008-11-26
 # Please send all comments, suggestions, bugs and patches to (rich AT
 #richhorner DOT com)
 
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 HOSTNAME=`hostname`
 RELEASE=`cat /etc/sabayon-release`
 CPU=`uname -p`
 KERNEL=`uname -r`
 KERN_ARCH=`uname -m`
 DISK=`df -lT --exclude-type=tmpfs --exclude-type=rootfs | awk 'BEGIN { total = 0; used = 0 } { if (NR > 1) { total += $3; used += $4 } } END { printf "%.1f/%.1fGB", used / 1000000, total / 1000000 }'`
 MEMORY=`cat /proc/meminfo | awk '{ if ($1 == "MemTotal:") { memtot = $2/1000 } else if ($1 == "MemFree:") { memfree = $2/1000 } } END { printf "%d/%dM", memfree, memtot }'`
 DAYS=`cat /proc/uptime | awk '{ printf "%.1f", $1/86400 }'`
 PROCS=`ps aux | awk 'END { print NR - 2 }'`
 RENDERER=`glxinfo | awk -F : '{ if ($1 == "OpenGL renderer string") print $2 }'`
 
 echo "Sysinfo for '$HOSTNAME': $RELEASE with $KERN_ARCH kernel $KERNEL
 on $CPU, HD: $DISK, MEM: $MEMORY, Renderer: $RENDERER, $PROCS procs,
 up $DAYS days"

 } 

 # pastebunz seems to have some limit on max pasted lines, so this would be handy...
 function files_pastebin_linelimit
 {
     # args: limit file(s) to pastebin
     # example: file_pastebin_linelimit file1 "file name" file3 ...
     # first arg: 0 means no limit
     # By Enlik <sn at ubucentrum.net>
     local TMPFILE=~/newbietmp
     local LINESLIMIT
     local LLIMIT_H
     
     if [[ $# -le 1 ]]; then
         echo "Warning: no files to paste (in files_pastebin_linelimit)." >&2
         return 2
     fi
     
     let LINESLIMIT=$1
     shift
     # If there is a limit of lines, we need to make space for file name and blank
     # lines to be pasted. Otherwise we have as much space as we want.
     if [[ $LINESLIMIT -eq 0 ]]; then
         LLIMIT_H="+1"
     else
         LLIMIT_H=$(( LINESLIMIT - 3 ))
     fi
     
     >"$TMPFILE" || {
         echo "Oh no! I can't create temporary file... Aborting." >&2
         return 3
     }
     
     for file in "$@"; do
	echo " $file"
	echo ""
	if [[ -r $file ]]; then
	     # This tail guarantees that LAST file name is visible (unless the limit is really small, <= 2 lines)
	     # (previous one(s) can be wiped by tail due to limit of lines).
	     # The file name is always visible if there's only one specified
	     # as argument (unless the limit value is really small, as above).
	     # All file names are always visible if there's no limit
	     # of lines, or the limit is big enough.
	     cat "$file" | tail -n $LLIMIT_H
	else
	     echo "THIS FILE COULDN'T BE READ." 
	fi
	echo ""
     done >> "$TMPFILE"
     if [[ $LINESLIMIT -eq 0 ]]; then
	cat "$TMPFILE" | pastebunz
     else
	cat "$TMPFILE" | tail -n $LINESLIMIT | pastebunz
     fi
     echo "** Please see the link above! **"
     
     rm "$TMPFILE"
     return 0
 }
 
 function files_pastebin
 {
     files_pastebin_linelimit 0 "$@"
 }

 function pastebinner
 {
     >~/newbietmp || {
	 echo "Oh no! I can't create temporary file... Aborting."
	 return 1
     }
     horner >> ~/newbietmp
 
    echo "
 /usr/sbin/lspci
 " >> ~/newbietmp
     lspci >> ~/newbietmp

     echo "
 /usr/sbin/lsusb
 " >> ~/newbietmp
     lsusb >> ~/newbietmp

     echo "
 
 /bin/lsmod
 " >> ~/newbietmp
     lsmod >> ~/newbietmp
     
     echo "
 uname -r
 " >> ~/newbietmp
     uname -r >> ~/newbietmp
    
     echo "
 eselect kernel list
 " >> ~/newbietmp
     eselect --no-color kernel list >> ~/newbietmp
    
     echo "
 eselect opengl list
 " >> ~/newbietmp
     eselect --no-color opengl list >> ~/newbietmp
    
     echo "
 eselect java-vm list
 " >> ~/newbietmp
     eselect --no-color java-vm list >> ~/newbietmp
    
     echo "
 xorg.conf
 " >> ~/newbietmp
     
     
     cat /etc/X11/xorg.conf >> ~/newbietmp
     
     cat ~/newbietmp
     cat ~/newbietmp | pastebunz
     echo "*** Please see the link above. ***"
     rm ~/newbietmp
 }
 
 function packages
 {
     clear
     echo "Warning - this function is not implemented."
     echo "You may play with the menu though. ;)"
     pack_sel=
     until [[ "$pack_sel" = "3" ]]; do
 
	 echo "PACKAGE MANAGEMENT SUB-MENU"
	 echo ""
	 echo "1 - Portage - Source based packages"
	 echo "2 - Entropy - Binary based packages"
	 echo ""
	 echo "3 - MAIN MENU"
 
	 echo ""
	 echo -n "Enter selection: "
	 read pack_sel
	 echo ""
	 case "$pack_sel" in
	     1 ) echo "portage"; press_enter ;;
	     2 ) echo "entropy"; press_enter ;;
	     3 ) echo "returning to MAIN MENU" ;;
	     * ) echo "Please be sensible - choose a number that exists in the menu"; press_enter ;;
	 esac
     done
 
 }

function system
 {
     local PASTE_TMP=/tmp/sabutil-tmp
     
     >"$PASTE_TMP" || {
	     echo "Oh no! I can't create temporary file... Aborting."
	     return 1
	 }
    
     echo "
     sh $SYS/logscript.sh
     " >> "$PASTE_TMP"
     sh "$SYS/logscript.sh" >> "$PASTE_TMP"
    
     cat "$PASTE_TMP"
     cat "$PASTE_TMP" | pastebunz
     echo "*** Please see the link above. ***"
     rm "$PASTE_TMP"
    
}

function xorglog
 {
     cat /var/log/Xorg.0.log
     files_pastebin_linelimit 150 /var/log/Xorg.0.log
 } 
 
function xsessionerrors
 {
     cat "$HOMEDIR/.xsession-errors" | tail -n 10
     files_pastebin_linelimit 150 "$HOMEDIR/.xsession-errors"
 }

 function dmesg
 {
     cat /var/log/dmesg
     files_pastebin_linelimit 150 /var/log/dmesg
 }
 
 function grub
 {
     cat /boot/grub/grub.cfg
     files_pastebin /boot/grub/grub.cfg
 }
 
function fdisk
 {
     local FDISK_TMP=/tmp/sabutil-tmp

     >"$FDISK_TMP" || {
	     echo "Oh no! I can't create temporary file... Aborting."
	     return 1
	 }
	 
     (
     echo "
     fdisk -l
     ";
     fdisk -l;
     ) > "$FDISK_TMP"
     
     cat "$FDISK_TMP"
     cat "$FDISK_TMP" | pastebunz
     echo "*** Please see the link above. ***"
     rm "$FDISK_TMP"
 }
 
function messages
 {
     cat /var/log/messages
     files_pastebin_linelimit 150 /var/log/messages
 }
 
function kdm
 {
     cat /var/log/kdm.log
     files_pastebin_linelimit 150 /var/log/kdm.log
 }

function rclog
 {
     if [[ -f /var/log/rc.log ]]; then
	 cat /var/log/rc.log | tail -n 10
	 files_pastebin_linelimit 150 /var/log/rc.log
     else
         echo "You Don't Have rc.log enabled in your /etc/rc.log"
         echo " To enable, edit /etc/rc.log change NO to YES for rc_logger= "
         echo ""
     fi
 }

 clear
 myname="Pick A Choice From Menu Below"

 if [[ $1 = "menu" ]]; then
     if [[ $EUID -ne 0 ]] && [[ $EUID -ne 1000 ]]; then
         echo "$myname"
         echo ""
         echo "This script should be run as root, or by someone in the root group.  Some commands will not work for you." 1>&2
     fi
     menu
 elif [[ $1 = "--help" || $1 = "-h" ]]; then
    echo "usage:"
    echo "$0 - will print some basic system infos"
    echo "$0 menu - will show you menu"
 else
     horner
 fi


