adde grimoire support files
[grimoire-witchcraft.git] / FUNCTIONS
blob324d77f37632a450968a27619339f35161675d46
1 . $GRIMOIRE/libaccount
2 . $GRIMOIRE/libgcc
3 . $GRIMOIRE/config_query_multi.function
5 #---------------------------------------------------------------------
6 ## Replaces sorcerys default_build with a custom version using
7 ## the invoke_gcc function from libgcc
8 #---------------------------------------------------------------------
9 function default_pre_build () {
10   real_default_pre_build                 &&
11   chmod -R u+w $SOURCE_DIRECTORY/        &&
12   chown -R root:root $SOURCE_DIRECTORY/  &&
13   invoke_gcc
16 #---------------------------------------------------------------------
17 ## @return 0 if glibc was compiled with NPTL
18 ## @return 1 otherwise
20 ## Detects if the current glibc includes NPTL support.
22 #---------------------------------------------------------------------
23 function glibc_is_nptl ()
25   if getconf GNU_LIBPTHREAD_VERSION | grep -q NPTL; then
26     return 0
27   fi
28   return 1
31 #---------------------------------------------------------------------
32 # Runs update-desktop-database if it is installed
33 #---------------------------------------------------------------------
34 function update_desktop_database ()
36   if test -x /usr/bin/update-desktop-database; then
37     message "${MESSAGE_COLOR}Updating application mime type database.${DEFAULT_COLOR}"
38     /usr/bin/update-desktop-database
39   fi
42 #---------------------------------------------------------------------
43 ## this function installs the *.desktop, start* files and qingy links
44 ## this function is intended to be used by spells for windowmanagers
45 #---------------------------------------------------------------------
46 function install_wmfiles ()
48   local wm_desktopfile_dir="${INSTALL_ROOT}/usr/share/xsessions"
49   local wm_startwm_dir="${INSTALL_ROOT}/usr/bin"
51   # install the start* file for the windowmanager if it's not
52   # installed already
53   if [ -f ${SCRIPT_DIRECTORY}/start${SPELL} ] ; then
54     if ! [ -e ${wm_startwm}/start${SPELL} ] ; then
55       install  -m  755  ${SCRIPT_DIRECTORY}/start${SPELL}     \
56                         ${wm_startwm_dir}
57     fi
58   fi
60   # making sure the destination directory exists
61   if ! [ -d  ${wm_desktopfile_dir} ] ; then
62       mkdir  ${wm_desktopfile_dir}
63   fi                                                        &&
65   # install the windowmanagers desktop file
66   if [ -f ${SCRIPT_DIRECTORY}/${SPELL}.desktop ] ; then
67     if ! [ -e ${wm_desktopfile_dir}/${SPELL}.desktop ]; then
68       install  -m  755  ${SCRIPT_DIRECTORY}/${SPELL}.desktop   \
69                         ${wm_desktopfile_dir}
70     fi
71   fi
73   if ( spell_ok qingy ); then
74     if [ -x ${wm_startwm_dir}/start${SPELL} ] ; then
75       if  [  !  -d  ${INSTALL_ROOT}/etc/qingy/xsessions  ];  then
76         install  -d  -m  755  ${INSTALL_ROOT}/etc/qingy/xsessions
77       fi                                                 &&
78       ln -s ${wm_startwm_dir}/start${SPELL}              \
79             ${INSTALL_ROOT}/etc/qingy/xsessions/${SPELL}
80     fi
81   fi
84 #---------------------------------------------------------------------
85 ## Sets the current script to run only one make job
86 #---------------------------------------------------------------------
88 function make_single ()
90   JOBS_PER_HOST=0  &&
91      MAKE_NJOBS=1
94 #---------------------------------------------------------------------
95 ## Re-enables the normal Sorcery make (cancels single_make)
96 #---------------------------------------------------------------------
98 function make_normal ()
100   source $COMPILE_CONFIG
103 #---------------------------------------------------------------------
104 ## @param shellname
105 ## @param full path to shell
107 ## Adds a shell to /etc/shells and optionally to qingy's sessions
108 #---------------------------------------------------------------------
109 function install_shell ()
111   local qingy_session_dir="${INSTALL_ROOT}/etc/qingy/sessions"
113   if [ -z "$1" ] ; then
114     message "${PROBLEM_COLOR}no shell name specified, aborting${DEFAULT_COLOR}"
115     return 1
116   fi &&
117   if [ ! -e "$2" ] ; then
118     message "${PROBLEM_COLOR} $2 isn't executable, no use adding it as a shell${DEFAULT_COLOR}"
119     return 1
120   fi &&
122   #installing shell into /etc/shells
123   message "installing $1 into /etc/shells"
124   if ! ( grep -q "^${2}$" ${INSTALL_ROOT}/etc/shells ) ; then
125     echo "$2" >> ${INSTALL_ROOT}/etc/shells
126   fi || return 1
128   #installing shell into qingy's session dir if it's installed
129   if ( spell_ok qingy ) && ! [ -f ${qingy_session_dir}/$1 ] ; then
130     message "installing $1 into $qingy_session_dir"
131     echo "$2" > ${qingy_session_dir}/$1 &&
132     chmod 0755 ${qingy_session_dir}/$1
133   fi
136 #---------------------------------------------------------------------
137 ## @param shellname
138 ## @param full path to shell
140 ## Removes a shell from /etc/shells and optionally from qingy's sessions
141 #---------------------------------------------------------------------
142 function remove_shell ()
144   local qingy_session_dir="${INSTALL_ROOT}/etc/qingy/sessions"
146   if [ -z "$1" ] ; then
147     message "${PROBLEM_COLOR}no shell name specified, aborting${DEFAULT_COLOR}"
148     return 1
149   fi &&
150   if [ -z "$2" ] ; then
151     message "${PROBLEM_COLOR}no shell path specified, aborting${DEFAULT_COLOR}"
152     return 1
153   fi &&
155   #removing shell from /etc/shells
156   if  [  -f  /etc/shells  ]  ;  then
157     sedit "\:^$2$:d"  /etc/shells
158   fi
160   # and optionally from qingy's session dir
161   if [ -f ${qingy_session_dir}/$1 ] ; then
162     message "removing $1 from $qingy_session_dir"
163     rm -f ${qingy_session_dir}/$1
164   fi
168 #---------------------------------------------------------------------
169 ## replacement for config_query_string to work around a short timeout
170 ## when entering long strings (hostnames, organisation names)
171 #---------------------------------------------------------------------
172 function config_query_long_string() {
173     local ANSWER
174     local DELAY=5 
175     if config_get_option "$1" ANSWER; then
176         # option allready answered in config
177         echo -e "[[ ${QUERY_COLOR}$2${DEFAULT} -> '${QUERY_COLOR}$ANSWER${DEFAULT}' ]]"
178     else
179         query_string ANSWER "$2" "$3"
180         read -t $DELAY -n 1 ANSWER_first
181         if [[ $ANSWER_first ]] ; then 
182           read ANSWER_rest
183         fi
184         ANSWER="${ANSWER_first}${ANSWER_rest}"
185         config_set_option "$1" "$ANSWER"
186     fi
187     return 0
190 #---------------------------------------------------------------------
191 ## @param Directory with files we might want to install
192 ## @param Destination directory (optional)
194 ## Installs files to WWW_DEST ($INSTALL_ROOT/usr/share/www/$SPELL) and
195 ## sets up proper www-data permissions
196 ## Optionally the second parameter will override WWW_DATA (and make
197 ## sure INSTALL_ROOT exists only once in the WWW_DATA)
198 #---------------------------------------------------------------------
199 function  install_www_files ()
201   if  [  !  -z  $2  ];  then
202     #
203     # Just in case the destination already includes INSTALL_ROOT, remove it
204     #
205     local  WWW_DEST="$INSTALL_ROOT/${2/\$INSTALL_ROOT/}"
206   else
207     local  WWW_DEST="$INSTALL_ROOT/usr/share/www/$SPELL"
208   fi  &&
210   #
211   # Make sure the www-data user exists
212   # Not sure if this will work :/
213   create_account www-data
215   #
216   # Make sure WWW_DEST exists
217   #
218   if  [  !  -d  "$WWW_DEST"  ];  then
219     if  [  !  -d  "${WWW_DEST/\/$SPELL/}"  ];  then
220       mkdir  -p  "${WWW_DEST/\/$SPELL/}"                   &&
221       chmod  0755                 "${WWW_DEST/\/$SPELL/}"  &&
222       chown  www-data:www-data    "${WWW_DEST/\/$SPELL/}"
223     fi  &&
225     mkdir  -p  "$WWW_DEST"                   &&
226     chmod  0755                 "$WWW_DEST"  &&
227     chown  www-data:www-data    "$WWW_DEST"
228   fi  &&
230   # find $@ | while read file; do install ... $file ...; done
231   # find  $1  -type  -f  |  while  read  file;  do
232   for  www_file  in  `find  $1  -type  f`;  do
233     if  install_config_file  "$www_file"   "$WWW_DEST/$www_file";  then
234       chmod  0644                 "$WWW_DEST/$www_file"  &&
235       chown  www-data:www-data    "$WWW_DEST/$www_file"
236     fi
237   done
238   #
239   # Warning message about new location for www files
240   # Remove around 2005-08-02 (one month in test, another in stable, supposedly)
241   #
242   message  "${MESSAGE_COLOR}Your web files have been moved out of the"        \
243            "APACHE specific locations (apache2/htdocs, httpd/htdocs, etc.)"   \
244            "and into a shared, non-DocumentRoot $INSTALL_ROOT/usr/share/www"  \
245            "directory. You can modify your web server to point there for"     \
246            "these files now.${DEFAULT_COLOR}"
249 #---------------------------------------------------------------------
250 ## Removes traces of NSPR and NSS from Mozilla-based software
251 ## TODO move patch to a central place
252 #---------------------------------------------------------------------
253 function mozilla_remove_nspr_nss() {
254   message 'Checking Mozilla source code...' &&
255   [[ "$(basename $(pwd))" == mozilla ]] &&
256   message 'Checking the spell...' &&
257   [[ -f $SCRIPT_DIRECTORY/security_manager_makefile.diff ]] &&
258   patch -p1 < $SCRIPT_DIRECTORY/security_manager_makefile.diff &&
259   rm -fr dbm nsprpub security/nss &&
260   # exclude DBM for top-level modules and don't build it
261   sedit 's@\<dbm\>@@g' Makefile.in &&
262   sedit 's@\<dbm\>@@g' build/unix/modules.mk &&
263   # don't define NSS libraries as dependencies and don't look for them in the tree
264   sedit 's@$(DIST)/lib/$(LIB_PREFIX)\(crmf\|dbm\|nss3\|softokn3\|smime3\|ssl3\)\.$(LIB_SUFFIX)@-l\1@g' config/config.mk &&
265   sedit 's@NSS_DEP_LIBS\s*=@__undefine_\0@g' config/config.mk &&
266   # align the makefile-s
267   find -name Makefile.in | while read __MAKEFILE; do
268     # use system NSPR's and NSS's headers
269     # option `--with-system-nspr' doesn't do it everywhere
270     sedit 's@-I\S*\(nss\|nspr\)\>@-I/usr/include/\1@g' $__MAKEFILE
271   done
274 #-------------------------------------------------------------------------
275 ## Returns the kernel version checks linux then linux-new (for now) and
276 ## then uname for the kernel version
277 #-------------------------------------------------------------------------
278 function get_kernel_version()
280         local KVER=$(installed_version linux)
281         if [[ $KVER ]] ; then
282                 echo $KVER
283         else
284                 KVER=$(installed_version linux-new)
285                 if [[ $KVER ]] ; then
286                         echo $KVER
287                 else
288                         KVER=$(uname -r)
289                         echo $KVER
290                 fi
291         fi
294 #---------------------------------------------------------------------------
295 ## Invokes the unamechange spell any spell that uses this should depend on
296 ## unamechange otherwise this function does nothing.
297 ## to use this function simply call then change the variables listed ro have
298 ## any call to uname return that value
299 #--------------------------------------------------------------------------
300 function invoke_uname_change()
302         if [[ $(installed_version unamechange) ]] ; then
303                 export UNAME_CHANGE_SYSNAME=$(uname -s)
304                 export UNAME_CHANGE_NODENAME=$(uname -n)
305                 export UNAME_CHANGE_RELEASE=$(uname -r)
306                 export UNAME_CHANGE_VERSION=$(uname -v)
307                 export UNAME_CHANGE_MACHINE=$(uname -m)
308                 export UNAME_CHANGE_DOMAINNAME=$(uname -o)
309         
310                 export LD_PRELOAD="${LD_PRELOAD} /usr/lib/unamechange.so"
311         fi
314 #-------------------------------------------------------------------------
315 ## Returns the state of the system back to normal after calling 
316 ## invoke_uname_change unset's all environmental vars and returns
317 ## LD_PRELOAD back to normal
318 #-------------------------------------------------------------------------
319 function devoke_uname_change()
321         if [[ $(installed_version unamechange) ]] ; then
322                 unset UNAME_CHANGE_SYSNAME
323                 unset UNAME_CHANGE_NODENAME
324                 unset UNAME_CHANGE_RELEASE
325                 unset UNAME_CHANGE_VERSION
326                 unset UNAME_CHANGE_MACHINE
327                 unset UNAME_CHANGE_DOMAINNAME
328         
329                 export LD_PRELOAD="${LD_PRELOAD/\/usr\/lib\/unamechange.so/}"
330         fi
333 #-----------------------------------------------------------------------
334 ## Get the running kernel config status of a some part of the kernel
335 ## given by $1. Used for spells that don't have linux triggers
337 ## $1 string Config variable to look for
338 #-----------------------------------------------------------------------
339 function get_running_kernel_config()
341         local KVER
342         # use proc interface because even inside a uname change invoke this
343         # still returns the version of the running kernel
344         if [ -f /proc/sys/kernel/osrelease ] ; then
345                 KVER=$(cat /proc/sys/kernel/osrelease)
346         else
347                 # apparently you don't have proc mount
348                 KVER=$(uname -r)
349         fi
350         if [ -f /proc/config.gz ] ; then
351                 echo $(zgrep "^$1=" /proc/config.gz | awk -F= '{ print $2 }')
352         elif [ -f /boot/config-$KVER ] ; then
353                 echo $(grep "^$1=" /boot/config-$KVER | awk -F= '{ print $2 }')
354         elif [ -f /usr/src/linux-$KVER/.config ] ; then
355                 echo $(grep "^$1=" /usr/src/linux-$KVER/.config | awk -F= '{ print $2 }')
356         else
357                 echo "-1"
358         fi
361 #-------------------------------------------------------------------------
362 ## Get the config status of some part of the kernel sorcery says is 
363 ## installed. Used by spells that have linux triggers.
365 ## $1 string Config var to look for
366 #-------------------------------------------------------------------------
367 function get_sorcery_kernel_config()
369         local KVER=$(get_kernel_version)
370         if [ -f /boot/config-$KVER ] ; then
371                 echo $(grep "^$1=" /boot/config-$KVER | awk -F= '{ print $2 }')
372         elif [ -f /usr/src/linux-$KVER/.config ] ; then
373                 echo $(grep "^$1=" /usr/src/linux-$KVER/.config | awk -F= '{ print $2 }')
374         else
375                 echo "-1"
376         fi
380 #-------------------------------------------------------------------------
381 ## Compatibility code for gracefully failing if the user uses an older
382 ## version of sorcery with a spell that calls unpack_file.
383 #-------------------------------------------------------------------------
384 declare -f unpack_file &> /dev/null ||
385 function unpack_file() {
386   message "This spell uses a function only available in sorcery 1.12.2 or newer, please update."
387   return 1
390 #-------------------------------------------------------------------------
391 ## Default build for Python spell.
392 #-------------------------------------------------------------------------
393 function default_build_python() {
394   python setup.py build
397 #-------------------------------------------------------------------------
398 ## Default install for Python spell.
399 ## INSTALL_ROOT is used here only if not empty, because some spells can't
400 ## install with "--root '/'" option.
401 #-------------------------------------------------------------------------
402 function default_install_python() {
403   python setup.py install ${INSTALL_ROOT:+--root "$INSTALL_ROOT/"}
407 #---
408 ## disable_pic
409 ## A function to smartly disable -DPIC and -fPIC flags
410 ## has one optional parameter: force which forces disable
411 #---
413 disable_pic() {
414 if [[ "$SMGL_COMPAT_ARCHS[1]" != "x86_64" ]] ||
415    [[ "$1" == "force" ]]
416 then
417 CFLAGS=${CFLAGS/-fPIC}                    &&
418 CFLAGS=${CFLAGS/-DPIC}                    &&
419 CXXFLAGS=${CXXFLAGS/-fPIC}                &&
420 CXXFLAGS=${CXXFLAGS/-DPIC}
424 #---
425 ## Function to check if we're on GNU/kFreeBSD system or not
426 #---
428 function is_gnu_kfbsd() {
429 if [[ "$(uname)" == "GNU/kFreeBSD" ]]; then
430    return 0
431 else
432    return 1