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