#!/bin/zsh -f

VERSION='0.2.1'

# Feb 18 2005



# Figure out where we are running this from:

WeAreHere=$(dirname $0)

# Define what the dialog should be like
# Take a look at Pashua's Readme file for more info on the syntax

#############################################################
##########       Interface      #############################
#############################################################

# All of this gets put into a single variable called conf:
conf="
        # Set transparency: 0 is transparent, 1 is opaque
        tx.transparency=0.95
        
        # Heavy metal overdose:
        # appearance=metal
        
        # Set window title
        tx.windowtitle = X11.app Customizations
        
        #img.type = image
        #img.path = "$(dirname $WeAreHere)"/Resources/zsh_on_osx.png
        #img.maxheight = 100
        
        ####################
        # Introductory text:
        ####################
        tx.type=text
        tx.width = 1050
        tx.text = X11.app Customization Assistant.[return][return]This interface assists the  user  in changing settings that govern the behavior of X11.  The individual user can specify whether X11 should be added  to  the  user's startup  items, whether focus follows mouse behavior (emulating canonical X11 unix systems) should be set, whether the user should be  warned on  logout  if  X11 is running, whether X11 should be run as a dockless and menuless application in the background, as so forth.  An option to restore all default behaviors is included.[return]
        
        
        ###############################################################
        # Ask if X11.app should be included in the Login startup items:
        ###############################################################
        # Define radiobuttons
        rb.type = popup
        rb.width = 300
        rb.label = Add X11.app to my Login startup items?
        rb.option = Start X11 when I log in.
        rb.option = Do not start X11 when I log in.
        rb.option = Leave everything the way it is now.
        rb.default = Leave everything the way it is now.
        
        ###############################################################
        # Ask if xterm should be started automatically:
        ###############################################################
        tx2.type=text
        tx2.width = 1050
        tx2.text = X11.app starts an xterm every time X11.app starts up. If you find this behavior to be annoying, you can prevent xterm from starting automatically.
        
        # Define radiobuttons
        rb2.type = popup
        rb2.width = 300
        rb2.label = Should xterm be started automatically?
        rb2.option = Prevent xterm from starting automatically.
        rb2.option = Restore xterm to starting automatically.
        rb2.option = Leave everything the way it is now.
        rb2.default = Leave everything the way it is now.
           

        ###############################################################
        # Ask if Warning for Quitting X11.app should be turned off:
        ###############################################################
        tx3.type=text
        tx3.width = 1050
        tx3.text = X11.app has a 'quit alert' warning that pops up, preventing a clean logout.  This can be turned off.        
        
        
        # Define radiobuttons
        rb3.type = popup
        rb3.width = 300
        rb3.label = Get rid of the popup Quit Alert for X11?
        rb3.option = Get rid of it.
        rb3.option = Restore it.
        rb3.option = Leave everything the way it is now.
        rb3.default = Leave everything the way it is now.   
        
        
        ###############################################################
        # Ask if Focus Follows Mouse in X11.app should be turned on:
        ###############################################################
        tx4.type=text
        tx4.width = 1050
        tx4.text = X11.app can be set to focus-follows-mouse and click-through behavior that is typical of standard X11 behavior.  
        
        
        # Define radiobuttons
        rb4.type = popup
        rb4.width = 300
        rb4.label = Use Focus Follows Mouse in X11?
        rb4.option = Turn on Focus Follows Mouse.
        rb4.option = Turn off Focus Follows Mouse.
        rb4.option = Leave everything the way it is now.
        rb4.default = Leave everything the way it is now.   
         
        ###############################################################
        # Ask if X11.app should go Dockless:
        ###############################################################
        tx5.type=text
        tx5.width = 1050
        tx5.text = X11.app can be launched silently and without any trace in the dock or menu bar. (If you do this you will lose the ability to use the X11 menu and will have to launch X11 apps from the command line only.) PLEASE SAVE any work in running X11 applications prior to answering yes, as we need to kill the X-server before starting. No need to do this if you anser Leave It Alone.  
 
        # Define radiobuttons
        rb5.type = popup
        rb5.width = 300
        rb5.label = Shall we make X11 Dockless (and Menuless)?
        rb5.option = Yes, make X11 Dockless.
        rb5.option = No, restore X11 to the Dock.
        rb5.option = Leave everything the way it is now.
        rb5.default = Leave everything the way it is now.
        

        ###############################################################
        # Ask if DISPLAY should be programmed to accommodate multiple
        # simultaneous X11.app users:
        ###############################################################
        tx6.type=text
        tx6.width = 1050
        tx6.text = Set DISPLAY variable to allow X-windows programs to run from Terminal.app and iTerm.app. Fast User Switching creates the problem of multiple simultaneous instances of X11.app running and how to find the appropriate DISPLAY value to assign to each. Choosing "Yes" solves this problem. 

 
        # Define radiobuttons
        rb6.type = popup
        rb6.width = 300
        rb6.label = Set up DISPLAY for single or multiple users of X11.app?
        rb6.option = Yes, create the appropriate files.
        rb6.option = No, leave everything the way it is now.
        rb6.default = No, leave everything the way it is now.
        
    cn.type=cancelbutton
    cn.label=Cancel

               
"; # end of conf

pashua_run "$conf"        
        

#############################################################
##########   Begin Actual Program ###########################
#############################################################

    
    
    ########################################################
    #########     Check for Apple's X11.app      ###########
    ########################################################
    
    # Find out if we have Apple's X11.  If we don't, quit now.
    
    if [[ -d /Applications/Utilities/X11.app ]];then
        PATH_TO_X11='/Applications/Utilities/X11.app'
    elif [[ -d /Applications/X11.app ]];then
        PATH_TO_X11='/Applications/X11.app'
    elif [[ -x /usr/bin/mdfind && -d $(mdfind X11  | grep X11.app | head -n 1) ]];then
        PATH_TO_X11=$(mdfind X11  | grep X11.app | head -n 1)
    elif [[ -d $(locate X11.app | head -n 1) ]];then
        PATH_TO_X11=$(locate X11.app | head -n 1)
    else
        print "Can't locate Apple's X11.app"
        print "If you have it, put it in /Applications/Utilities or in"
        print "/Applications, or else run /usr/libexec/locate.updatedb "
        return 100
    fi



    ########################################################
    #######  Is X11.app in Login Startup Items?   ##########
    ########################################################

    
    # Check to see if X11.app is already among the Userlogin items.  If not, add it if the user
    # requested it.  (The check avoids having it in there twice.)

        
    
    AlreadyThere=$(command grep X11 ~/Library/Preferences/loginwindow.plist | command awk '{print $1}')
    
    if [[  -z $AlreadyThere ]]; then
        if [[ $rb == 'Start X11 when I log in.' ]];then
            cp /Users/$USER/Library/Preferences/loginwindow.plist \
                /Users/$USER/Library/Preferences/loginwindow.plist.backup
            defaults write loginwindow AutoLaunchedApplicationDictionary \
                  -array-add "<dict><key>Path</key><string>$PATH_TO_X11</string></dict>"
            print ""
            print "X11.app has now been added to the Users Accounts Login startup items."
            print ""
        elif [[ $rb == 'Do not start X11 when I log in.' ]];then
            open /System/Library/PreferencePanes/Accounts.prefPane
            # Maybe this can be gui scripted someday.
            print ""
            print "The user must manually remove X11.app from the Accounts preference pane."
            print ""            
        else  
            print ""
            print "Nothing has been added to the Users Accounts Login startup items."
            print ""            
        fi
    else
            print ""
            print "The X11.app has previously been added to the Users Accounts Login startup items."
            print "" 
    fi

    
    ########################################################
    ### Nix the start up of an xterm on login?   ###########
    ########################################################
     
    if [[ $rb2 == 'Prevent xterm from starting automatically.' ]];then

        if [[ ! -f ~/.xinitrc ]];then
            sed -e 's/xterm/# xterm/g' /etc/X11/xinit/xinitrc >| ~/.xinitrc
        else
            print " "
            print " "
            print "You already have a ~/.xinitrc file in your home directory, so I will"
            print "just comment out the call to xterm without clobbering this file."
            cp ~/.xinitrc ~/.xinitrc-backup
            chmod u+w ~/.xinitrc
            perl -pi -e 's/xterm/# xterm/g' ~/.xinitrc
        fi
     
    elif [[ $rb2 == 'Restore xterm to starting automatically.' ]];then
    
        if [[ ! -f ~/.xinitrc ]];then
            cp /etc/X11/xinit/xinitrc ~/.xinitrc
            chmod u+w ~/.xinitrc
            perl -pi -e 's/# xterm/ xterm/g' ~/.xinitrc
            perl -pi -e 's/#  xterm/ xterm/g' ~/.xinitrc
        else
            print " "
            print " "
            print "You already have a ~/.xinitrc file in your home directory, so I will"
            print "just add the call to xterm without clobbering this file."
            chmod u+w ~/.xinitrc
            cp ~/.xinitrc ~/.xinitrc-backup
            perl -pi -e 's/# xterm/ xterm/g' ~/.xinitrc
            perl -pi -e 's/#  xterm/ xterm/g' ~/.xinitrc
        fi
    
    else
        print "I will not create nor alter ~/.xinitrc" 
    fi


    ########################################################
    ###  Nix the Quit Alert warning on logout?   ###########
    ########################################################
     
        
    if [[ $rb3 == 'Get rid of it.' ]];then
        defaults write com.apple.x11 no_quit_alert true  
        print ""; print "writing 'no_quit_alert true' to com.apple.x11.plist"   
    elif [[ $rb3 == 'Restore it.' ]];then 
        defaults write com.apple.x11 no_quit_alert false  
        print ""; print "writing 'no_quit_alert false' to com.apple.x11.plist"   
    else
        print ""; print "leaving com.apple.x11.plist alone"
    fi


    ########################################################
    ###  Restore canonical window focus behavior?   ########
    ########################################################
  
    function change_ffm {
        defaults write com.apple.x11 wm_ffm $BOOL 
        defaults write com.apple.x11 wm_click_through -bool $BOOL 
        }
        
    if [[ $rb4 == 'Turn on Focus Follows Mouse.' ]];then
        BOOL='true'
        change_ffm
        BOOL=''
    
    elif [[ $rb4 == 'Turn off Focus Follows Mouse.' ]];then
        BOOL='false'
        change_ffm         
        BOOL=''   
    else
        BOOL=''
        print ""; print "leaving com.apple.x11.plist alone" 
    fi
    
 
    
            
    ########################################################
    ###  Get rix of X11.app's Dock Icon and menu bar?  #####
    ########################################################       
         
    function RemoveDockIcon { 
        
        killall X11
        # Find the lenght of the file Info.plist
        lines=$( wc -l $PATH_TO_X11/Contents/Info.plist | awk '{ print $1 }' )
        # Subtract two from this result
        twofewerlines=$((lines - 2))
        # Get all but the last two lines of the file
        command head -n $twofewerlines $PATH_TO_X11/Contents/Info.plist >| /tmp/X11_Info.plist
        # Back up the original file
        command gzip -f $PATH_TO_X11/Contents/Info.plist
        # Append new lines to the end of the trunctated file
        command echo "        <key>LSUIElement</key>"  >> /tmp/X11_Info.plist
        command echo "        <string>1</string>  "  >> /tmp/X11_Info.plist
        command echo "</dict>  "  >> /tmp/X11_Info.plist
        command echo "</plist>  "  >> /tmp/X11_Info.plist
        # copy it back
        command cp /tmp/X11_Info.plist $PATH_TO_X11/Contents/Info.plist
        #
        # Now issue the following command so that the change is made to take effect:
        
        /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f $PATH_TO_X11
        
        open -a X11
    }
    
    
    function RestoreDockIcon { 
        
        killall X11
        # Find the lenght of the file Info.plist
        lines=$( wc -l $PATH_TO_X11/Contents/Info.plist | awk '{ print $1 }' )
        # Subtract two from this result
        twofewerlines=$((lines - 4))
        # Get all but the last two lines of the file
        command head -n $twofewerlines $PATH_TO_X11/Contents/Info.plist >| /tmp/X11_Info.plist
        # Back up the original file
        command gzip -f $PATH_TO_X11/Contents/Info.plist
        # Append new lines to the end of the trunctated file
        command echo "        <key>LSUIElement</key>"  >> /tmp/X11_Info.plist
        command echo "        <string>0</string>  "  >> /tmp/X11_Info.plist
        command echo "</dict>  "  >> /tmp/X11_Info.plist
        command echo "</plist>  "  >> /tmp/X11_Info.plist
        # copy it back
        command cp /tmp/X11_Info.plist $PATH_TO_X11/Contents/Info.plist
        #
        # Now issue the following command so that the change is made to take effect:
        
        /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -f $PATH_TO_X11
        
        open -a X11
    }


        if [[ $rb5 == 'Yes, make X11 Dockless.' ]];then
            RemoveDockIcon
            print "X11 removed from the Dock."
        elif [[ $rb5 == 'No, restore X11 to the Dock.' ]]; then
            RestoreDockIcon
            print "X11 restored to the Dock."
        else
            print "Not changing X11's dock behavior."
        fi
        
###############################################################################
#
#    DISPLAY variable in startup scripts for bash, zsh and tcsh
#
###############################################################################

if [[ $rb6 == 'Yes, create the appropriate files.' ]];then

# bash:
touch ~/.bashrc
cat >>| ~/.bashrc   << eof-1

if [[ -z \$DISPLAY && -z \$SSH_CONNECTION ]]; then
	disp_no=(\$( ps -wwx | grep -F X11.app | awk '{print $NF}' | grep -e ":[0-9]"  ))
    if [[ -n \$disp_no ]];then
        export DISPLAY=\${disp_no}.0
    else
        export DISPLAY=:0.0
    fi
    echo "DISPLAY has been set to \$DISPLAY"
fi
eof-1

touch ~/.bash_profile
cat >>| ~/.bash_profile   << eof-11

if [[ -z \$DISPLAY && -z \$SSH_CONNECTION ]]; then
	disp_no=(\$( ps -wwx | grep -F X11.app | awk '{print $NF}' | grep -e ":[0-9]"  ))
    if [[ -n \$disp_no ]];then
        export DISPLAY=\${disp_no}.0
    else
        export DISPLAY=:0.0
    fi
    echo "DISPLAY has been set to \$DISPLAY"
fi
eof-11


# zsh:
touch ~/.zshrc
cat >>| ~/.zshrc   << eof-2

if [[ -z \$DISPLAY && -z \$SSH_CONNECTION ]]; then
	disp_no=(\$( ps -wwx | grep -F X11.app | awk '{print $NF}' | grep -e ":[0-9]"  ))
    if [[ -n \$disp_no ]];then
        export DISPLAY=\${disp_no}.0
    else
        export DISPLAY=:0.0
    fi
    echo "DISPLAY has been set to \$DISPLAY"
fi
eof-2

# Do this only if the .tcshrc file already exists, or else it will mess people up
   if [[ -f ~/.tcshrc ]];then

touch  ~/.tcshrc
cat >>| ~/.tcshrc   << eof-3
if (! \$?DISPLAY && ! \$?SSH_CONNECTION ) then
  setenv DISPLAY \`ps -wwx | grep -F X11.app | awk '{print $NF}' | grep -e ":[0-9]"  \`.0 
    if ( \$DISPLAY == '.0' )then
       setenv DISPLAY :0.0
    endif
  echo "DISPLAY has been set to \$DISPLAY"
endif   
eof-3

   else
    
touch  ~/.cshrc
cat >>| ~/.cshrc   << eof-4
if (! \$?DISPLAY && ! \$?SSH_CONNECTION ) then
  setenv DISPLAY \`ps -wwx | grep -F X11.app | awk '{print $NF}' | grep -e ":[0-9]"  \`.0 
    if ( \$DISPLAY == '.0' )then
       setenv DISPLAY :0.0
    endif
  echo "DISPLAY has been set to \$DISPLAY"
endif   
eof-4
    
    
    fi

else
    print "Remember to set the DISPLAY environment variable if you have not already done so."
fi
        
############ END #############################
