]> pd.if.org Git - zpackage/blob - zpm-add
change chown to lchown
[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
129         # only stat the file for items not set on the command line
130         mode=${clmode:-$(zpm stat -l -f '%a' $path)}
131         username=${cluser:-$(zpm stat -l -f '%U' $path)}
132         groupname=${clgroup:-$(zpm stat -l -f '%G' $path)}
133         mtime=${clmtime:-$(zpm stat -l -f '%y' $path)}
134         filetype=${cltype:-$(zpm stat -l -f '%t' "$path")}
135
136         rpath="$path"
137
138         rpath=$(cleanpath "$path")
139
140         # strip off leading slash
141         rpath=${rpath#/}
142
143         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
144                 continue
145         fi
146
147         if [ ! -z "$strip" ]; then
148                 rpath=${rpath#$strip}
149                 rpath=${rpath#/}
150         fi
151
152         if [ -z "$rpath" ]; then
153                 die "$path resolves to nothing"
154         fi
155
156         prefix=$(cleanpath "$prefix")
157         if [ ! -z "$prefix" ]; then
158                 rpath="$prefix/$rpath"
159         fi
160
161         # ensure all paths are absolute
162         rpath=/${rpath#/}
163
164         hash='NULL'
165         target='NULL'
166         case "$filetype" in
167                 regular)
168                         filetype=r
169                         if [ $addcontent -eq 1 ]; then
170                                 hash=$(zpm addfile $pkgfile "$path")
171                                 if [ $? -ne 0 ]; then
172                                         die "zpm addfile failed ($?): $pkgfile $path" 
173                                 fi
174                         else
175                                 if [ -z "$clhash" ]; then
176                                         hash=$(zpm hash "$path")
177                                 else
178                                         hash=$clhash
179                                 fi
180                         fi
181                         hash="'$hash'"
182                         ;;
183                 directory)
184                         filetype=d
185                         ;;
186                 symlink)
187                         filetype=l
188                         if [ -n "$cltarget" ]; then
189                                 target=$cltarget
190                         else 
191                                 target=$(readlink $path)
192                                 target=$(zpm quote -q "$target")
193                         fi
194                         ;;
195                 *)
196                         die "filetype $filetype not supported"
197                         ;;
198         esac
199
200         # TODO check that we have such a package,version,release
201         #cat <<EOS
202         if [ $verbose -gt 2 ]; then
203                 shellecho=".echo on"
204         fi
205         zpm shell $pkgfile <<EOS
206 begin;
207 $shellecho
208 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,configuration,target)
209 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$isconfig,$target);
210 commit;
211 EOS
212
213 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
214 if [ $verbose -gt 1 ]; then
215         printf "%s%s %s:%s %s\n" $filetype $mode $username $groupname $path
216 elif [ $verbose -gt 0 ]; then
217         printf "%s\n" $path
218 fi
219
220 done
221
222 if [ $complete -eq 1 ]; then
223         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
224         zpm packagehash -f $pkgfile -s -q $pkgid
225 else
226         zpm pkg -f $pkgfile $pkgid hash=
227 fi