"=====================================================================
" creamrc -- Initialize path ($CREAM) and load main (cream.vim)
"
" Cream -- An easy-to-use configuration of the famous Vim text editor
" Copyright (C) 2002-2003  Steve Hall
"
" License:
" 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 2 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, write to the Free Software
" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
" 02111-1307, USA.
"

" CRITICAL SETTINGS: (Don't use fold markers yet!)

" nocompatible (behave like Vim, not Vi)
set nocompatible

" cpoptions (exclude characters from literal mappings)
set cpoptions-=<

" shellslash (use a common path separator across all platforms)
" convert all backslashes to forward slashes on expanding filenames.
" Enables consistancy in Cream between Windows and Linux platforms,
" but BE CAREFUL! Windows file operations require backslashes--any
" paths determined manually (not by Vim) need to be reversed.
set shellslash

function! Cream_checkdir(dir)
" if directory doesn't exist, try to make it

	if isdirectory(a:dir) != 1

		" system call prep
		if has("win32")
			" remove trailing slash (Win95)
			let tmp = substitute(a:dir, '\(\\\|/\)$', '', 'g')

			" convert slashes to backslashes
			let tmp = substitute(tmp, '/', '\', 'g')
		else
			let tmp = a:dir
		endif

		" mkdir (quote, regardless of spaces)
		set noshellslash
		call system("mkdir " . '"' . tmp . '"')
		set shellslash

	endif

endfunction

" path initializations
" Cream_init() ($CREAM)   $VIMRUNTIME/cream {{{1
function! Cream_init()
" establish $CREAM
" o Valid value is writable:
"   * $HOME/.cream in Linux
"   * $VIMRUNTIME/cream/  in Windows
" o $HOME on Windows is trouble
"   * Doesn't exist for Win95/98/ME
"   * May not be multi-user on WinNT/2K/XP and Cygwin setups
" o We don't escape the value of $CREAM, since its value might be used
"   in a shell call

	" use $CREAM if present
	if exists("$CREAM")

		" accept as is

	" use $VIMINIT fragment if present
	elseif exists("$VIMINIT")

		" set $CREAM to a subdirectory below *this* file
		" remove initial 'source '
		let $CREAM = strpart($VIMINIT, 7)
		" if first 7 chars don't equal "source ", quit
		if strpart($VIMINIT, 0, 7) !=? "source "
			echo "---------------------------------------------------------------------"
			echo " WARNING! First 7 chars of $VIMINIT isn't \"source \""
			echo "   $VIMINIT = " . $VIMINIT
			echo "   $CREAM   = " . $CREAM
			echo "---------------------------------------------------------------------"
			let $CREAM = ""
			return -1
		endif
		" expand full path, minus filename head
		"let $CREAM = fnamemodify(expand($VIMINIT), ":p:h")
		let $CREAM = fnamemodify(expand($CREAM), ":p:h")
		" add cream sub-directory
		if     filewritable($CREAM . "/cream") == 2
			let $CREAM = $CREAM . "/cream/"
		elseif filewritable($CREAM . "/.cream") == 2
			let $CREAM = $CREAM . "/.cream/"
		else
			" error?
		endif

	" defaults
	else

		" convert all backslashes to forward slashes on Windows
		if has("win32")

			let $CREAM = $VIMRUNTIME . "/cream/"
			" get rid of path spaces
			if v:version >= 602
				let $CREAM = fnamemodify($CREAM, ":8")
			endif
			" change backslashes to slashes
			let $CREAM = substitute($CREAM, '\', '/', "g")

			""*** DEBUG: fallback
			"if filewritable($CREAM) != 2
			"    let $CREAM = $VIMRUNTIME . "/cream/"
			"endif
			""***

		else
			let $CREAM = $VIMRUNTIME . "/cream/"
		endif

	endif

	" return error if $CREAM doesn't exist
	if !exists("$CREAM")
		return -1
	endif

endfunction

" Cream_init_userdir()    ~/.cream {{{1
function! Cream_init_userdir()
" Set g:cream_user by finding or creating a location for user files.

	" environment var
	if exists("$CREAM_USER")
	\&& filewritable($CREAM_USER) == 2

		let g:cream_user = $CREAM_USER

	" Windows
	elseif has("win32")

		" initial find
		" Note: Vim always discovers $HOME, even on a Win95 system
		" that doesn't have one declared!!
		if exists("$HOME")
			" fully expand (WinXP drops drive letter)
			let mydir = fnamemodify($HOME, ":p")
		else
			" fallback
			let mydir = fnamemodify($CREAM, ":p")
		endif

		" simplify if possible
		if v:version >= 602
			let mydir = fnamemodify(mydir, ":8")
		endif

		" remove trailing slash (such as Win95 "C:\")
		let mydir = substitute(mydir, '\(\\\|/\)$', '', 'g')

		" add .cream/
		let mydir = mydir . '/.cream/'

		" if directory doesn't exist, try to make it
		call Cream_checkdir(mydir)

		" use if directory, writable
		if filewritable(mydir) == 2

			" convert backslashes to slashes
			let g:cream_user = escape(substitute(mydir, '\', '/', 'g'), ' \')

			" escape any spaces or backslashes remaining
			let g:cream_user = escape(g:cream_user, ' \')

		endif

	" other (Linux, Unix) (OSX?)
	elseif !has("win32")

		let mydir = $HOME . "/.cream/"

		" if directory doesn't exist, try to make it
		call Cream_checkdir(mydir)

		" use if directory, writable
		if filewritable(mydir) == 2
			let g:cream_user = mydir
		endif

	endif

	" fail if g:cream_user not found
	if !exists("g:cream_user")
		return -1
	endif

endfunction

" Cream_init_viewdir()    ~/.cream/views {{{1
function! Cream_init_viewdir()
" file view retainage

	if exists("$CREAM_VIEW")
	\&& filewritable($CREAM_VIEW) == 2

		execute "set viewdir=" . escape($CREAM_VIEW . "/", ' \')

	" default
	else

		" (remember, g:cream_user is simplified and escaped)
		let mydir = g:cream_user . "views"

		" if directory doesn't exist, try to make it
		call Cream_checkdir(mydir)

		if filewritable(mydir) == 2
			" we set a script global, only so viminfo (following) can
			" use it
			let s:cream_views = mydir
			execute "set viewdir=" . mydir
		else
			" failure
			return -1
		endif

	endif

endfunction

" Cream_init_viminfo()    ~/.cream/views {{{1
function! Cream_init_viminfo()
" setting/history/etc. file location

	" execute statement (everything but path)
	" \"100 escaped twice: once for itself, once for the quote
	let myviminfo = "set viminfo='500,\\\"100,%,!,h,n"

	" $CREAM_VIEW
	if exists("$CREAM_VIEW")
	\&& filewritable($CREAM_VIEW) == 2

		execute myviminfo . escape($CREAM_VIEW, ' \') . "/.viminfo"

	" default
	else

		execute myviminfo . s:cream_views . "/.viminfo"

	endif

	" must exist (directory has already been tested and is writable)

endfunction

" Cream_init_spelldicts() ~/.cream/spelldicts {{{1
function! Cream_init_spelldicts()
" backup file location

	" (remember, g:cream_user is simplified and escaped)
	let mydir = g:cream_user . "spelldicts"

	" if directory doesn't exist, try to make it
	call Cream_checkdir(mydir)

	if filewritable(mydir) != 2
		return -1
	endif

endfunction

" Cream_init_backupdir()  ~/.cream/tmp {{{1
function! Cream_init_backupdir()
" backup file location

	let prior = ""

	" $CREAM_BAK (environmental variable)
	if exists("$CREAM_BAK")
	\&& filewritable($CREAM_BAK) == 2

		let prior = $CREAM_BAK

	" default
	else

		" (remember, g:cream_user is simplified and escaped)
		let mydir = g:cream_user . "tmp"

		" if directory doesn't exist, try to make it
		call Cream_checkdir(mydir)

		if filewritable(mydir) != 2
			return -1
		else
			let prior = mydir
		endif

	endif

	" append comma to default if non-empty
	if prior != ""
		let prior = prior . ","
	endif

	" set
	execute "set backupdir=" . prior . "./bak,."

endfunction

" Cream_init_directory()  ./  (swap files) {{{1
function! Cream_init_directory()
" swap file location
" (Always with file to avoid overwritten by second user)

	if exists("$CREAM_SWP")
	\&& filewritable($CREAM_SWP) == 2
		" use variable
		execute "set directory=" . $CREAM_SWP . ",."
	else
		execute "set directory=."
	endif

endfunction

" 1}}}
" loader
" Cream() {{{1
function! Cream()
" load the project
" * return 1 on load, -1 on fatal error, 2 on terminal abort

""*** DEBUG:
"let $CREAM = "C:/PROGRA~1/VIM/VIM62/CREAM/"
"execute "set viminfo='500,\\\"100,%,!,h,nC:/PROGRA~1/VIM/VIM62/CREAM/views/.viminfo"
"set backupdir=C:/PROGRA~1/VIM/VIM62/CREAM/tmp,./bak,.
"set directory=.
"set viewdir=C:/PROGRA~1/VIM/VIM62/CREAM/views
"
"let g:cream_init = 1
""***

	" initialize (once--note behave module may try again ;)
	if !exists("g:cream_init")

		" $CREAM
		let init = Cream_init()
		if init == -1
			echo "\n Error: Unable to find Cream installation.\n"
			return -1
		endif

		"" vi behavior should stop here (but still can't read globals)
		"if exists("g:CREAM_BEHAVE")
		"    if g:CREAM_BEHAVE ==? "vi"
		"        return 1
		"    endif
		"endif

		" g:cream_user
		let init = Cream_init_userdir()
		if init == -1
			echo "\n Error: Unable to find a location for user files.\n"
			return -1
		endif

		" &viewdir
		let init = Cream_init_viewdir()
		if init == -1
			echo "\n Error: Unable to find a location for view files.\n"
			return -1
		endif

		" spelldicts
		let init = Cream_init_spelldicts()
		if init == -1
			echo "\n Error: Unable to find a location for spell dictionaries.\n"
			return -1
		endif

		" these are automatic
		call Cream_init_viminfo()
		call Cream_init_backupdir()
		call Cream_init_directory()
		let g:cream_init = 1

	endif

	""*** BROKEN: vim behavior should stop here, but still can't read
	""            globals so it doesn't.
	"if exists("g:CREAM_BEHAVE")
	"    if g:CREAM_BEHAVE ==? "vim"
	"        return 1
	"    endif
	"endif
	""***

""*** DEBUG:
"
"" as loading...
"echo "---------------------------------------------------------------------"
"echo " DEBUG: "
"echo "   $VIMINIT    = " . $VIMINIT
"echo "   $CREAM      = " . $CREAM
"echo "   &viewdir    = " . &viewdir
"echo "   &viminfo    = " . &viminfo
"echo "   &backupdir  = " . &backupdir
"echo "   &directory  = " . &directory
"echo "---------------------------------------------------------------------"
"
"" one line paste-in
"echo "\n $CREAM = " . $CREAM . "\n &viewdir = " . &viewdir . "\n &viminfo = " . &viminfo . "\n &backupdir = " . &backupdir . "\n &directory = " . &directory . "\n"
"
""***

	" load the loader
	if filereadable($CREAM . "cream.vim") > 0
		execute "source " . $CREAM . "cream.vim"
	else
		echo "\n Error: Unable to find Cream loader.\n"
		return -1
	endif

	return 1

endfunction
call Cream()

" 1}}}
" vim:filetype=vim:foldmethod=marker
