#!/bin/bash
ADMIN_DIR=/var/lib/dpkg
APT_SOURCES_DIR=/etc/apt/sources.list.d
APT_PKGDB_DIR=/var/lib/apt/lists
CHROOT_DIR=""
#based on https://github.com/s243a/woof-next/blob/e6590c1044adc0a182a9b299129ed707b7bd95d2/builders/deb-build.sh#L80
prepare_dirs() {
for aDir in "$CHROOT_DIR" "$CHROOT_DIR$APT_SOURCES_DIR" "$CHROOT_DIR$APT_PKGDB_DIR"; do
[ ! -z "`which realpath`" ] && aDir=$(realpath -m "$aDir")
mkdir -p "$aDir"
done
! [ -e $CHROOT_DIR$ADMIN_DIR/status ] && echo > $CHROOT_DIR$ADMIN_DIR/status
! [ -e $CHROOT_DIR$ADMIN_DIR/available ] && echo > $CHROOT_DIR$ADMIN_DIR/available
}
#based on https://github.com/s243a/woof-next/blob/e6590c1044adc0a182a9b299129ed707b7bd95d2/builders/deb-build.sh#L457
function update_pkg_status(){
#Probably better just to call remove_pkg_status instead of processing args.
#while [ ! -z $1 ];
# case $1 in
# --status=*)
# shift
# ;;
# *)
# break
# ;;
# esac
{
echo \
"Package: $1
Status: install ok installed
Priority: $2
Section: $3
Maintainer: unspecified
Architecture: $ARCH
Version: $4"
[ "${5%,}" ] && echo "Depends: ${5%,}"
echo "Description: $1 installed by deb-build.sh
"
} >> "$CHROOT_DIR$ADMIN_DIR/status"
}
#based on https://pastebin.com/cRcR6yE2
AWK_PRG=$(cat <<EOF
function dep_fix(str, a,k1,p1,p2,out,seps){
split(str,a,",")
out = ""
#print "str=" str
for (k1 in a){
p1=a[k1]
#print "a[ " k1 "]=" a[k1]
gsub(/^+/,"",p1)
split(p1,p2,/\&../,seps) # +libqpdf21>8.4 > p2[1]="+libqpdf21" seps[1]=">" p2[2]="8.4",
#print "p2[0]=" p2[0]
#print "p2[1]=" p2[1]
#print "p2[2]=" p2[2]
#print "seps[1]=" seps[1]
switch (seps[1]) {
case ">":
out = out ", " p2[1] " (>" p2[2] ")"
break
case "&ge":
out = out ", " p2[1] " (>=" p2[2] ")"
break
case "<":
out = out ", " p2[1] " (<" p2[2] ")"
break
case "&le":
out = out "," p2[1] " (<=" p2[2] ")"
break
case "&eq":
out = out ", " p2[1] " (=" p2[2] ")"
break
default:
out = out ", " p2[1]
break
}
gsub(/^, /,"",out)
}
return out
}
BEGIN{FS="|"}
{
PACKAGE=\$2
print "Package: " PACKAGE
VERSION = \$3
#TODO make PACKAGE regular expression safe
gsub(/[+]/,"\\\\+",PACKAGE)
gsub(PACKAGE,"",VERSION)
gsub(/^_/,"",VERSION)
gsub(/_[^_]*$/,"",VERSION)
print "Version: " VERSION
ARCH = \$1
gsub(/.*_/,"",ARCH)
if ( ARCH == "i386" ){
print "Architecture: " ARCH
} else if ( ARCH = noarch ){
print "Architecture: noarch"
}
#TODO Architecture: \$1 but trimmed after last _ check that it is like i386
print "Maintainer: unspecified"
DEPS = \$9
DEPS = dep_fix(DEPS)
if (length(DEPS)>0){
print "Depends: " DEPS
}
print "Priority: " PRIORITY
print "Section: " \$5
print ""
}
EOF
)
#echo "$AWK_PRG"
function append_statuses(){
for aFile in "$@"; do
aFile=$(realpath -m $aFile)
if [[ aFile = */woof-installed-packages ]]; then
PRIORITY=required
ESSENTIAL=yes
else
PRIORITY=optional
ESSENTIAL=no
fi
cat "$aFile" | awk --assign PRIORITY="$PRIORITY" "$AWK_PRG" >> "$CHROOT_DIR$ADMIN_DIR/status"
done
}
mv $CHROOT_DIR$ADMIN_DIR/status $CHROOT_DIR$ADMIN_DIR/status_back$$
prepare_dirs
append_statuses /var/packages/woof-installed-packages /var/packages/user-installed-packages
#TODO symlink file lists