1#!/bin/sh
2# /etc/profile.d/dev-perso
3#
4# (c) 2006-2008, mmu_man
5#
6# project-specific environment handling:
7# - sources project-specific .profile,
8# - maintains project-specific .bash_history with bigger default size,
9# to avoid mixing command histories
10# - pushes "cvs up -d" or "svn up" or "p4 sync" as last history command
11# for quick access
12# - has a nice prompt
13# - lowers shell priority on BeOS to limit the effects of svn or jam on
14# the gui
15# - automatically completes project names on the "dev" function
16# from project subfolders containing a .profile
17#
18# This script should be sourced by bash, either from /etc/profile
19# (in /etc/profile.d/) or your own .bashrc or .profile
20# after exporting DEVROOT optionally to point to the projects folder.
21# 
22# setup:
23# - edit DRLIST below to include your own possible devroots
24# (ie. where you have subfolders for your own projects)
25# or export DEVROOT before sourcing dev-perso.
26# - optionally force EDITOR globally (for all projects)
27# - for each project create a .profile in the subfolder,
28# which might include commands like the following, but can remain empty.
29# usually it will just contain a "cd" to the source/trunk subfolder...
30#
31#	# force svn ssh user for this project
32#	export SVN_SSH='ssh -l myuser'
33#
34#	# force CVSROOT for this project
35#	export CVSROOT='...'
36#
37#	# change to the source tree
38#	cd trunk
39#	# ease use of cd
40#	export CDPATH=":$PWD"
41#
42# Now you just have to type:
43# dev h[TAB]
44# to complete to "dev haiku", [RET] and you'll get the history from the
45# last time you worked on it, and everything set up.
46
47
48# automagically find them on different machines...
49if [ -z "$DEVROOT" ]; then
50	DRLIST="$HOME/devel /Data /work /Volumes/Data/devel"
51	for d in $DRLIST; do
52		test -d "$d" && DEVROOT="$d" && break;
53	done
54fi
55export DEVROOT
56
57# svn sometimes forgets about vi and wants me to use nano...
58#export EDITOR=vim
59
60dev() {
61	if [ $# -lt 1 ]; then 
62		#ls $DEVROOT/*/.profile | sed 's,.*/\([^/]*\)/.profile,\1,'
63		for f in "$DEVROOT/"*; do test -e "$f/.profile" || continue; echo ${f##*/}; done
64		return 0
65	fi
66	if [ "x$1" = "x--help" ]; then
67		echo "setup project-specific development environment"
68		echo "usage: dev [-n] [project]"
69		echo "running without argument lists available projects"
70		echo "-n projname initializes a new project"
71		return 1
72	fi
73	if [ "x$1" = "x-n" -a -n "$2" ]; then
74		shift
75		mkdir "$DEVROOT/$1" && touch "$DEVROOT/$1/.profile"
76		# fallback
77	fi
78
79	export DEVPROJ="$1"
80	if [ ! -d "$DEVROOT/$1" ]; then
81		echo "invalid project name '$1'"
82		return 1
83	fi
84
85	# change to the project root folder
86	cd "$DEVROOT/$1"
87
88	# use a specific history file
89	export HISTFILE="$DEVROOT/$1/.bash_history"
90	# and bump up the history limits
91	export HISTSIZE=1000
92	export HISTFILESIZE=100000
93	export HISTCONTROL=ignoreboth
94	# and force loading the new histfile
95	history -r
96
97	# force default locale so that compiler errors and such are reported in english
98	#export LC_ALL=C.UTF-8
99
100	# set the prompt
101	# cf. http://tldp.org/HOWTO/Bash-Prompt-HOWTO/
102	NICEPS1='\[\033[1m\][\u@\h \w]\[\033[0m\]\$ '
103	case "$TERM" in
104	dumb|emacs)
105		# simpler prompt
106		export PS1='[\u@\h \w]\$ '
107		;;
108	linux)
109		export PS1="$NICEPS1"
110		;;
111	*)
112		# prompt: set window title to [project:folder] also
113		#export PS1='\[\033]0;['$1':\W]\a\]\[\033[1m\][\u@\h \w]\[\033[0m\]\$ '
114		#export PS1='\033]0;['$1':\W]\a\033[1m[\u@\h \w]\033[0m\$ '
115		export PS1="$NICEPS1"
116		export PROMPT_COMMAND='echo -en "\033]0;['$1':${PWD##*/}]\a"'
117		;;
118	esac
119
120	# lower priority so background builds don't slow the GUI too much
121	case "$OSTYPE" in
122	beos|haiku)
123		prio $$ 1
124		;;
125	darwin10.*)
126		renice 3 $$
127		;;
128	linux-*)
129		# linux doesn't really need it much
130		#renice 3 $$
131		;;
132	esac
133
134	# check for a TODO list
135	for f in TODO TODO.org todo todo.org; do
136		test -e "$f" && grep '^* ' "$f" | grep --color=always ' TODO ' | head -5 && break
137	done
138
139	DEVUPCMD=""
140
141	# source the specific profile file
142	# (which will likely cd to the checkout directory)
143	test -f .profile && . .profile
144
145	# if no editor defined, set one
146	test -z "$EDITOR" -a -z "$SVN_EDITOR" && export EDITOR=vim
147	
148	# make sure the update action is the first found in history.
149	test -z "$DEVUPCMD" -a -d .git && DEVUPCMD="git pull"
150	test -z "$DEVUPCMD" -a -d .svn && DEVUPCMD="svn up"
151	test -z "$DEVUPCMD" -a -d .bzr && DEVUPCMD="bzr update"
152	test -z "$DEVUPCMD" -a -d .hg && DEVUPCMD="hg pull"
153	test -z "$DEVUPCMD" -a -d CVS && DEVUPCMD="cvs up -d"
154	test -z "$DEVUPCMD" -a \( -f _FOSSIL_ -o -f .fslckout \) && DEVUPCMD="fossil update"
155	test -z "$DEVUPCMD" -a -n "$P4PORT" && DEVUPCMD="p4 sync"
156	test -n "$DEVUPCMD" && history -s "$DEVUPCMD"
157
158	# spice up terminal window title for git, add current branch name
159	test -d .git && PROMPT_COMMAND='echo -en "\033]0;['$DEVPROJ':`git branch 2>/dev/null | sed "/^[^*]/d;s/^\*\s//"`:${PWD##*/}]\a"'
160}
161
162complete -W complete -W "$(dev)" dev
163
164_devup_notify() {
165	echo "#### $*" >&2
166}
167
168devup() {
169	if [ $# -lt 1 ]; then 
170		#ls $DEVROOT/*/.profile | sed 's,.*/\([^/]*\)/.profile,\1,'
171		for f in "$DEVROOT/"*; do
172			test -e "$f/.profile" || continue
173			grep "^# AUTOUP$" "$f/.profile" > /dev/null 2>&1 || continue
174			p="${f##*/}"
175			_devup_notify "Updating $p..."
176			devup "$p" && _devup_notify "Updated $p: OK" || (_devup_notify "Updating $p: Error: $?"; read)
177		done
178		return 0
179	fi
180
181	# subshell!!!
182	(
183
184	export DEVPROJ="$1"
185	if [ ! -d "$DEVROOT/$1" ]; then
186		echo "invalid project name '$1'"
187		return 1
188	fi
189
190	# change to the project root folder
191	cd "$DEVROOT/$1"
192
193	# lower priority so background builds don't slow the GUI too much
194	case "$OSTYPE" in
195	beos|haiku)
196		prio $$ 1
197		;;
198	darwin10.*)
199		renice 3 $$
200		;;
201	linux-*)
202		# linux doesn't really need it much
203		#renice 3 $$
204		;;
205	esac
206
207	DEVUPCMD=""
208
209	# source the specific profile file
210	test -f .profile && . .profile
211
212	# make sure the update action is the first found in history.
213	if [ -z "$DEVUPCMD" ]; then
214		test -d .svn && DEVUPCMD="svn up"
215		test -d .bzr && DEVUPCMD="bzr update"
216		test -d .hg && DEVUPCMD="hg pull"
217		test -d .git && DEVUPCMD="git pull"
218		test -d CVS && DEVUPCMD="cvs up -d"
219		test -n "$P4PORT" && DEVUPCMD="p4 sync"
220	fi
221
222	test -n "$DEVUPCMD" || return 7
223
224	# run the update command...
225	eval "$DEVUPCMD"
226
227	return $?
228	)
229
230	return $?
231}
232
233complete -W complete -W "$(dev)" devup
234
235