blob: 55ff88e6ca5aea17ab98fd9765312958f3d0d1ac (
plain)
1 #!/bin/sh
2 #
3 # pkgexport
4 # 05/24/2004
5 #
6 # Copyright (c) 2002-2004 by Andrew Green <crux@dorkatronix.org>
7 # License: GNU General Public License (GPL)
8 #
9 # Script to build a package from installed components.
10 #
11 # Should be useful if upgraded package fails
12 #
13 #
14 COMMAND=$(basename $0)
15 VERSION=0.8
16 VERBOSE=0
17 PKGNAME=""
18 TARGET=$(pwd)
19 ROOT=""
20
21 #########
22 # Usage
23 #########
24 usage() {
25 echo "
26 $COMMAND $VERSION
27 options: [-h] [-v] [-r /pkg/root] <packagename> [outputfile/dir]
28
29 -r Directory of alternate package root. See pkginfo(8)
30 -h Usage/Help
31 -v Verbose output
32
33 Examples:
34
35 Export a current package to 'package#1.2.4-1.pkg.tar.gz' in the current dir:
36 $COMMAND currentpackage
37
38 Export mozilla to '/tmp/mozilla#1.2.1-1.pkg.tar.gz':
39 $COMMAND mozilla /tmp
40
41 Export pkgutils to '/root/pkgutils.tar.gz':
42 $COMMAND pkgutils /root/pkgutils.tar.gz
43
44 Export a different CRUX installation of samba:
45 $COMMAND -r /mnt samba
46 "
47 exit 1
48 }
49
50 ##########
51 # clean_on_exit removes temporary files etc.
52 ##########
53 clean_on_exit() {
54 [ -f $FILES ] && rm $FILES
55 }
56
57 ##########
58 # pkginfo_l lists files inside a package (pkginfo -l $file)
59 ##########
60 pkginfo_l() {
61
62 FILES="$(mktemp --tmpdir ${COMMAND}.XXXXXXXXXX)"
63 pkginfo $ROOT -l $1 > $FILES
64 if [ $RET -ne 0 ]
65 then
66 echo "Package $1 contains no files and/or pkginfo -l $1 failed."
67 exit 1
68 fi
69
70 }
71
72 #########
73 # pkginfo_i queries to see if a package is installed (pkginfo -i)
74 #########
75 pkginfo_i() {
76
77 INSTALLED=(`pkginfo $ROOT -i | grep "^$1 "`)
78 RET=$?
79
80 if [ $RET -ne 0 ]
81 then
82 echo "Package $1 is not installed."
83 exit 1
84 fi
85
86 }
87
88 #########
89 # exportpkg will build a pkgfile
90 #########
91 exportpkg() {
92
93 pkgname=${INSTALLED[0]}
94 pound="#"
95 version=${INSTALLED[1]}
96
97 # get compression mode from /etc/pkgmk.conf
98 compression="-z"
99 compression_suffix="gz"
100 [ -f /etc/pkgmk.conf ] && . /etc/pkgmk.conf
101 [ $PKGMK_COMPRESSION_MODE ] && compression_suffix=$PKGMK_COMPRESSION_MODE
102 suffix=".pkg.tar.$compression_suffix"
103 case $compression_suffix in
104 gz) compression="-z" ;;
105 bz2) compression="-j" ;;
106 xz) compression="-J" ;;
107 esac
108
109 if [ -d $TARGET ]; then
110 FIRST=$(echo $TARGET | cut -c 1)
111 if [ "$FIRST" != "/" ]; then
112 newpkg=$(pwd)/$TARGET/$pkgname
113 else
114 newpkg=$TARGET/$pkgname
115 fi
116
117 newver=$version
118 else
119 newpkg=$(pwd)/$TARGET
120 pound=""
121 newver=""
122 suffix=""
123 fi
124
125 BIGFILENAME="$newpkg$pound$newver$suffix"
126 if [ -f "$BIGFILENAME" ]; then
127 echo "$BIGFILENAME" exists. Exiting
128 exit 1
129 fi
130
131 [ $VERBOSE = 1 ] && TARARGS="-v"
132
133 cd /
134 bsdtar -c $compression $TARARGS -f "$BIGFILENAME" -n `cat $FILES`
135 RET=$?
136
137 if [ $RET -ne 0 ]
138 then
139 echo "Tar: error in creating $BIGFILENAME."
140 rm -f "$BIGFILENAME"
141 exit 1
142 fi
143
144 if [ $VERBOSE = 1 ]; then
145 echo ===============================================
146 echo
147 echo Package export information for current package:
148 echo
149 echo Name: $pkgname
150 echo Version: $version
151 echo
152 echo Package saved as: $BIGFILENAME
153 echo
154 echo ===============================================
155 else
156 echo Package saved as: $BIGFILENAME
157 fi
158
159 }
160
161 #########
162 # Main !
163 #########
164 main() {
165
166 if [ $# -eq 0 ]; then
167 usage
168 fi
169
170 while getopts :r:hv arg
171 do
172 case $arg in
173 r) ROOT="-r "$OPTARG;;
174 h) usage;;
175 v) VERBOSE=1;;
176 \?) usage;;
177 :) usage;;
178 esac
179 done
180 shift $((OPTIND - 1))
181
182 # echo 1: $1 2: $2
183 PKGNAME=$1
184
185 if [ "$PKGNAME" = "" ]; then
186 usage
187 fi
188 if [ "$2" != "" ]; then
189 TARGET=$2
190 fi
191
192 pkginfo_i $PKGNAME
193 pkginfo_l $PKGNAME
194 exportpkg
195
196 }
197
198
199 trap "clean_on_exit" EXIT
200 main $*
|