]> pd.if.org Git - zpackage/blob - zpm-add
add zpm-parse program
[zpackage] / zpm-add
1 #!/bin/sh
2
3 pkgver=${ZPMPKGVER:-1.0}
4 pkgrel=${ZPMPKGREL:-1}
5
6 die() {
7         echo $* 1>&2
8         exit 1
9 }
10
11 # basic cleanup on a path
12 cleanpath() {
13         clean="$1"
14         if [ -z "$clean" ]; then printf ''; fi
15
16         # multiple slashes
17         clean=$(printf "%s" "$clean" | sed -e 's|/+|/|g')
18         # curdir
19         clean=$(printf "%s" "$clean" | sed -e 's|/\./|/|g')
20         # leading curdir
21         clean=${clean#./}
22         # trailing curdir
23         clean=${clean%/.}
24         # trailing slash
25         clean=${clean%/}
26         printf "%s" "$clean"
27 }
28
29 tags=
30 create=0
31 # option for "multipackage" just to let the system know that's what you meant
32 # option to take filenames from stdin
33 # parse package, version, release from file if not given
34 while getopts :f:v:r:l:P:S:Ccu:g: opt; do
35         case $opt in
36                 f) pkgfile="$OPTARG" ;;
37                 v) pkgver="$OPTARG" ;;
38                 r) pkgrel="$OPTARG" ;;
39                 l) licenses="$OPTARG" ;;
40                 P) prefix="$OPTARG" ;;
41                 S) strip=$(cleanpath "$OPTARG"); ;;
42                 t) tags="$tags $OPTARG" ;;
43                 c) tags="$tags configuration" ;;
44                 C) create=1 ;;
45                 u) username="$OPTARG" ;;
46                 g) groupname="$OPTARG" ;;
47                 *) echo 'unknown option' $opt; exit 1 ;;
48         esac
49 done
50 shift $((OPTIND - 1))
51
52 package="$1"
53 shift
54 if [ -z "$package" ]; then
55         die "must specify package"
56 fi
57
58 set -e
59 if [ -z "$pkgfile" ]; then
60         pkgfile="$package-$pkgver-$pkgrel.zpm"
61 fi
62
63 # check for package file
64 if [ ! -f "$pkgfile" ]; then
65         if [ $create -eq 1 ]; then
66                 echo creating $pkgfile
67                 zpm newpackage -I -f $pkgfile -v $pkgver -r $pkgrel $package || exit 1
68         else
69                 echo $pkgfile does not exist
70                 exit 1
71         fi
72 fi
73
74 zpm test -v $pkgfile
75
76 #strip=$(cleanpath "$strip")
77 for path in $*; do
78         #echo adding $path
79         mtime=$(zpm stat -f '%y' $path)
80         uid=$(zpm stat -f '%u' $path)
81         gid=$(zpm stat -f '%g' $path)
82         : ${username:=$(zpm stat -f '%U' $path)}
83         : ${groupname:=$(zpm stat -f '%G' $path)}
84         mode=$(zpm stat -f '%a' $path)
85         rpath="$path"
86
87         rpath=$(cleanpath "$path")
88
89         # strip off leading slash
90         rpath=${rpath#/}
91
92         if [ -z "$rpath" ] || [ "$rpath" = '.' ]; then
93                 continue
94         fi
95
96         if [ ! -z "$strip" ]; then
97                 echo "stripping $strip"
98                 rpath=${rpath#$strip}
99                 rpath=${rpath#/}
100         fi
101
102         if [ -z "$rpath" ]; then
103                 die "$path resolves to nothing"
104         fi
105
106         prefix=$(cleanpath "$prefix")
107         if [ ! -z "$prefix" ]; then
108                 rpath="$prefix/$rpath"
109         fi
110
111         filetype=$(zpm stat -f '%t' "$path")
112         hash='NULL'
113         target='NULL'
114         case "$filetype" in
115                 regular)
116                         filetype=r
117                         hash=$(zpm addfile $pkgfile "$path")
118                         if [ $? -ne 0 ]; then
119                                 die "zpm addfile failed ($?): $pkgfile $path" 
120                         fi
121                         hash="'$hash'"
122                         ;;
123                 directory)
124                         filetype=d
125                         ;;
126                 symlink)
127                         filetype=l
128                         target=$(readlink $path)
129                         target="'$target'"
130                         ;;
131         esac
132
133         # TODO check that we have such a package,version,release
134         zpm shell $pkgfile <<EOS
135 PRAGMA foreign_keys = ON;
136 begin;
137 insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,filetype,hash,target)
138 values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$filetype',$hash,$target);
139 commit;
140 EOS
141
142 #printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
143 printf "%s\n" $path
144 done