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