#!/bin/sh -efu

owner="$1"; shift
GB_REPO_NAME="$1"; shift
src="$1"; shift
flavour="$1"; shift
nprocs="$1"; shift
dir="$1"; shift
arch="$1"; shift
comps="$1"; shift
GB_APT_CACHE_LIMIT="$1"; shift

# a simple sanity check
[ "$GB_APT_CACHE_LIMIT" -gt 1048576 ]

owner=$(printf %s "$owner" |tr . _)
repo_name=$(printf %s "$GB_REPO_NAME" |tr . _)

cd

# recreate $TMPDIR/tmp
# recreate ~/tmp as symlink to $TMPDIR/tmp
tmpdir="$TMPDIR/tmp"
rm -rf -- "$tmpdir"
mkdir -p -- "$tmpdir"
rm -rf tmp
ln -snf -- "$tmpdir"

# recreate $TMPDIR/build
# recreate ~/build as symlink to $TMPDIR/build
build="$TMPDIR/build"
rm -rf -- "$build"
mkdir -p -- "$build"
ln -snf -- "$build"

# recreate $TMPDIR/install
# recreate ~/install as symlink to $TMPDIR/install
install="$TMPDIR/install"
rm -rf -- "$install"
mkdir -p -- "$install"
ln -snf -- "$install"

# create $TMPDIR/hasher if necessary
# recreate ~/hasher as symlink to $TMPDIR/hasher
workdir="$TMPDIR/hasher"
mkdir -p -- "$workdir"
ln -snf -- "$workdir"

# store pkglists outside of aptbox
rm -rf -- "$workdir/lists"
mkdir -p -- "$workdir/lists/partial"

# support for full private repo
gb_repo="$TMPDIR/gb-repo"
rm -rf -- "$gb_repo"
mkdir -p -- "$gb_repo"
ln -snf -- "$gb_repo"

# store hasher build repo outside of workdir
rm -rf -- "$HOME/hasher_repo"
mkdir -p -- "$HOME/hasher_repo"

# recreate hasher config file
mkdir -p -- .hasher
cat >.hasher/config <<EOF
workdir="\$TMPDIR/hasher"
apt_config="\$workdir/apt.conf"
def_target="$arch"
def_repo="\$HOME/hasher_repo"
known_mountpoints=/proc,/dev/pts
USER="$owner-$repo_name"
EOF
[ -z "$nprocs" ] ||
	echo "nprocs=$nprocs" >>.hasher/config
[ -f .hasher/config.local ] &&
	cat .hasher/config.local >>.hasher/config

# pass remaining command line arguments (if any) to .hasher/config
for arg; do
	printf '%s\n' "$arg"
done >>.hasher/config

# recreate apt config files
cat >"$workdir/apt.conf" <<EOF
Dir::Etc::main "/dev/null";
Dir::Etc::parts "/var/empty";
Dir::Etc::sourceparts "/var/empty";
Dir::Etc::sourcelist "$workdir/sources.list";
Dir::State::lists "$workdir/lists";
Dir::Etc::pkgpriorities "$workdir/pkgpriorities";
Debug::pkgMarkInstall "true";
Debug::pkgProblemResolver "true";
APT::Cache-Limit "$GB_APT_CACHE_LIMIT";
EOF
cat >"$workdir/sources.list" <<EOF
rpm file:$dir $arch $comps
rpm file:$dir noarch $flavour
EOF
cat >"$workdir/pkgpriorities" <<EOF
Important:
  basesystem
Required:
  apt
Standard:
  kernel-doc
  libpam0
  libpam0-devel
  maxima-bin-gcl
  postfix
  python-dev
  python-modules-tkinter
  altlinux-release-$GB_REPO_NAME
  branding-altlinux-office-server-alterator
  branding-altlinux-office-server-bootloader
  branding-altlinux-office-server-bootsplash
  branding-altlinux-office-server-graphics
  branding-altlinux-office-server-indexhtml
  branding-altlinux-office-server-notes
  branding-altlinux-office-server-slideshow
EOF
sed -e "s/@arch@/$arch/" "$src" >>"$workdir/sources.list"
