grimoire support files
[grimoire-arcane.git] / FUNCTIONS
blobd0d56c25db71607e100dc3c7ccce7c13a2d697d9
1 . $GRIMOIRE/libaccount
2 . $GRIMOIRE/libgcc
4 #---------------------------------------------------------------------
5 ## Replaces sorcerys default_build with a custom version using
6 ## the invoke_gcc function from libgcc
7 #---------------------------------------------------------------------
8 function default_build () {
9   invoke_gcc  &&
10   real_default_build
13 #---------------------------------------------------------------------
14 ## @return 0 if glibc was compiled with NPTL
15 ## @return 1 otherwise
17 ## Detects if the current glibc includes NPTL support.
19 #---------------------------------------------------------------------
20 function glibc_is_nptl ()
22   if getconf GNU_LIBPTHREAD_VERSION | grep -q NPTL; then
23     return 0
24   fi
25   return 1
28 #---------------------------------------------------------------------
29 # Runs update-desktop-database if it is installed
30 #---------------------------------------------------------------------
31 function update_desktop_database ()
33   if test -x /usr/bin/update-desktop-database; then
34     message "${MESSAGE_COLOR}Updating application mime type database.${DEFAULT_COLOR}"
35     /usr/bin/update-desktop-database
36   fi
39 #---------------------------------------------------------------------
40 ## this function installs the *.desktop, start* files and qingy links
41 ## this function is intended to be used by spells for windowmanagers
42 #---------------------------------------------------------------------
43 function install_wmfiles ()
45   local wm_desktopfile_dir="${INSTALL_ROOT}/usr/share/xsessions"
46   local wm_startwm_dir="${INSTALL_ROOT}/usr/bin"
48   # install the start* file for the windowmanager if it's not
49   # installed already
50   if [ -f ${SCRIPT_DIRECTORY}/start${SPELL} ] ; then
51     if ! [ -e ${wm_startwm}/start${SPELL} ] ; then
52       install  -m  755  ${SCRIPT_DIRECTORY}/start${SPELL}     \
53                         ${wm_startwm_dir}
54     fi
55   fi
57   # making sure the destination directory exists
58   if ! [ -d  ${wm_desktopfile_dir} ] ; then
59       mkdir  ${wm_desktopfile_dir}
60   fi                                                        &&
62   # install the windowmanagers desktop file
63   if [ -f ${SCRIPT_DIRECTORY}/${SPELL}.desktop ] ; then
64     if ! [ -e ${wm_desktopfile_dir}/${SPELL}.desktop ]; then
65       install  -m  755  ${SCRIPT_DIRECTORY}/${SPELL}.desktop   \
66                         ${wm_desktopfile_dir}
67     fi
68   fi
70   if ( spell_ok qingy ); then
71     if [ -x ${wm_startwm_dir}/start${SPELL} ] ; then
72       if  [  !  -d  ${INSTALL_ROOT}/etc/qingy/xsessions  ];  then
73         install  -d  -m  755  ${INSTALL_ROOT}/etc/qingy/xsessions
74       fi                                                 &&
75       ln -s ${wm_startwm_dir}/start${SPELL}              \
76             ${INSTALL_ROOT}/etc/qingy/xsessions/${SPELL}
77     fi
78   fi
81 #---------------------------------------------------------------------
82 ## Sets the current script to run only one make job
83 #---------------------------------------------------------------------
85 function make_single ()
87   JOBS_PER_HOST=0  &&
88      MAKE_NJOBS=1
91 #---------------------------------------------------------------------
92 ## Re-enables the normal Sorcery make (cancels single_make)
93 #---------------------------------------------------------------------
95 function make_normal ()
97   source $COMPILE_CONFIG
100 #---------------------------------------------------------------------
101 ## @param shellname
102 ## @param full path to shell
104 ## Adds a shell to /etc/shells and optionally to qingy's sessions
105 #---------------------------------------------------------------------
106 function install_shell ()
108   local qingy_session_dir="${INSTALL_ROOT}/etc/qingy/sessions"
110   if [ -z "$1" ] ; then
111     message "${PROBLEM_COLOR}no shell name specified, aborting${DEFAULT_COLOR}"
112     return 1
113   fi &&
114   if [ ! -e "$2" ] ; then
115     message "${PROBLEM_COLOR} $2 isn't executable, no use adding it as a shell${DEFAULT_COLOR}"
116     return 1
117   fi &&
119   #installing shell into /etc/shells
120   message "installing $1 into /etc/shells"
121   if ! ( grep -q "^${2}$" ${INSTALL_ROOT}/etc/shells ) ; then
122     echo "$2" >> ${INSTALL_ROOT}/etc/shells
123   fi || return 1
125   #installing shell into qingy's session dir if it's installed
126   if ( spell_ok qingy ) && ! [ -f ${qingy_session_dir}/$1 ] ; then
127     message "installing $1 into $qingy_session_dir"
128     echo "$2" > ${qingy_session_dir}/$1 &&
129     chmod 0755 ${qingy_session_dir}/$1
130   fi
133 #---------------------------------------------------------------------
134 ## @param shellname
135 ## @param full path to shell
137 ## Removes a shell from /etc/shells and optionally from qingy's sessions
138 #---------------------------------------------------------------------
139 function remove_shell ()
141   local qingy_session_dir="${INSTALL_ROOT}/etc/qingy/sessions"
143   if [ -z "$1" ] ; then
144     message "${PROBLEM_COLOR}no shell name specified, aborting${DEFAULT_COLOR}"
145     return 1
146   fi &&
147   if [ -z "$2" ] ; then
148     message "${PROBLEM_COLOR}no shell path specified, aborting${DEFAULT_COLOR}"
149     return 1
150   fi &&
152   #removing shell from /etc/shells
153   if  [  -f  /etc/shells  ]  ;  then
154     sedit "\:^$2$:d"  /etc/shells
155   fi
157   # and optionally from qingy's session dir
158   if [ -f ${qingy_session_dir}/$1 ] ; then
159     message "removing $1 from $qingy_session_dir"
160     rm -f ${qingy_session_dir}/$1
161   fi
165 #---------------------------------------------------------------------
166 ## replacement for config_query_string to work around a short timeout
167 ## when entering long strings (hostnames, organisation names)
168 #---------------------------------------------------------------------
169 function config_query_long_string() {
170     local ANSWER
171     local DELAY=5 
172     if config_get_option "$1" ANSWER; then
173         # option allready answered in config
174         echo -e "[[ ${QUERY_COLOR}$2${DEFAULT} -> '${QUERY_COLOR}$ANSWER${DEFAULT}' ]]"
175     else
176         query_string ANSWER "$2" "$3"
177         read -t $DELAY -n 1 ANSWER_first
178         if [[ $ANSWER_first ]] ; then 
179           read ANSWER_rest
180         fi
181         ANSWER="${ANSWER_first}${ANSWER_rest}"
182         config_set_option "$1" "$ANSWER"
183     fi
184     return 0
187 #---------------------------------------------------------------------
188 ## @param Directory with files we might want to install
189 ## @param Destination directory (optional)
191 ## Installs files to WWW_DEST ($INSTALL_ROOT/usr/share/www/$SPELL) and
192 ## sets up proper www-data permissions
193 ## Optionally the second parameter will override WWW_DATA (and make
194 ## sure INSTALL_ROOT exists only once in the WWW_DATA)
195 #---------------------------------------------------------------------
196 function  install_www_files ()
198   if  [  !  -z  $2  ];  then
199     #
200     # Just in case the destination already includes INSTALL_ROOT, remove it
201     #
202     local  WWW_DEST="$INSTALL_ROOT/${2/\$INSTALL_ROOT/}"
203   else
204     local  WWW_DEST="$INSTALL_ROOT/usr/share/www/$SPELL"
205   fi  &&
207   #
208   # Make sure WWW_DEST exists
209   #
210   if  [  !  -d  "$WWW_DEST"  ];  then
211     if  [  !  -d  "${WWW_DEST/\/$SPELL/}"  ];  then
212       mkdir  -p  "${WWW_DEST/\/$SPELL/}"                   &&
213       chmod  0755                 "${WWW_DEST/\/$SPELL/}"  &&
214       chown  www-data:www-data    "${WWW_DEST/\/$SPELL/}"
215     fi  &&
217     mkdir  -p  "$WWW_DEST"                   &&
218     chmod  0755                 "$WWW_DEST"  &&
219     chown  www-data:www-data    "$WWW_DEST"
220   fi  &&
222   # find $@ | while read file; do install ... $file ...; done
223   # find  $1  -type  -f  |  while  read  file;  do
224   for  www_file  in  `find  $1  -type  f`;  do
225     if  install_config_file  "$www_file"   "$WWW_DEST/$www_file";  then
226       chmod  0644                 "$WWW_DEST/$www_file"  &&
227       chown  www-data:www-data    "$WWW_DEST/$www_file"
228     fi
229   done
230   #
231   # Warning message about new location for www files
232   # Remove around 2005-08-02 (one month in test, another in stable, supposedly)
233   #
234   message  "${MESSAGE_COLOR}Your web files have been moved out of the"        \
235            "APACHE specific locations (apache2/htdocs, httpd/htdocs, etc.)"   \
236            "and into a shared, non-DocumentRoot $INSTALL_ROOT/usr/share/www"  \
237            "directory. You can modify your web server to point there for"     \
238            "these files now.${DEFAULT_COLOR}"
241 #---------------------------------------------------------------------
242 ## Removes traces of NSPR and NSS from Mozilla-based software
243 ## TODO move patch to a central place
244 #---------------------------------------------------------------------
245 function mozilla_remove_nspr_nss() {
246   message 'Checking Mozilla source code...' &&
247   [[ "$(basename $(pwd))" == mozilla ]] &&
248   message 'Checking the spell...' &&
249   [[ -f $SCRIPT_DIRECTORY/security_manager_makefile.diff ]] &&
250   patch -p1 < $SCRIPT_DIRECTORY/security_manager_makefile.diff &&
251   rm -fr dbm nsprpub security/nss &&
252   # exclude DBM for top-level modules and don't build it
253   sedit 's@\<dbm\>@@g' Makefile.in &&
254   sedit 's@\<dbm\>@@g' build/unix/modules.mk &&
255   # don't define NSS libraries as dependencies and don't look for them in the tree
256   sedit 's@$(DIST)/lib/$(LIB_PREFIX)\(crmf\|dbm\|nss3\|softokn3\|smime3\|ssl3\)\.$(LIB_SUFFIX)@-l\1@g' config/config.mk &&
257   sedit 's@NSS_DEP_LIBS\s*=@__undefine_\0@g' config/config.mk &&
258   # align the makefile-s
259   find -name Makefile.in | while read __MAKEFILE; do
260     # use system NSPR's and NSS's headers
261     # option `--with-system-nspr' doesn't do it everywhere
262     sedit 's@-I\S*\(nss\|nspr\)\>@-I/usr/include/\1@g' $__MAKEFILE
263   done
266 #-------------------------------------------------------------------------
267 ## Returns the kernel version checks linux then linux-new (for now) and
268 ## then uname for the kernel version
269 #-------------------------------------------------------------------------
270 function get_kernel_version()
272         local KVER=$(installed_version linux)
273         if [[ $KVER ]] ; then
274                 echo $KVER
275         else
276                 KVER=$(installed_version linux-new)
277                 if [[ $KVER ]] ; then
278                         echo $KVER
279                 else
280                         KVER=$(uname -r)
281                         echo $KVER
282                 fi
283         fi
286 #---------------------------------------------------------------------------
287 ## Invokes the unamechange spell any spell that uses this should depend on
288 ## unamechange otherwise this function does nothing.
289 ## to use this function simply call then change the variables listed ro have
290 ## any call to uname return that value
291 #--------------------------------------------------------------------------
292 function invoke_uname_change()
294         if [[ $(installed_version unamechange) ]] ; then
295                 export UNAME_CHANGE_SYSNAME=$(uname -s)
296                 export UNAME_CHANGE_NODENAME=$(uname -n)
297                 export UNAME_CHANGE_RELEASE=$(uname -r)
298                 export UNAME_CHANGE_VERSION=$(uname -v)
299                 export UNAME_CHANGE_MACHINE=$(uname -m)
300                 export UNAME_CHANGE_DOMAINNAME=$(uname -o)
301         
302                 export LD_PRELOAD="${LD_PRELOAD} /usr/lib/unamechange.so"
303         fi
306 #-------------------------------------------------------------------------
307 ## Returns the state of the system back to normal after calling 
308 ## invoke_uname_change unset's all environmental vars and returns
309 ## LD_PRELOAD back to normal
310 #-------------------------------------------------------------------------
311 function devoke_uname_change()
313         if [[ $(installed_version unamechange) ]] ; then
314                 unset UNAME_CHANGE_SYSNAME
315                 unset UNAME_CHANGE_NODENAME
316                 unset UNAME_CHANGE_RELEASE
317                 unset UNAME_CHANGE_VERSION
318                 unset UNAME_CHANGE_MACHINE
319                 unset UNAME_CHANGE_DOMAINNAME
320         
321                 export LD_PRELOAD="${LD_PRELOAD/\/usr\/lib\/unamechange.so/}"
322         fi
325 #-----------------------------------------------------------------------
326 ## Get the running kernel config status of a some part of the kernel
327 ## given by $1. Used for spells that don't have linux triggers
329 ## $1 string Config variable to look for
330 #-----------------------------------------------------------------------
331 function get_running_kernel_config()
333         local KVER
334         # use proc interface because even inside a uname change invoke this
335         # still returns the version of the running kernel
336         if [ -f /proc/sys/kernel/osrelease ] ; then
337                 KVER=$(cat /proc/sys/kernel/osrelease)
338         else
339                 # apparently you don't have proc mount
340                 KVER=$(uname -r)
341         fi
342         if [ -f /proc/config.gz ] ; then
343                 echo $(zgrep "^$1=" /proc/config.gz | awk -F= '{ print $2 }')
344         elif [ -f /boot/config-$KVER ] ; then
345                 echo $(grep "^$1=" /boot/config-$KVER | awk -F= '{ print $2 }')
346         elif [ -f /usr/src/linux-$KVER/.config ] ; then
347                 echo $(grep "^$1=" /usr/src/linux-$KVER/.config | awk -F= '{ print $2 }')
348         else
349                 echo "-1"
350         fi
353 #-------------------------------------------------------------------------
354 ## Get the config status of some part of the kernel sorcery says is 
355 ## installed. Used by spells that have linux triggers.
357 ## $1 string Config var to look for
358 #-------------------------------------------------------------------------
359 function get_sorcery_kernel_config()
361         local KVER=$(get_kernel_version)
362         if [ -f /boot/config-$KVER ] ; then
363                 echo $(grep "^$1=" /boot/config-$KVER | awk -F= '{ print $2 }')
364         elif [ -f /usr/src/linux-$KVER/.config ] ; then
365                 echo $(grep "^$1=" /usr/src/linux-$KVER/.config | awk -F= '{ print $2 }')
366         else
367                 echo "-1"
368         fi
372 #-------------------------------------------------------------------------
373 ## Compatibility code for gracefully failing if the user uses an older
374 ## version of sorcery with a spell that calls unpack_file.
375 #-------------------------------------------------------------------------
376 declare -f unpack_file &> /dev/null ||
377 function unpack_file() {
378   message "This spell uses a function only available in sorcery 1.12.2 or newer, please update."
379   return 1
382 #-------------------------------------------------------------------------
383 ## Default build for Python spell.
384 #-------------------------------------------------------------------------
385 function default_build_python() {
386   python setup.py build
389 #-------------------------------------------------------------------------
390 ## Default install for Python spell.
391 ## INSTALL_ROOT is used here only if not empty, because some spells can't
392 ## install with "--root '/'" option.
393 #-------------------------------------------------------------------------
394 function default_install_python() {
395   python setup.py install ${INSTALL_ROOT:+--root "$INSTALL_ROOT/"}