-# TODO mtime, mode
-sqlite3 $pkgfile <<EOS
-PRAGMA foreign_keys = ON;
-insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,hash)
-values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$hash')
-;
-EOS
-elif [ -d "$path" ]; then
-sqlite3 $pkgfile <<EOS
-PRAGMA foreign_keys = ON;
-insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname)
-values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname')
-;
-EOS
-elif [ -l "$path" ]; then
- target=$(readlink $path)
-sqlite3 $pkgfile <<EOS
-PRAGMA foreign_keys = ON;
-insert or replace into packagefiles (package,version,release,path,mode,mtime,username,groupname,target)
-values ('$package', '$pkgver', $pkgrel, '$rpath', '$mode',$mtime, '$username','$groupname','$target')
-;
-EOS
-fi
-#printf "%s %s%s\n" $path $rpath ${target:+" -> $target"}
-printf "%s\n" $path
+conflicts() {
+ echo conflict check $pkg $pkgfile
+ #newver=$1
+ {
+ zpm shell $pkgfile<<-EOS
+ .separator " "
+ select hash,path from packagefiles
+ where package||'-'||version||'-'||release = '$pkg'
+ and filetype = 'r'
+ ;
+ EOS
+ } | while read hash path; do
+ rpath=$pkgroot$path
+ if [ -f $rpath ]; then
+ ihash=$(zpm hash $rpath)
+ if [ $ihash != $hash ]; then
+ die $path already exists
+ fi
+ elif [ -d $rpath ]; then
+ die $path already exists
+ fi
+ done
+
+ {
+ zpm shell $pkgfile<<-EOS
+ .separator " "
+ select path from packagefiles
+ where package||'-'||version||'-'||release = '$pkg'
+ and filetype = 'd'
+ ;
+ EOS
+ } | while read hash path; do
+ rpath=$pkgroot$path
+ if [ -f $rpath ]; then
+ die $path already exists
+ elif [ -d $rpath ]; then
+ die $path already exists
+ elif [ -e $rpath ]; then
+ die $path already exists
+ fi
+ done
+}
+
+for pkg in $pkglist; do
+ pname=$(pkgname $pkg)
+ curver=$(zpm findpkg $ZPMDB $pname)
+ pkg=$(zpm findpkg $pkgfile $pkg)
+ upgrade='n'
+
+ if [ -n "$curver" ]; then
+ set +e
+ upgrade=$(zpm vercmp $pkg $curver)
+ fi
+
+ case $upgrade in
+ 0) die 'already installed' $curver;;
+ 1) do_upgrade $pkg $curver ; exit 0 ;;
+ -1) die 'would be downgrade' $curver '->' $pkg ;;
+ n) printf 'ok to install %s\n' $pkg ;;
+ esac
+
+ zpm merge $pkgfile $pkg
+ # TODO find scripts marked as pre-install
+ #run_preinstall
+
+ # TODO if this is an upgrade, run pre-upgrade
+ #if [ $upgrade != 'n' ]; then
+ # run_preupgrade
+ #fi
+
+ # TODO get total size of install so we can do a progress bar
+ #tsize=totalsize
+
+ # add package info to local package db
+ #zpm merge $pkgfile $pkg
+
+ # check for conflicts
+
+ conflicts $pkgroot $pkg
+
+ # mark package in localdb as installing
+ # zpm setmark installing $pkg
+ # install all the files for a package
+ # TODO install directories first in order of path length
+ echo installing $pkglist
+ filecount=$(pathlist | wc -l)
+ echo $filecount files
+ #die 'aborting for test'
+ #pathlist
+ zpm pkg $pkg status installing
+ pathlist | xargs zpm ipkgfile ${user+-u $user} ${group+-g $group} -R "$pkgroot" -f $pkgfile -- $pkg
+
+ # TODO find scripts marked as post-install
+ # TODO if this is an upgrade, run post-upgrade
+
+ # mark as ready for install
+ zpm pkg $pkg status installed
+ #zpm setmark ready $pkg