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