reorganized Config file
[hdw-linux/hdw-linux.git] / scripts / subroutines
1 #! /bin/sh
2 #
3 # author: hackbard@hackdaworld.dyndns.org
4 #
5 # this script defines functions used by the other scripts
6 #
7
8 echo_r()        {
9         echo -e "\e[01;31m${1}\e[0m"            # Red
10                 }
11
12 echo_g()        {
13         echo -e "\e[01;32m${1}\e[0m"            # Green
14                 }
15
16 echo_y()        {
17         echo -e "\e[01;33m${1}\e[0m"            # Yellow
18                 }
19
20 echo_c()        {
21         echo -e "\e[01;36m${1}\e[0m"            # Cyan
22                 }
23
24 echo_w()        {
25         echo -e "\e[01;37m${1}\e[0m"            # White
26                 }
27
28 pkg_c()         {
29         echo -en "\e[01;36m${1}\e[0m"
30                 }
31
32 warn_beep()     {
33         (( n = 1 ))
34         while (( n <= 5 ))
35         do
36                 echo -en "\a"
37                 sleep 1
38                 (( n += 1 ))
39         done
40                 }
41
42 abort_when_package_build_failed()       {
43         return="$1"
44         last="$2"
45         if [ "$return" != "0" -a "$hdw_abort" = "1" ]; then
46                 echo "failed building package $package!"
47                 echo "failed executing $last"
48                 echo "abort."
49                 warn_beep
50                 echo
51                 exit 1
52         fi
53                                         }
54
55 create_init()   {
56         echo "creating init ..."
57         # default variables
58         run="`basename $1 | awk -F. '{ print $1 }'`"
59         bin=$package ; params="" ; depends="" ; exec=""
60         path=$prefix/sbin ; sec_append="exit 0"
61         sync_respawn="" ; use_pfh=0
62         ((s_value=50))
63         # reading variables
64         . $1
65         # process
66         ((s2_value = 100 - s_value))
67         if [ ! -d $root/etc/minit ] ; then
68                 
69                 echo "assuming sysvinit as init system!"
70                 cat > $root/etc/init.d/$run << EOF
71 #!/bin/sh
72 #
73 # hdw - linux $root/etc/init.d/$run
74 #
75
76 # remove this for automatic startup
77 $sec_append
78
79 [ ! -f $path/$bin ] && exit 0
80
81 case "\$1" in
82         start)
83                 echo "starting $run ..."
84                 $path/$bin $params ;;
85         stop)
86                 echo "stopping $run ..."
87                 killall -15 $path/$bin ;;
88         restart)
89                 echo "restarting $run ..."
90                 killall -1 $path/$bin ;;
91         *)
92                 echo "usage: \$0 [start|stop|restart]"
93                 exit 1 ;;
94 esac
95 exit 0
96 EOF
97                 # permissions and links
98                 chmod 750 $root/etc/init.d/$run
99                 ln -sf ../$run $root/etc/init.d/rc2.d/S${s_value}${run}
100                 ln -sf ../$run $root/etc/init.d/rc2.d/K${s2_value}${run}
101         else
102                 echo "assuming minit as init system!"
103                 mkdir -p $root/etc/minit/$run
104                 if [ "$use_pfh" = "1" ] ; then
105                         ln -sf $root/sbin/pidfilehack $root/etc/minit/$run/run
106                 elif [ ! -z "$exec" ] ; then
107                         rm -f $root/etc/minit/$run/run
108                         echo "exec $exec" > $root/etc/minit/$run/run
109                 else
110                         ln -sf $path/$bin $root/etc/minit/$run/run
111                 fi
112                 [ ! -z "$sync_respawn" ] && \
113                         touch $root/etc/minit/$run/$snc_respawn
114                 if [ ! -z "$params" ]; then
115                         rm -f $root/etc/minit/$run/params
116                         for i in $params; do
117                                 echo "$i" >> $root/etc/minit/$run/params
118                         done
119                 fi
120                 if [ ! -z "$depends" ]; then
121                         rm -f $root/etc/minit/$run/depends
122                         for i in $depends; do
123                                 echo "$i" >> $root/etc/minit/$run/depends
124                         done
125                 fi
126         fi
127                 }
128
129 detect_file_ending()    {
130         file="$1"
131         if [ "`echo $file | awk -F'tar\\\.' '{ print $2 }'`" = "bz2" ] ; then
132                 echo "tar.bz2"
133         elif [ ! -z "`echo $file | grep '.tbz2'`" ] ; then
134                 echo "tbz2"
135         fi
136         if [ "`echo $file | awk -F'tar\\\.' '{ print $2 }'`" = "gz" ] ; then
137                 echo "tar.gz"
138         elif [ ! -z "`echo $file | grep '.tgz'`" ] ; then
139                 echo "tgz"
140         elif [ ! -z "`echo $file | grep 'tar.Z'`" ] ; then
141                 echo "tar.Z"
142         fi
143 }
144
145 create_setup_scripts()  {
146         destfile="`echo $1 | awk -F/ '{ print $NF }' | sed 's/\.setup//'`.sh"
147         cat > $root/etc/setup.d/$destfile << EOF
148 #!/bin/sh
149 #
150 # hdw - linux $root/etc/setup.d/$destfile
151 #
152
153 EOF
154         if [ -z "`grep setup_block $1`" ] ; then
155                 cat >> $root/etc/setup.d/$destfile << EOF
156 setup_block()   {
157         echo "setup not supported"
158                 }
159 EOF
160         fi
161         if [ -z "`grep uninstall_block $1`" ] ; then
162                 cat >> $root/etc/setup.d/$destfile << EOF
163 uninstall_block()       {
164         echo "uninstall not supported"
165                         }
166 EOF
167         fi
168         cat $1 >> $root/etc/setup.d/$destfile
169         cat >> $root/etc/setup.d/$destfile << EOF
170 usage() {
171         echo "usage: \$0 [setup|uninstall]"
172         exit 1
173         }
174
175 [ -z "\$1" ] && usage
176
177 while [ "\$1" ] ; do
178         case "\$1" in
179                 setup)          setup_block ; shift ;;
180                 uninstall)      uninstall_block ; shift ;;
181                 *)              usage ; exit 1 ;;
182         esac
183 done
184 EOF
185         chmod 750 $root/etc/setup.d/$destfile
186                         }
187
188 output_if_valid()       {
189         local stage
190         local priority
191         local package
192         local dir
193
194         package=$1
195         stage=$2
196         priority=$3
197         dir="`echo $package | awk -F/ '{ print $3 }'`"
198         for match in `grep '^# \[S\]' $package | \
199                         awk '{ for(i=3;i<=NF;i++) print $i }'`; do
200                 if [ "$stage-$priority" = "$match" ] ; then
201                         echo -en "$stage \t $priority \t\t $dir "
202                         echo -e "`basename $package`"
203                 fi
204                 # stage 4 packages (build at the same time)
205                 if [ "$stage" = "3" ] ; then
206                         if [ "4-$priority" = "$match" ] ; then
207                                 echo -en "4 \t $priority \t\t $dir "
208                                 echo -e "`basename $package`"
209                         fi
210                 fi
211         done
212 }
213
214 create_buildorder()     {
215         local stage
216         stages=$1
217         max_priority=$2
218
219         # categories
220         categories=`grep '^# \[C\]' ./targets/$hdw_target/include | \
221                         awk '{ print $3 }'`
222         categories="$categories $hdw_arch"
223         # add single packages to categories
224         singlepackages=`grep '^# \[P\]' ./targets/$hdw_target/include | \
225                         awk '{ print $3 }'`
226         [ ! -z "$singlepackages" ] && categories="$categories $singlepackages"
227         # packages to remove
228         delpackages=`grep '^# \[R\]' ./targets/$hdw_target/include | \
229                 awk '{ print $3 }'`
230         # use dietlibc?
231         if [ "$hdw_use_dietlibc" = "1" ] ; then
232                 [ -z "`echo $categories | grep diet`"] && \
233                         singlepackages="$singlepackages diet/dietlibc"
234                 delpackages="$delpackages base/glibc"
235         fi
236
237         echo -e "#stage \t #priority \t #category package"
238         for stage in $stages; do
239                 (( counter = -1 ))
240                 while (( counter <= max_priority )); do
241
242                         ((counter += 1))
243                         for dir in $categories; do
244                                 for package in ./packages/$dir/[!C]*; do
245
246                 pkg=`basename $package`
247                 [ -f $package -a "$pkg" != "`basename $dir`" ] && continue
248                 if [ -d $package -a -f $package/$pkg ] ; then
249
250                 [ -z "`echo $delpackages | grep $dir/$pkg`" ] && \
251                         output_if_valid $package/`basename $package` \
252                                         $stage $counter
253
254                 else
255                         foo=`basename $dir`
256                         [ -f ./packages/$dir/$foo ] && \
257                                 output_if_valid ./packages/$dir/$foo \
258                                         $stage $counter
259                 fi
260
261                                 done
262                         done
263                 done
264         done
265 }