]> pd.if.org Git - zpackage/blob - zpm-add
cleanup foreach-path
[zpackage] / zpm-add
1 #!/bin/sh
2
3 # zpm add -f pkgfile pkgid [ files ]
4
5 # no package file? take from ZPM_PACKAGE_FILE
6 # no pkgid ? take from ZPM_PACKAGE_ID
7 # no id impossible
8
9 die() {
10         echo $* 1>&2
11         exit 1
12 }
13
14 # basic cleanup on a path
15 cleanpath() {
16         clean="$1"
17         if [ -z "$clean" ]; then printf ''; fi
18
19         # multiple slashes
20         clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
21         # curdir
22         clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
23         # leading curdir
24         clean=${clean#./}
25         # trailing curdir
26         clean=${clean%/.}
27         # trailing slash
28         clean=${clean%/}
29         printf "%s" "$clean"
30 }
31
32 verbose=0
33 tags=
34 isconfig=0
35 addcontent=1
36 complete=0
37 # option for "multipackage" just to let the system know that's what you meant
38 # option to take filenames from stdin
39 # parse package, version, release from file if not given
40 # TODO -l follow symlinks, -L follow symlinks, adding all, links and targets
41 # -T set link target
42 # -M set mtime
43 # -m set mode
44 # -H set hash, will be overridden for regular files unless -N is set
45 # -F set filetype, regular, symlink, directory
46 while getopts :f:vr:l:P:S:cu:g:NCm:M:T:H:F: opt; do
47         case $opt in
48                 N) addcontent=0 ;;
49                 f) pkgfile="$OPTARG" ;;
50                 P) prefix="$OPTARG" ;;
51                 S) strip=$(cleanpath "$OPTARG"); ;;
52                 t) tags="$tags $OPTARG" ;;
53                 c) isconfig=1 ;;
54                 u) cluser="$OPTARG" ;;
55                 g) clgroup="$OPTARG" ;;
56                 m) clmode="$OPTARG" ;;
57                 M) clmtime="$OPTARG" ;;
58                 T) cltarget="$OPTARG" ;;
59                 F) cltype="$OPTARG" ;;
60                 H) clhash="$OPTARG" ;;
61                 v) verbose=$((verbose + 1)) ;;
62                 C) complete=1 ;;
63                 *) echo 'unknown option' $OPTARG; exit 1 ;;
64         esac
65 done
66 shift $((OPTIND - 1))
67
68 if [ $isconfig -eq 1 ]; then
69         tags="$tags configuration"
70 fi
71
72 if [ $verbose -gt 2 ]; then
73         set -x
74 fi
75
76 pkgid="$1"
77 shift
78 eval $(zpm parse -E $pkgid)
79
80 if [ -z "$pkgfile" ]; then
81         pkgfile=$ZPM_PACKAGE_FILE
82 fi
83
84 if [ -z "$release" ]; then
85         if [ -z "$pkgfile" ]; then
86                 die "cannot determine package file"
87         else
88                 pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
89                 if [ -z "$pkgstr" ]; then
90                         die "unable to find package id for $pkgid in $pkgfile"
91                 fi
92                 pkgid=$pkgstr
93                 # need to reparse the new package id
94                 eval $(zpm parse -E $pkgid)
95         fi
96 fi
97
98 # look for a .zpm file here
99 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
100         pkgfile="$pkgid.zpm"
101 fi
102
103 if [ -z "$pkgfile" ]; then
104         die "cannot determine package file"
105 fi
106
107 # check for package file
108 if [ ! -f "$pkgfile" ]; then
109         echo $pkgfile does not exist
110         exit 1
111 fi
112
113 set -e
114 zpm test -v $pkgfile
115 set +e
116
117 if [ $verbose -gt 0 ]; then
118         echo adding to $pkgfile $pkgid
119 fi
120
121 package=$(zpm quote "$name")
122 pkgver=$(zpm quote "$version")
123 pkgrel=$(zpm quote "$release")
124
125 #strip=$(cleanpath "$strip")
126 for path in $*; do
127         #echo adding $path
128 if [ $verbose -gt 1 ]; then
129         printf "adding %s\n" $path
130 fi
131
132         # only stat the file for items not set on the command line
133         mode=${clmode:-$(zpm stat -l -f '%a' $path)}
134         username=${cluser:-$(zpm stat -l -f '%U' $path)}
135         groupname=${clgroup:-$(zpm stat -l -f '%G' $path)}
136         mtime=${clmtime:-$(zpm stat -l -f '%y' $path)}
137         filetype=${cltype:-$(zpm stat -l -f '%t' "$path")}
138
139         rpath="$path"
140
141         rpath=$(cleanpath "$path")
142
143         # strip off leading slash
144         rpath=${rpath#/}
145
146         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
147                 continue
148         fi
149
150         if [ ! -z "$strip" ]; then
151                 rpath=${rpath#$strip}
152                 rpath=${rpath#/}
153         fi
154
155         if [ -z "$rpath" ]; then
156                 die "$path resolves to nothing"
157         fi
158
159         prefix=$(cleanpath "$prefix")
160         if [ ! -z "$prefix" ]; then
161                 rpath="$prefix/$rpath"
162         fi
163
164         # ensure all paths are absolute
165         rpath=/${rpath#/}
166
167         hash='NULL'
168         target='NULL'
169         case "$filetype" in
170                 regular)
171                         filetype=r
172                         if [ $addcontent -eq 1 ]; then
173                                 hash=$(zpm addfile $pkgfile "$path")
174                                 if [ $? -ne 0 ]; then
175                                         die "zpm addfile failed ($?): $pkgfile $path" 
176                                 fi
177                         else
178                                 if [ -z "$clhash" ]; then
179                                         hash=$(zpm hash "$path")
180                                 else
181                                         hash=$clhash
182                                 fi
183                         fi
184                         hash="'$hash'"
185                         ;;
186                 directory)
187                         filetype=d
188                         ;;
189                 symlink)
190                         filetype=l
191                         if [ -n "$cltarget" ]; then
192                                 target=$cltarget
193                         else 
194                                 target=$(readlink $path)
195                                 target=$(zpm quote -q "$target")
196                         fi
197                         ;;
198                 *)
199                         die "filetype $filetype not supported"
200                         ;;
201         esac
202
203         # TODO check that we have such a package,version,release
204         #cat <<EOS
205         if [ $verbose -gt 2 ]; then
206                 shellecho=".echo on"
207         fi
208         zpm shell $pkgfile <<EOS
209 begin;
210 $shellecho
211 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
212 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
213 commit;
214 EOS
215
216 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
217 if [ $verbose -gt 1 ]; then
218         printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname $path
219 elif [ $verbose -gt 0 ]; then
220         printf "%s\n" $path
221 fi
222
223 done
224
225 if [ $complete -eq 1 ]; then
226         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
227         zpm packagehash -f $pkgfile -s -q $pkgid
228 else
229         zpm pkg -f $pkgfile $pkgid hash=
230 fi