treewide: replace /bin/ksh93 hashbangs with /bin/sh
[unleashed-userland.git] / components / osol / ddu / srcs / usr / ddu / scripts / pkg_relate.sh
blob78b321137a6e8a475e5cda7de32f59cde9a06a53
1 #!/bin/sh
3 # CDDL HEADER START
5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
20 # CDDL HEADER END
23 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 # Description: Perform pkg related function:
26 # list repo, add repo, search repo and modify
27 # repo etc.
29 # Usage:
30 # pkg_relate.sh $1 $2
31 # $1 can be "add","list","search" and "modify"
32 # $2 is the sub-argument for $1
33 # $3 is the sub-argument for $1
36 builtin chmod
37 builtin cp
38 builtin mkdir
39 builtin mv
40 builtin rm
41 builtin cat
42 builtin printf
45 # Get all publishers info on the system
47 function fetch_repo
49 pfexec pkg publisher -Ha > /tmp/repo_temp_store 2>>$err_log
50 if [ $? -ne 0 ]; then
51 print -u2 "$0: Error found when getting repo list"
52 exit 1
54 repo_list=$(cat /tmp/repo_temp_store | cut -d " " -f 1 2>/dev/null)
55 repo_url=$(cat /tmp/repo_temp_store | nawk -F"http://" '{ print $2 }' \
56 2>/dev/null)
57 repo_count=$(cat /tmp/repo_temp_store | wc -l 2>/dev/null)
61 # Output the publisher info in the following format:
62 # opensolaris.org http://ipkg.sfbay.sun.com/dev/
65 function print_repo
67 index=1
68 while [ $index -le ${repo_count} ]; do
69 repo_listi=$(echo ${repo_list} | nawk '{ print $'${index}' }')
70 repo_urli=$(echo ${repo_url} | nawk '{ print $'${index}' }')
71 printf "${repo_listi}\thttp://${repo_urli}\n"
72 index=$(($index + 1))
73 done
77 # Output the publisher info with following format based on the input
78 # parameter:
79 # opensolaris.org http://ipkg.sfbay.sun.com/dev/
81 # Input paramenters can be:
82 # all: list all the publisher info.
83 # default: list preferred publisher info.
84 # publisher name: list specified pulibsher info.
87 function list
89 if [ ${min_argu_1} == "all" ]; then #list all repo.
90 fetch_repo
91 print_repo
92 elif [ ${min_argu_1} == "default" ]; then #list default repo
93 pfexec pkg publisher -HP > /tmp/repo_default_store 2>>$err_log
94 repo_list=$(cat /tmp/repo_default_store | cut -d " " -f 1 2>/dev/null)
95 repo_url=$(cat /tmp/repo_default_store | nawk -F"http://" '{ print $2 }' \
96 2>/dev/null)
97 repo_count=$(cat /tmp/repo_default_store | wc -l 2>/dev/null)
98 print_repo
99 elif [ ! -z ${min_argu_1} ]; then #list a specific repo
100 repo_name=${min_argu_1}
101 pfexec pkg publisher ${repo_name} | grep "URI" \
102 > /tmp/repo_spe_store 2>>$err_log
103 if [ $? != 0 ]; then
104 print -u2 "$0: ${repo_name} is not a valid repo name!"
105 exit 1
107 repo_url=$(cat /tmp/repo_spe_store | nawk -F"http://" '{ print $2 }' \
108 2>/dev/null)
109 for repo_urli in ${repo_url}
111 repo_url="http://"${repo_urli}
112 printf "${repo_name}\t${repo_url}\n"
113 done
118 # Search for driver package via pulisher based on the device compatible string
120 function search
122 if [ -z ${min_argu_2} ]; then
123 pakcage_found=0
124 fetch_repo
126 index=1
127 while [ $index -le ${repo_count} ]; do
128 repo_listi=$(echo ${repo_list} | nawk '{ print $'${index}' }')
129 repo_urli=$(echo ${repo_url} | nawk '{ print $'${index}' }')
131 repo_urli="http://"${repo_urli}
133 pfexec pkg search -p -s "${repo_urli}" "${min_argu_1}" \
134 2>>/dev/null | nawk -F"@" '{ print $1 }' | \
135 nawk -F"pkg:/" '{ print $2 }' | uniq > /tmp/pkg_candi
137 pkgname=$(grep driver /tmp/pkg_candi | tail -1 2>/dev/null)
138 if [ -z ${pkgname} ]; then
139 pkgname=$(tail -1 /tmp/pkg_candi 2>/dev/null)
142 if [[ $? != 0 || -z ${pkgname} ]]; then
143 index=$(($index + 1))
144 continue
147 depend_str="depend:incorporate:${pkgname}"
148 pkg_ver=$(pfexec pkg search -l ${depend_str} | tail -1 2>>/dev/null)
150 if [ $? -eq 0 ]; then
151 pkgvalue=$(echo ${pkg_ver} | nawk '{ print $3 }')
152 if [ -z "`echo ${pkgvalue} | grep "pkg:/"`" ]; then
153 pkgcondidate=${pkgvalue}
154 else
155 pkgcondidate=$(echo ${pkgvalue} | nawk -F"pkg:/" '{ print $2 }')
159 if [ -z ${pkgcondidate} ]; then
160 pkgcondidate=${pkgname}
162 pfexec pkg info -r pkg://${repo_listi}/${pkgcondidate} \
163 >/dev/null 2>>$err_log
164 if [ $? -eq 0 ]; then
165 /usr/bin/printf "${repo_listi}\t${pkgcondidate}\n"
166 pakcage_found=1
168 index=$(($index + 1))
169 done
170 else
171 repo_listi=${min_argu_2}
172 pakcage_found=0
173 repo_urli=$(pfexec pkg publisher ${repo_listi} | grep "URI" \
174 | nawk -F"http://" '{ print $2 }')
176 for repo_urli_i in $repo_urli; do
177 repo_urli_i="http://"${repo_urli_i}
179 pfexec pkg search -p -s "${repo_urli_i}" "${min_argu_1}" \
180 2>>/dev/null | nawk -F"@" '{ print $1 }' | \
181 nawk -F"pkg:/" '{ print $2 }' | uniq > /tmp/pkg_candi
183 pkgname=$(grep driver /tmp/pkg_candi | tail -1 2>/dev/null)
184 if [ -z ${pkgname} ]; then
185 pkgname=$(tail -1 /tmp/pkg_candi 2>/dev/null)
187 if [ ! -z ${pkgname} ]; then
188 break
190 done
192 if [[ $? != 0 || -z ${pkgname} ]]; then
193 exit 1
196 depend_str="depend:incorporate:${pkgname}"
197 pkg_ver=$(pfexec pkg search -l ${depend_str} | tail -1 2>>/dev/null)
199 if [ $? -eq 0 ]; then
200 pkgvalue=$(echo ${pkg_ver} | nawk '{ print $3 }')
201 if [ -z "`echo ${pkgvalue} | grep "pkg:/"`" ]; then
202 pkgcondidate=${pkgvalue}
203 else
204 pkgcondidate=$(echo ${pkgvalue} | nawk -F"pkg:/" '{ print $2 }')
208 if [ -z ${pkgcondidate} ]; then
209 pkgcondidate=${pkgname}
211 pfexec pkg info -r pkg://${repo_listi}/${pkgcondidate} \
212 >/dev/null 2>>$err_log
213 if [ $? -eq 0 ]; then
214 printf "${repo_listi}\t${pkgcondidate}\n"
215 pakcage_found=1
218 if [ ${pakcage_found} -eq 0 ]; then
219 exit 1
224 # Add publisher on the system
227 function add
230 #add a repo and set it to default one if set.
232 if [ -z ${min_argu_2} ]; then
233 print -u2 "\n$0: Third argument can not be null..."
234 exit 1
236 publisher_name=${min_argu_1}
237 publisher_url=${min_argu_2}
238 pfexec pkg set-publisher -O ${publisher_url} ${publisher_name} \
239 2>>$err_log 1>&2
240 if [ $? -ne 0 ]; then
241 print -u2 "$0: Error found when adding repo: "
242 print -u2 "${publisher_name} ${publisher_url}"
243 exit 1
248 # Modify the existing publisher on the system via
249 # pkg set-publisher and pkg unset-publisher.
251 function modify
254 #modify a repo and set it to default one if set.
256 if [ -z ${min_argu_2} ]; then
257 print -u2 "\n$0: Third argument can not be null..."
258 exit 1
260 publisher_name=${min_argu_1}
261 publisher_url=${min_argu_2}
263 LC_ALL="C" pfexec pkg publisher ${publisher_name} > \
264 /tmp/spe_repo_temp_store 2>>$err_log
267 origin_uri=`cat /tmp/spe_repo_temp_store | grep "Origin URI" | \
268 nawk -F ": " '{ print $2 }' 2>>$err_log`
270 mirror_uris=`cat /tmp/spe_repo_temp_store | grep "Mirror URI" | \
271 nawk -F ": " '{ print $2 }' 2>>$err_log`
273 store_repo=" -O "${origin_uri}
274 extra_repo=" "
275 unset_extra_repo=" "
277 for mirror_uri in ${mirror_uris}
279 extra_repo=${extra_repo}" -m "${mirror_uri}
280 unset_extra_repo=${unset_extra_repo}" -M "${mirror_uri}
281 done
283 extra_repo=${extra_repo}" "${publisher_name}
285 pfexec pkg unset-publisher ${publisher_name} 2>>/dev/null 1>&2
286 if [ $? -ne 0 ]; then
287 print -u2 "Warning: Can not remove repo ${publisher_name}"
289 pfexec pkg set-publisher -O ${publisher_url} \
290 ${unset_extra_repo} \
291 ${publisher_name} \
292 2>>$err_log 1>&2
293 if [ $? -ne 0 ]; then
294 print -u2 "$0: Error found when adding repo:"
295 print -u2 "${publisher_name} ${publisher_url}"
296 exit 1
297 else
298 echo ${store_repo} > /tmp/spe_repo_store
299 echo ${extra_repo} >> /tmp/spe_repo_store
301 else
302 echo ${store_repo} > /tmp/spe_repo_store
303 echo ${extra_repo} >> /tmp/spe_repo_store
308 # Restore the original publisher on the system
310 function restore
312 pfexec pkg set-publisher ${min_argu_1} ${min_argu_2} \
313 2>>$err_log 1>&2
314 if [ $? -ne 0 ]; then
315 print -u2 "$0: Error found when restoring repo!"
316 exit 1
320 function clean_up
323 rm -f /tmp/repo_temp_store /tmp/repo_default_store \
324 /tmp/repo_spe_store \
325 /tmp/spe_repo_temp_store /tmp/pkg_candi
326 } >/dev/null 2>&1
330 #Main()
332 PATH=/usr/bin:/usr/sbin:$PATH; export PATH
333 LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/ddu/lib; export LD_LIBRARY_PATH
335 trap 'clean_up;exit 10' KILL INT
336 trap 'clean_up' EXIT
340 # Confirm the correct number of commnad line argument
342 if (( $# < 2 )) || (( $# > 3 )); then
343 print -u2 "\n$0: Wrong number of arguments provided..."
344 exit 1
347 if [ -z $1 ]; then
348 print -u2 "\n$0: First argument can not be null..."
349 exit 1
352 if [ -z $2 ]; then
353 print -u2 "\n$0: Second argument can not be null..."
354 exit 1
358 typeset main_argu=$1
359 typeset min_argu_1=$2
360 typeset min_argu_2=$3
361 typeset repo_list repo_url repo_count
362 typeset repo_listi repo_urli repo_name pkgname
363 typeset depend_str pkg_ver pkgcondidate pakcage_found
364 typeset index
365 typeset err_log=/tmp/ddu_err.log
368 # Copy /var/pkg to /tmp if booted off a ramdisk
369 # Needed by pkg utility so it can expand its bootkeeping files in swap space.
371 if [ -x /usr/lib/install/live_img_pkg5_prep ] ; then
372 pfexec /usr/lib/install/live_img_pkg5_prep 2>>$err_log
373 if [ $? != 0 ]; then
374 print -u2 "Error executing: /usr/lib/install/live_img_pkg5_prep."
375 exit 1
380 case ${main_argu} in
381 list) list;;
382 search) search;;
383 add) add;;
384 modify) modify;;
385 restore) restore;;
386 *) print -u2 "\n$0: ${main_argu} is not valid"
387 exit 1;;
388 esac