1#!/bin/sh
2#
3# Script to configure "staticability" of plugins
4# author: pancake // nopcode
5# update: 2010-01-14
6#
7
8LANG=C
9LC_ALL=C
10LOADLIBS=1
11
12export LANG
13export LC_ALL
14
15list () {
16  for a in $STATIC ; do echo "static  $a" ; done
17  for a in $SHARED ; do echo "shared  $a" ; done
18  exit 0
19}
20
21help () {
22  echo "Usage: ./configure-plugins [options]"
23  echo "  -n                   do nothing.. do not generate any file"
24  echo "  --list               list all static and shared plugins"
25  echo "  --rm-static [dir]    remove plugins that are already in core from dir"
26  echo "  --static [name ..]   define named plugin as static"
27  echo "  --shared [name ..]   define named plugin as shared"
28  echo "  --help, -h           display this helpful message"
29  echo "NOTE: static plugins are compiled inside the owner library"
30  exit 0
31}
32
33cfg=./plugins.cfg
34
35if [ ! -f "$cfg" ]; then
36  cp -f dist/plugins-cfg/plugins.def.cfg plugins.cfg
37fi
38
39load () {
40  if [ -e $cfg ]; then
41    echo "configure-plugins: Loading $cfg .."
42    . $cfg
43  else
44    echo "configure-plugins: Loading $1 .."
45    . "$1"
46  fi
47}
48
49save () {
50  cp $cfg plugins.cfg
51 # echo "STATIC=\"$STATIC\"" > $cfg
52 # echo "SHARED=\"$SHARED\"" >>$cfg
53}
54
55generate_configh () {
56  plugins=""
57  oldlib=""
58  for a in ${STATIC} ; do
59    lib=$(echo $a | cut -d . -f 1) # library
60    plg=$(echo $a | cut -d . -f 2) # plugin name
61    if [ ! "$oldlib" = "$lib" ]; then
62      [ -n "$oldlib" ] && echo "     0"
63      oldlib=$lib
64      if type perl > /dev/null 2>&1 ; then
65        uclib=$(echo $lib | perl -pe 'tr/[a-z]/[A-Z]/')
66      else
67        uclib=$(echo $lib | tr '[a-z]' '[A-Z]')
68      fi
69      echo
70      echo "#define R_${uclib}_STATIC_PLUGINS \\"
71      plugins="${plugins} __${uclib}"
72    fi
73    echo "     &r_${lib}_plugin_${plg}, \\"
74  done
75  [ -n "$oldlib" ] && echo "     0"
76
77  # FILL EMPTY PLUGIN ARRAYS WITH LOVE
78  for a in ${SHARED} ; do
79    lib=$(echo $a | cut -d . -f 1) # library
80    plg=$(echo $a | cut -d . -f 2) # plugin name
81    if [ ! "$oldlib" = "$lib" ]; then
82      oldlib=$lib
83      if type perl > /dev/null 2>&1; then
84        uclib=$(echo $lib | perl -pe 'tr/[a-z]/[A-Z]/')
85      else
86        uclib=$(echo $lib | tr '[a-z]' '[A-Z]')
87      fi
88      if [ -z "`echo ${plugins} | grep __${uclib}`" ]; then
89        plugins="${plugins} __${uclib}"
90        echo
91        echo "#define R_${uclib}_STATIC_PLUGINS 0"
92      fi
93    fi
94  done
95}
96
97generate_configmk () {
98  plugins=""
99  oldlib=""
100  for a in ${STATIC} ; do
101    lib=$(echo $a | cut -d . -f 1) # library
102    plg=$(echo $a | cut -d . -f 2) # plugin name
103    if [ ! "$oldlib" = "$lib" ]; then
104      [ -n "$oldlib" ] && printf "\n"
105      oldlib=$lib
106      if type perl > /dev/null 2>&1 ; then
107        uclib=$(echo $lib | perl -pe 'tr/[a-z]/[A-Z]/')
108      else
109        uclib=$(echo $lib | tr '[a-z]' '[A-Z]')
110      fi
111      printf "STATIC_${uclib}_PLUGINS= "
112      plugins="${plugins} __${uclib}"
113    fi
114    printf "p/${plg}.mk "
115  done
116
117  echo
118
119  # fill the holes with love
120  for a in ${SHARED} ; do
121    lib=$(echo $a | cut -d . -f 1) # library
122    if type perl > /dev/null 2>&1 ; then
123      uclib=$(echo $lib | perl -pe 'tr/[a-z]/[A-Z]/')
124    else
125      uclib=$(echo $lib | tr '[a-z]' '[A-Z]')
126    fi
127    if [ -z "`echo ${plugins} | grep __${uclib}`" ]; then
128      plugins="${plugins} __${uclib}"
129      echo "STATIC_${uclib}_PLUGINS="
130    fi
131  done
132}
133
134generate () {
135  echo "configure-plugins: Generating libr/config.h .."
136  cat libr/config.h.head > libr/config.h
137  echo "#define R2_LOADLIBS ${LOADLIBS}" >> libr/config.h
138  generate_configh >> libr/config.h
139  cat libr/config.h.tail >> libr/config.h
140
141  echo "configure-plugins: Generating libr/config.mk .."
142  cat libr/config.mk.head > libr/config.mk
143  generate_configmk >> libr/config.mk
144  cat libr/config.mk.tail >> libr/config.mk
145  return
146}
147
148add () {
149  for a in $1 ; do [ $a = $2 ] && return ; done ; echo $1 $2
150}
151
152sub () {
153  n="" ; for a in $1 ; do [ $a = $2 ] && continue ; n="$n $a" ; done ; echo $n
154}
155
156
157echo | sort -t. > /dev/null 2>&1
158if [ $? = 0 ]; then
159  SORT="sort -t."
160else
161  SORT="sort"
162fi
163
164dosort () {
165  ( for a in $1 ; do echo $a ; done ) | tr _ Z | ${SORT} | tr Z _
166  #( for a in $1 ; do echo $a ; done ) | sort -t. --key=1,1d
167}
168
169sort_vars () {
170  STATIC=$(dosort "$STATIC")
171  SHARED=$(dosort "$SHARED")
172}
173
174make_static () {
175  STATIC=$(add "$STATIC" $1)
176  SHARED=$(sub "$SHARED" $1)
177}
178
179make_shared () {
180  SHARED=$(add "$SHARED" $1)
181  STATIC=$(sub "$STATIC" $1)
182}
183
184make_ () { : ; }
185
186
187MODE=""
188DONOTHING=0
189DEFCFG=dist/plugins-cfg/plugins.def.cfg
190
191rmstatic() {
192  C=0
193  if [ -z "$1" ]; then
194    echo "Missing argument"
195    exit 1
196  fi
197  for a in ${STATIC} ; do
198    b="`echo $a | tr . _`"
199    for ext in dll dylib so ; do
200      f="$b.$ext"
201      if [ -f "$f" ]; then
202        C=$(($C+1))
203	printf "  $C found\r"
204        #echo "rm -f $f"
205        rm -f "$f"
206      fi
207    done
208  done
209  echo "Removed $C shared plugins that are already static"
210}
211
212RMSTATIC=-
213
214while : ; do
215  [ -z "$1" ] && break
216  case "$1" in
217  "--static") MODE=static ; ;;
218  "--shared") MODE=shared ; ;;
219  "--without-gpl") DEFCFG=./plugins.nogpl.cfg ;;
220  "--disable-loadlibs") LOADLIBS=0 ;;
221  "--rm-static") RMSTATIC="$2" ; ;;
222  "--list") sort_vars ; list ; ;;
223  "-n") DONOTHING=1 ; ;;
224  "-h"|"--help") help ; ;;
225  *) eval make_$MODE $1 ; ;;
226  esac
227  shift
228done
229
230load ${DEFCFG}
231sort_vars
232
233if [ - != "${RMSTATIC}" ]; then
234  if [ -z "${RMSTATIC}" ]; then
235    echo "Missing argument" >&2
236    exit
237  fi
238  if [ -d "${RMSTATIC}" ]; then
239    cd "${RMSTATIC}" && rmstatic "${RMSTATIC}"
240  fi
241  exit 0
242fi
243
244[ ${DONOTHING} = 0 ] && generate
245
246echo SHARED: ${SHARED}
247echo STATIC: ${STATIC}
248
249save
250
251exit 0
252