Added a create_handbook_icon function and rearranged some of the icon creation for...
[catalyst.git] / targets / support / chroot-functions.sh
1
2 # Trap these signals and kill ourselves if recieved
3 # Force ourselves to die if any of these signals are recieved
4 # most likely our controlling terminal is gone
5 trap "echo SIGTERM signal recieved killing $0 with pid $$;kill -9 $$" SIGTERM
6 trap "echo SIGHUP signal recieved killing $0 with pid $$;kill -9 $$" SIGHUP
7 trap "echo SIGKILL signal recieved killing $0 with pid $$;kill -9 $$" SIGKILL
8
9 #SIGINT interrupt character (usually Ctrl-C)
10 #       * example: high-level sequence of events
11 #       * my process (call it "P") is running
12 #       * user types ctrl-c
13 #       * kernel recognizes this and generates SIGINT signal
14 trap "echo SIGINT signal recieved killing $0 with pid $$;kill -9 $$" SIGINT
15  
16 check_genkernel_version(){
17         if [ -x /usr/bin/genkernel ]
18         then
19                 genkernel_version=$(genkernel --version)
20                 genkernel_version_major=${genkernel_version%%.*}
21                 genkernel_version_minor_sub=${genkernel_version#${genkernel_version_major}.}
22                 genkernel_version_minor=${genkernel_version_minor_sub%%.*}
23                 genkernel_version_sub=${genkernel_version##*.}
24                 if [ -n "${genkernel_version}" -a "${genkernel_version_major}" -eq '3' -a "${genkernel_version_minor}" -ge '3' ]
25                 then
26                         echo "Genkernel version ${genkernel_version} found ... continuing"
27                 else
28                         echo "ERROR: Your genkernel version is too low in your seed stage.  genkernel version 3.3.0"
29                         echo "or greater is required."
30                         exit 1
31                 fi
32         else
33                 exit 1
34         fi
35 }
36
37 setup_myfeatures(){
38         if [ -n "${clst_CCACHE}" ]
39         then
40                 export clst_myfeatures="${clst_myfeatures} ccache"
41                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_ccache ]
42                 #then
43                 #       echo "CCACHE Autoresume point found not emerging ccache"
44                 #else
45                         emerge --oneshot --nodeps -b -k ccache || exit 1
46                 #       touch /tmp/.clst_ccache
47                 #fi
48         fi
49
50         if [ -n "${clst_DISTCC}" ]
51         then
52                 export clst_myfeatures="${clst_myfeatures} distcc"
53                 export DISTCC_HOSTS="${clst_distcc_hosts}"
54                 #if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_distcc ]
55                 #then
56                 #       echo "DISTCC Autoresume point found not emerging distcc"
57                 #else
58                         USE="-gtk -gnome" emerge --oneshot --nodeps -b -k distcc || exit 1
59                         #touch /tmp/.clst_distcc
60                 #fi
61                 mkdir -p /etc/distcc
62                 echo "${clst_distcc_hosts}" > /etc/distcc/hosts
63         fi
64 }
65
66 setup_myemergeopts(){
67         if [ -n "${clst_VERBOSE}" ]
68         then
69                 clst_myemergeopts="--verbose"
70         else
71                 clst_myemergeopts="--quiet"
72         fi
73         if [ -n "${clst_FETCH}" ]
74         then
75                 export bootstrap_opts="-f"
76                 export clst_myemergeopts="${clst_myemergeopts} -f"
77         elif [ -n "${clst_PKGCACHE}" ]
78         then
79                 export clst_myemergeopts="${clst_myemergeopts} --usepkg --buildpkg --newuse"
80                 export bootstrap_opts="-r"
81         fi
82 }
83
84
85 setup_portage(){
86         # portage needs to be merged manually with USE="build" set to avoid frying
87         # our make.conf. emerge system could merge it otherwise.
88         if [ "${clst_AUTORESUME}" = "1" -a -e /tmp/.clst_portage ]
89         then
90                 echo "Portage Autoresume point found not emerging portage"
91         else
92                 USE="build" run_emerge --oneshot --nodeps portage
93                 touch /tmp/.clst_portage || exit 1
94         fi
95 }
96
97 setup_gcc(){
98         if [ -x /usr/bin/gcc-config ]
99         then
100                 mythang=$( cd /etc/env.d/gcc; ls ${clst_CHOST}-* | head -n 1 )
101                 if [ -z "${mythang}" ]
102                 then
103                         mythang=1
104                 fi
105                 gcc-config ${mythang}; update_env_settings
106         fi
107 }
108
109 setup_binutils(){
110         if [ -x /usr/bin/binutils-config ]
111         then
112                 mythang=$( cd /etc/env.d/binutils; ls ${clst_CHOST}-* | head -n 1 )
113                 if [ -z "${mythang}" ]
114                 then
115                         mythang=1
116                 fi
117                 binutils-config ${mythang}; update_env_settings
118         fi
119 }
120
121 cleanup_distcc() {
122         rm -rf /etc/distcc/hosts
123 }
124
125 update_env_settings(){
126         /usr/sbin/env-update
127         source /etc/profile
128         [ -f /tmp/envscript ] && source /tmp/envscript
129 }
130
131 die() {
132         echo "$1"
133         exit 1
134 }
135
136 make_destpath() {
137         if  [ "${1}" = "" ]
138         then
139                 export ROOT=/
140         else
141                 export ROOT=${1}
142                 if [ ! -d ${ROOT} ]
143                 then
144                         install -d ${ROOT}
145                 fi
146         fi
147 }
148
149 run_emerge() {
150         # Sets up the ROOT= parameter
151         # with no options ROOT=/
152         make_destpath ${clst_root_path}
153         
154         export EMERGE_WARNING_DELAY=0   
155         export CLEAN_DELAY=0
156         export EBEEP_IGNORE=0
157         export EPAUSE_IGNORE=0
158         export CONFIG_PROTECT="-*"
159
160         if [ -n "${clst_VERBOSE}" ]
161         then
162                 echo "ROOT=${ROOT} emerge ${clst_myemergeopts} -pt $@" || exit 1
163                 emerge ${clst_myemergeopts} -pt $@ || exit 3
164                 echo "Press any key within 15 seconds to pause the build..."
165                 read -s -t 15 -n 1
166                 if [ $? -eq 0 ]
167                 then
168                         echo "Press any key to continue..."
169                         read -s -n 1
170                 fi
171         fi
172
173         echo "emerge ${clst_myemergeopts} $@" || exit 1
174
175         emerge ${clst_myemergeopts} $@ || exit 1
176 }
177
178 # Functions
179 # Copy libs of a executable in the chroot
180 function copy_libs() {
181         # Check if it's a dynamix exec
182         ldd ${1} > /dev/null 2>&1 || return
183
184         for lib in `ldd ${1} | awk '{ print $3 }'`
185         do
186                 echo ${lib}
187                 if [ -e ${lib} ]
188                 then
189                         if [ ! -e ${clst_root_path}/${lib} ]
190                         then
191                                 copy_file ${lib}
192                                 [ -e "${clst_root_path}/${lib}" ] && \
193                                 strip -R .comment -R .note ${clst_root_path}/${lib} \
194                                 || echo "WARNING : Cannot strip lib ${clst_root_path}/${lib} !"
195                         fi
196                 else
197                         echo "WARNING : Some library was not found for ${lib} !"
198                 fi
199         done
200 }
201
202 function copy_symlink() {
203         STACK=${2}
204         [ "${STACK}" = "" ] && STACK=16 || STACK=$((${STACK} - 1 ))
205
206         if [ ${STACK} -le 0 ] 
207         then
208                 echo "WARNING : ${TARGET} : too many levels of symbolic links !"
209                 return
210         fi
211
212         [ ! -e ${clst_root_path}/`dirname ${1}` ] && \
213                 mkdir -p ${clst_root_path}/`dirname ${1}`
214         [ ! -e ${clst_root_path}/${1} ] && \
215                 cp -vfdp ${1} ${clst_root_path}/${1}
216         
217         if [[ -n $(type -p realpath) ]]; then
218             TARGET=`realpath ${1}`
219         else
220             TARGET=`readlink -f ${1}`
221         fi
222         if [ -h ${TARGET} ]
223         then
224                 copy_symlink ${TARGET} ${STACK}
225         else
226                 copy_file ${TARGET}
227         fi
228 }
229
230 function copy_file() {
231         f="${1}"
232
233         if [ ! -e "${f}" ]
234         then
235                 echo "WARNING : File not found : ${f}"
236                 continue
237         fi
238
239         [ ! -e ${clst_root_path}/`dirname ${f}` ] && \
240                 mkdir -p ${clst_root_path}/`dirname ${f}`
241         [ ! -e ${clst_root_path}/${f} ] && \
242                 cp -vfdp ${f} ${clst_root_path}/${f}
243         if [ -x ${f} -a ! -h ${f} ]
244         then
245                 copy_libs ${f}
246                 strip -R .comment -R .note ${clst_root_path}/${f} > /dev/null 2>&1
247         elif [ -h ${f} ]
248         then
249                 copy_symlink ${f}
250         fi
251 }
252
253 create_handbook_icon() {
254         # This function creates a local icon to the Gentoo Handbook
255         echo "[Desktop Entry]
256 Encoding=UTF-8
257 Version=1.0
258 Type=Link
259 URL=file:///mnt/cdrom/docs/handbook/html/index.html
260 Terminal=false
261 Name=Gentoo Linux Handbook
262 GenericName=Gentoo Linux Handbook
263 Comment=This is a link to the local copy of the Gentoo Linux Handbook.
264 Icon=gedit-icon.png" > /usr/share/applications/gentoo-handbook.desktop
265 }