]> pd.if.org Git - zpackage/blob - zpm-add
remove create option to 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 set -x
58 eval $(zpm parse -E $pkgid)
59
60 if [ -z "$pkgfile" ];
61         pkgfile=$ZPM_PACKAGE_FILE
62 fi
63
64 # look for a .zpm file here
65 if [ -z "$pkgfile" ] && [ -n "$release" ] && [ -f "$pkgid.zpm" ];
66         pkgfile="$pkgid.zpm"
67 fi
68
69 if [ -z "$pkgfile" ]; then
70         die "cannot determine package file"
71 fi
72
73 set -e
74
75 # check for package file
76 if [ ! -f "$pkgfile" ]; then
77         echo $pkgfile does not exist
78         exit 1
79 fi
80
81 zpm test -v $pkgfile
82
83 if [ -z "$release" ]; then
84         pkgstr=$(zpm findpkg -f $pkgfile $pkgid)
85 fi
86
87 if [ -z "$pkgstr" ]; then
88         die "unable to find package id for $pkgid"
89 fi
90 pkgid=$pkgstr
91
92 #strip=$(cleanpath "$strip")
93 for path in $*; do
94         #echo adding $path
95         mtime=$(zpm stat -f '%y' $path)
96         uid=$(zpm stat -f '%u' $path)
97         gid=$(zpm stat -f '%g' $path)
98         : ${username:=$(zpm stat -f '%U' $path)}
99         : ${groupname:=$(zpm stat -f '%G' $path)}
100         mode=$(zpm stat -f '%a' $path)
101         rpath="$path"
102
103         rpath=$(cleanpath "$path")
104
105         # strip off leading slash
106         rpath=${rpath#/}
107
108         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
109                 continue
110         fi
111
112         if [ ! -z "$strip" ]; then
113                 echo "stripping $strip"
114                 rpath=${rpath#$strip}
115                 rpath=${rpath#/}
116         fi
117
118         if [ -z "$rpath" ]; then
119                 die "$path resolves to nothing"
120         fi
121
122         prefix=$(cleanpath "$prefix")
123         if [ ! -z "$prefix" ]; then
124                 rpath="$prefix/$rpath"
125         fi
126
127         filetype=$(zpm stat -f '%t' "$path")
128         hash='NULL'
129         target='NULL'
130         case "$filetype" in
131                 regular)
132                         filetype=r
133                         hash=$(zpm addfile $pkgfile "$path")
134                         if [ $? -ne 0 ]; then
135                                 die "zpm addfile failed ($?): $pkgfile $path" 
136                         fi
137                         hash="'$hash'"
138                         ;;
139                 directory)
140                         filetype=d
141                         ;;
142                 symlink)
143                         filetype=l
144                         target=$(readlink $path)
145                         target="'$target'"
146                         ;;
147         esac
148
149         # TODO check that we have such a package,version,release
150         zpm shell $pkgfile <<EOS
151 PRAGMA foreign_keys = ON;
152 begin;
153 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
154 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
155 commit;
156 EOS
157
158 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
159 printf "%s\n" $path
160 done