]> pd.if.org Git - zpackage/blob - zpm-add
add support for notes
[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 while getopts :f:vr:l:P:S:cu:g:NC opt; do
42         case $opt in
43                 N) addcontent=0 ;;
44                 f) pkgfile="$OPTARG" ;;
45                 P) prefix="$OPTARG" ;;
46                 S) strip=$(cleanpath "$OPTARG"); ;;
47                 t) tags="$tags $OPTARG" ;;
48                 c) isconfig=1 ;;
49                 u) username="$OPTARG" ;;
50                 g) groupname="$OPTARG" ;;
51                 v) verbose=1 ;;
52                 C) complete=1 ;;
53                 *) echo 'unknown option' $OPTARG; exit 1 ;;
54         esac
55 done
56 shift $((OPTIND - 1))
57
58 if [ $isconfig -eq 1 ]; then
59         tags="$tags configuration"
60 fi
61
62 if [ $verbose -gt 1 ]; then
63         set -x
64 fi
65
66 pkgid="$1"
67 shift
68 eval $(zpm parse -E $pkgid)
69
70 if [ -z "$pkgfile" ]; then
71         pkgfile=$ZPM_PACKAGE_FILE
72 fi
73
74 if [ -z "$release" ]; then
75         if [ -z "$pkgfile" ]; then
76                 die "cannot determine package file"
77         else
78                 pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
79                 if [ -z "$pkgstr" ]; then
80                         die "unable to find package id for $pkgid in $pkgfile"
81                 fi
82                 pkgid=$pkgstr
83                 # need to reparse the new package id
84                 eval $(zpm parse -E $pkgid)
85         fi
86 fi
87
88 # look for a .zpm file here
89 if [ -z "$pkgfile" ] && [ -f "$pkgid.zpm" ]; then
90         pkgfile="$pkgid.zpm"
91 fi
92
93 if [ -z "$pkgfile" ]; then
94         die "cannot determine package file"
95 fi
96
97 # check for package file
98 if [ ! -f "$pkgfile" ]; then
99         echo $pkgfile does not exist
100         exit 1
101 fi
102
103 set -e
104 zpm test -v $pkgfile
105 set +e
106
107 if [ $verbose -gt 0 ]; then
108         echo adding to $pkgfile $pkgid
109 fi
110
111 package=$(zpm quote "$name")
112 pkgver=$(zpm quote "$version")
113 pkgrel=$(zpm quote "$release")
114
115 #strip=$(cleanpath "$strip")
116 for path in $*; do
117         #echo adding $path
118         mtime=$(zpm stat -f '%y' $path)
119         uid=$(zpm stat -f '%u' $path)
120         gid=$(zpm stat -f '%g' $path)
121         : ${username:=$(zpm stat -f '%U' $path)}
122         : ${groupname:=$(zpm stat -f '%G' $path)}
123         mode=$(zpm stat -f '%a' $path)
124         rpath="$path"
125
126         rpath=$(cleanpath "$path")
127
128         # strip off leading slash
129         rpath=${rpath#/}
130
131         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
132                 continue
133         fi
134
135         if [ ! -z "$strip" ]; then
136                 echo "stripping $strip"
137                 rpath=${rpath#$strip}
138                 rpath=${rpath#/}
139         fi
140
141         if [ -z "$rpath" ]; then
142                 die "$path resolves to nothing"
143         fi
144
145         prefix=$(cleanpath "$prefix")
146         if [ ! -z "$prefix" ]; then
147                 rpath="$prefix/$rpath"
148         fi
149
150         filetype=$(zpm stat -l -f '%t' "$path")
151         hash='NULL'
152         target='NULL'
153         case "$filetype" in
154                 regular)
155                         filetype=r
156                         if [ $addcontent -eq 1 ]; then
157                                 hash=$(zpm addfile $pkgfile "$path")
158                                 if [ $? -ne 0 ]; then
159                                         die "zpm addfile failed ($?): $pkgfile $path" 
160                                 fi
161                         else
162                                 hash=$(zpm hash "$path")
163                         fi
164                         hash="'$hash'"
165                         ;;
166                 directory)
167                         filetype=d
168                         ;;
169                 symlink)
170                         filetype=l
171                         target=$(readlink $path)
172                         target="'$target'"
173                         ;;
174         esac
175
176         # TODO check that we have such a package,version,release
177         #cat <<EOS
178         zpm shell $pkgfile <<EOS
179 begin;
180 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
181 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
182 commit;
183 EOS
184
185 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
186 if [ $verbose -gt 0 ]; then
187         printf "%s\n" $path
188 fi
189
190 done
191
192 if [ $complete -eq 1 ]; then
193         zpm pkg -f $pkgfile $pkgid build_time=$(date +'%s')
194         zpm packagehash -f $pkgfile -s -q $pkgid
195 else
196         zpm pkg -f $pkgfile $pkgid hash=
197 fi