File indexing completed on 2024-04-28 05:02:35

0001 #!/usr/bin/env bash
0002 
0003 set -euo pipefail
0004 
0005 finalimagename=""
0006 
0007 TEMPDIR="$(mktemp --tmpdir -d "$(basename "${0/.sh}")"-XXXXX.d)"
0008 export TEMPDIR
0009 function cleanup_on_exit {
0010         rm -rf "${TEMPDIR}"
0011 }
0012 trap cleanup_on_exit EXIT
0013 
0014 function buildahsetx() {
0015         set -x
0016         buildah "${@}"
0017         exitcode=$?
0018         { set +x ; } 2>/dev/null
0019         return ${exitcode}
0020 }
0021 
0022 function podmansetx() {
0023         set -x
0024         podman "${@}"
0025         exitcode=$?
0026         { set +x ; } 2>/dev/null
0027         return ${exitcode}
0028 }
0029 
0030 function create_directories() {
0031         local id=$1
0032 
0033         mkdir -p /tmp/kbibtex-podman
0034         for d in xdg-config-home kbibtex-podman ; do
0035                 buildahsetx run "${id}" -- mkdir -p "/tmp/${d}" || exit 1
0036         done
0037         for d in runtime cache config ; do
0038                 buildahsetx run "${id}" -- mkdir -m 700 -p "/tmp/xdg-${d}-dir" || exit 1
0039         done
0040 }
0041 
0042 function set_environment() {
0043         buildahsetx config --env QT_PLUGIN_PATH=/usr/lib/qt/plugins --env QT_X11_NO_MITSHM=1 --env KDEDIRS=/usr --env XDG_RUNTIME_DIR=/tmp/xdg-runtime-dir --env XDG_CACHE_HOME=/tmp/xdg-cache-home --env XDG_CONFIG_HOME=/tmp/xdg-config-home --env XDG_DATA_DIRS=/usr/share --env XDG_DATA_HOME=/tmp/xdg-data-home --env XDG_CURRENT_DESKTOP=KDE "$1"
0044 }
0045 
0046 function create_user() {
0047         local id
0048         id=$1
0049         local username
0050         username="${2:-kdeuser}"
0051 
0052         echo "Creating user '${username}' in working container '${id}'"
0053 
0054         [[ "${username}" != "neon" ]] && buildahsetx run --user root "${id}" -- useradd --home-dir /tmp --no-log-init --no-create-home --shell /bin/bash --user-group "${username}"
0055         for d in xdg-config-home kbibtex-podman ; do
0056                 buildahsetx run --user root "${id}" -- chown -R "${username}:${username}" "/tmp/${d}" || exit 1
0057         done
0058         for d in runtime cache config ; do
0059                 buildahsetx run --user root "${id}" -- chown -R "${username}:${username}" "/tmp/xdg-${d}-dir" || exit 1
0060         done
0061 }
0062 
0063 function copy_config_files_to_image() {
0064         local id
0065         id=$1
0066         echo "Copying configuration files into working container '${id}'"
0067 
0068         cat <<EOF >"${TEMPDIR}/etc-fonts-local.conf"
0069 <?xml version='1.0' encoding='UTF-8'?>
0070 <!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
0071 <fontconfig>
0072  <!-- Generic name aliasing -->
0073  <alias>
0074   <family>sans-serif</family>
0075   <prefer>
0076    <family>IBM Plex Sans</family>
0077   </prefer>
0078  </alias>
0079  <alias>
0080   <family>serif</family>
0081   <prefer>
0082    <family>IBM Plex Serif</family>
0083   </prefer>
0084  </alias>
0085  <alias>
0086   <family>monospace</family>
0087   <prefer>
0088    <family>IBM Plex Mono</family>
0089   </prefer>
0090  </alias>
0091 </fontconfig>
0092 EOF
0093         buildahsetx copy "${id}" "${TEMPDIR}/etc-fonts-local.conf" /etc/fonts/local.conf || exit 1
0094 
0095         # Make KDE use double-click and use Breeze theme
0096         cat <<EOF >"${TEMPDIR}/kbibtex-kdeglobals"
0097 [General]
0098 ColorScheme=Breeze
0099 Name=Breeze
0100 widgetStyle=Breeze
0101 
0102 [Icons]
0103 Theme=breeze
0104 
0105 [KDE]
0106 SingleClick=false
0107 ColorScheme=Breeze
0108 LookAndFeelPackage=org.kde.breeze.desktop
0109 widgetStyle=breeze
0110 EOF
0111         buildahsetx copy "${id}" "${TEMPDIR}/kbibtex-kdeglobals" /tmp/xdg-config-home/kdeglobals || exit 1
0112 }
0113 
0114 
0115 function create_ubuntu_ddebslist() {
0116         local CODENAME
0117         CODENAME="$1"
0118 
0119         {
0120                 echo "deb http://ddebs.ubuntu.com ${CODENAME} main restricted universe multiverse"
0121                 echo "deb http://ddebs.ubuntu.com ${CODENAME}-updates main restricted universe multiverse"
0122                 echo "deb http://ddebs.ubuntu.com ${CODENAME}-proposed main restricted universe multiverse"
0123         } >"${TEMPDIR}/ddebs.list"
0124 }
0125 
0126 
0127 function build_archlinux() {
0128         local FROMIMAGE
0129         FROMIMAGE="docker://archlinux:latest"
0130         local IMAGENAME
0131         IMAGENAME="archlinux-kde-devel"
0132         local WORKINGCONTAINERNAME
0133         WORKINGCONTAINERNAME="working-$(sed -r 's!docker://!!g;s![^a-z0-9.-]+!-!g' <<<"${FROMIMAGE}")"
0134 
0135         # Remove any residual images of the same name, ignore errors such as if no such image
0136         buildahsetx rm "${WORKINGCONTAINERNAME}" 2>/dev/null >&2
0137         podmansetx rmi -f "${IMAGENAME}" 2>/dev/null >&2
0138 
0139         # Pull base image from remote repository
0140         id=$(buildahsetx from --name "${WORKINGCONTAINERNAME}" "${FROMIMAGE}") || exit 1
0141         [[ -n "${id}" ]] || { echo "ID is empty" >&2 ; exit 1 ; }
0142         echo "Using ID ${id} for '${IMAGENAME}'"
0143 
0144         create_directories "${id}"
0145         set_environment "${id}"
0146 
0147         # DISTRIBUTION-SPECIFIC CODE BEGINS HERE
0148         buildahsetx run --user root "${id}" -- pacman -Syu --noconfirm || exit 1
0149         # TODO install BibUtils
0150         buildahsetx run --user root "${id}" -- pacman -S --noconfirm ttf-ibm-plex sudo okular frameworkintegration breeze-icons xdg-desktop-portal-kde kbibtex || exit 1
0151         buildahsetx run --user root "${id}" -- rm -f /var/cache/pacman/pkg/*.pkg* || exit 1
0152         # DISTRIBUTION-SPECIFIC CODE ENDS HERE
0153 
0154         copy_config_files_to_image "${id}" || exit 1
0155         create_user "${id}" || exit 1
0156 
0157         buildahsetx commit "${id}" "${IMAGENAME}" 2>&1 | tee "${TEMPDIR}/buildah-commit-output.txt" || exit 1
0158         buildahcommitlastline="$(tail -n 1 <"${TEMPDIR}/buildah-commit-output.txt")"
0159         grep -qP '^[0-9a-f]{24,96}$' <<<"${buildahcommitlastline}" && finalimagename="${buildahcommitlastline}"
0160 
0161         return 0
0162 }
0163 
0164 
0165 function build_ubuntu2204() {
0166         local FROMIMAGE
0167         FROMIMAGE="docker://ubuntu:22.04"
0168         local IMAGENAME
0169         IMAGENAME="ubuntu2204-kbibtex-package"
0170         local WORKINGCONTAINERNAME
0171         WORKINGCONTAINERNAME="working-$(sed -r 's!docker://!!g;s![^a-z0-9.-]+!-!g' <<<"${FROMIMAGE}")"
0172 
0173         # Remove any residual images of the same name, ignore errors such as if no such image
0174         buildahsetx rm "${WORKINGCONTAINERNAME}" 2>/dev/null >&2
0175         podmansetx rmi -f "${IMAGENAME}" 2>/dev/null >&2
0176 
0177         # Pull base image from remote repository
0178         id=$(buildahsetx from --name "${WORKINGCONTAINERNAME}" "${FROMIMAGE}") || exit 1
0179         [[ -n "${id}" ]] || { echo "ID is empty" >&2 ; exit 1 ; }
0180         echo "Using ID ${id} for '${IMAGENAME}'"
0181 
0182         create_directories "${id}"
0183         set_environment "${id}"
0184 
0185         # DISTRIBUTION-SPECIFIC CODE BEGINS HERE
0186         create_ubuntu_ddebslist jammy
0187         buildahsetx copy "${id}" "${TEMPDIR}/ddebs.list" /etc/apt/sources.list.d/ddebs.list || exit 1
0188         export TZ="Europe/Berlin"
0189         buildahsetx config --env TZ="${TZ}" "${id}"
0190         echo "${TZ}" >"${TEMPDIR}/timezone"
0191         buildahsetx copy "${id}" "${TEMPDIR}/timezone" /etc/timezone || exit 1
0192         buildahsetx run "${id}" -- ln -snf /usr/share/zoneinfo/"${TZ}" /etc/localtime || exit 1
0193         buildahsetx run "${id}" -- apt install -y ubuntu-dbgsym-keyring || exit 1
0194         buildahsetx run "${id}" -- apt update || exit 1
0195         # TODO install BibUtils
0196         buildahsetx run "${id}" -- apt install -y sudo fonts-ibm-plex breeze-icon-theme kde-style-breeze frameworkintegration gdb valgrind xdg-desktop-portal-kde okular kbibtex kbibtex-dbgsym || exit 1
0197         buildahsetx run "${id}" -- sudo apt autoremove || exit 1
0198         buildahsetx run "${id}" -- sudo apt clean || exit 1
0199         # DISTRIBUTION-SPECIFIC CODE ENDS HERE
0200 
0201         copy_config_files_to_image "${id}" || exit 1
0202         create_user "${id}" || exit 1
0203 
0204         buildahsetx commit "${id}" ${IMAGENAME} 2>&1 | tee "${TEMPDIR}/buildah-commit-output.txt" || exit 1
0205         buildahcommitlastline="$(tail -n 1 <"${TEMPDIR}/buildah-commit-output.txt")"
0206         grep -qP '^[0-9a-f]{24,96}$' <<<"${buildahcommitlastline}" && finalimagename="${buildahcommitlastline}"
0207 
0208         return 0
0209 }
0210 
0211 
0212 function build_ubuntu2210() {
0213         local FROMIMAGE
0214         FROMIMAGE="docker://ubuntu:22.10"
0215         local IMAGENAME
0216         IMAGENAME="ubuntu2210-kbibtex-package"
0217         local WORKINGCONTAINERNAME
0218         WORKINGCONTAINERNAME="working-$(sed -r 's!docker://!!g;s![^a-z0-9.-]+!-!g' <<<"${FROMIMAGE}")"
0219 
0220         # Remove any residual images of the same name, ignore errors such as if no such image
0221         buildahsetx rm "${WORKINGCONTAINERNAME}" 2>/dev/null >&2
0222         podmansetx rmi -f "${IMAGENAME}" 2>/dev/null >&2
0223 
0224         # Pull base image from remote repository
0225         id=$(buildahsetx from --name "${WORKINGCONTAINERNAME}" "${FROMIMAGE}") || exit 1
0226         [[ -n "${id}" ]] || { echo "ID is empty" >&2 ; exit 1 ; }
0227         echo "Using ID ${id} for '${IMAGENAME}'"
0228 
0229         create_directories "${id}"
0230         set_environment "${id}"
0231 
0232         # DISTRIBUTION-SPECIFIC CODE BEGINS HERE
0233         create_ubuntu_ddebslist kinetic
0234         buildahsetx copy "${id}" "${TEMPDIR}/ddebs.list" /etc/apt/sources.list.d/ddebs.list || exit 1
0235         export TZ="Europe/Berlin"
0236         buildahsetx config --env TZ="${TZ}" "${id}"
0237         echo "${TZ}" >"${TEMPDIR}/timezone"
0238         buildahsetx copy "${id}" "${TEMPDIR}/timezone" /etc/timezone || exit 1
0239         buildahsetx run "${id}" -- ln -snf /usr/share/zoneinfo/"${TZ}" /etc/localtime || exit 1
0240         buildahsetx run "${id}" -- apt install -y ubuntu-dbgsym-keyring || exit 1
0241         buildahsetx run "${id}" -- apt update || exit 1
0242         # TODO install BibUtils
0243         buildahsetx run "${id}" -- apt install -y sudo fonts-ibm-plex breeze-icon-theme kde-style-breeze frameworkintegration gdb valgrind xdg-desktop-portal-kde okular kbibtex kbibtex-dbgsym || exit 1
0244         buildahsetx run "${id}" -- sudo apt autoremove || exit 1
0245         buildahsetx run "${id}" -- sudo apt clean || exit 1
0246         # DISTRIBUTION-SPECIFIC CODE ENDS HERE
0247 
0248         copy_config_files_to_image "${id}" || exit 1
0249         create_user "${id}" || exit 1
0250 
0251         buildahsetx commit "${id}" ${IMAGENAME} 2>&1 | tee "${TEMPDIR}/buildah-commit-output.txt" || exit 1
0252         buildahcommitlastline="$(tail -n 1 <"${TEMPDIR}/buildah-commit-output.txt")"
0253         grep -qP '^[0-9a-f]{24,96}$' <<<"${buildahcommitlastline}" && finalimagename="${buildahcommitlastline}"
0254 
0255         return 0
0256 }
0257 
0258 
0259 function build_debian12() {
0260         local FROMIMAGE
0261         FROMIMAGE="docker://debian:bookworm"
0262         local IMAGENAME
0263         IMAGENAME="debian12-kbibtex-package"
0264         local WORKINGCONTAINERNAME
0265         WORKINGCONTAINERNAME="working-$(sed -r 's!docker://!!g;s![^a-z0-9.-]+!-!g' <<<"${FROMIMAGE}")"
0266 
0267         # Remove any residual images of the same name, ignore errors such as if no such image
0268         buildahsetx rm "${WORKINGCONTAINERNAME}" 2>/dev/null >&2
0269         podmansetx rmi -f "${IMAGENAME}" 2>/dev/null >&2
0270 
0271         # Pull base image from remote repository
0272         id=$(buildahsetx from --name "${WORKINGCONTAINERNAME}" "${FROMIMAGE}") || exit 1
0273         [[ -n "${id}" ]] || { echo "ID is empty" >&2 ; exit 1 ; }
0274         echo "Using ID ${id} for '${IMAGENAME}'"
0275 
0276         create_directories "${id}"
0277         set_environment "${id}"
0278 
0279         # DISTRIBUTION-SPECIFIC CODE BEGINS HERE
0280         echo "deb http://ftp.se.debian.org/debian/ bookworm main contrib" >"${TEMPDIR}/etc-apt-sources.list"
0281         buildahsetx copy "${id}" "${TEMPDIR}/etc-apt-sources.list" /etc/apt/sources.list || exit 1
0282         buildahsetx run "${id}" -- apt update || exit 1
0283         buildahsetx run "${id}" -- apt dist-upgrade -y || exit 1 # typically up-to-date, but still possible:
0284         # TODO install BibUtils
0285         buildahsetx run "${id}" -- apt install -y sudo fonts-ibm-plex breeze-icon-theme kde-style-breeze frameworkintegration gdb xdg-desktop-portal-kde okular kbibtex || exit 1
0286         buildahsetx run "${id}" -- apt autoremove || exit 1
0287         buildahsetx run "${id}" -- apt clean || exit 1
0288         # DISTRIBUTION-SPECIFIC CODE ENDS HERE
0289 
0290         copy_config_files_to_image "${id}" || exit 1
0291         create_user "${id}" || exit 1
0292 
0293         buildahsetx commit "${id}" ${IMAGENAME} 2>&1 | tee "${TEMPDIR}/buildah-commit-output.txt" || exit 1
0294         buildahcommitlastline="$(tail -n 1 <"${TEMPDIR}/buildah-commit-output.txt")"
0295         grep -qP '^[0-9a-f]{24,96}$' <<<"${buildahcommitlastline}" && finalimagename="${buildahcommitlastline}"
0296 
0297         return 0
0298 }
0299 
0300 
0301 if (( $# == 1 )) ; then
0302         if [[ $1 == "--cleanup" ]] ; then
0303                 buildahsetx images -f dangling=true
0304                 echo "!!! Remove any with  podman rmi -f IMAGE-HEX"
0305                 echo
0306                 podmansetx image ls
0307                 echo "!!! Remove any with  podman rmi -f IMAGE-HEX"
0308                 echo
0309                 podmansetx container ls
0310                 echo "!!! Remove any with  buildah rm CONTAINER-HEX"
0311                 echo
0312                 buildahsetx containers
0313                 echo "!!! Remove any with  buildah rm CONTAINER-HEX"
0314                 exit 0
0315         elif [[ $1 == "archlinux" ]] ; then
0316                 build_archlinux || exit 1
0317         elif [[ $1 == "debian12" || $1 == "bookworm" ]] ; then
0318                 build_debian12 || exit 1
0319         elif [[ $1 == "ubuntu2204" ]] ; then
0320                 build_ubuntu2204 || exit 1
0321         elif [[ $1 == "ubuntu"* ]] ; then
0322                 build_ubuntu2210 || exit 1
0323         else
0324                 echo "Unknown argument, expecting one of the following:  archlinux  debian12  ubuntu2204  ubuntu2210" >&2
0325                 echo "To get help how to clean up previously created images or containers, run  $(basename "$0") --cleanup" >&2
0326                 exit 1
0327         fi
0328 else
0329         echo "Missing argument, expecting one of the following:  archlinux  debian12  ubuntu2204  ubuntu2210" >&2
0330         echo "To get help how to clean up previously created images or containers, run  $(basename "$0") --cleanup" >&2
0331         exit 1
0332 fi
0333 
0334 if [[ -n "${finalimagename}" ]] ; then
0335         echo "-------------------------------------------------------------------------------"
0336         echo "To run KBibTeX in a container, issue:"
0337         echo "  xhost local:root && mkdir -p /tmp/kbibtex-podman && podman container run --rm --net=host -e DISPLAY --env XDG_RUNTIME_DIR=/tmp/xdg-runtime-dir --env XDG_CACHE_HOME=/tmp/xdg-cache-home -v /tmp/kbibtex-podman:/tmp/kbibtex-podman --env XDG_CONFIG_HOME=/tmp/xdg-config-home --env XDG_DATA_HOME=/tmp/xdg-data-home --env XDG_CURRENT_DESKTOP=KDE -v /tmp/.X11-unix:/tmp/.X11-unix -v ${HOME}/.Xauthority:/.Xauthority --tty --interactive ${finalimagename} sudo -u kdeuser /usr/bin/kbibtex ; xhost -local:root"
0338         echo
0339         echo "To run KBibTeX in a container inside of gdb, issue:"
0340         echo "  xhost local:root && mkdir -p /tmp/kbibtex-podman && podman container run --rm --net=host -e DISPLAY --env XDG_RUNTIME_DIR=/tmp/xdg-runtime-dir --env XDG_CACHE_HOME=/tmp/xdg-cache-home -v /tmp/kbibtex-podman:/tmp/kbibtex-podman --env XDG_CONFIG_HOME=/tmp/xdg-config-home --env XDG_DATA_HOME=/tmp/xdg-data-home --env XDG_CURRENT_DESKTOP=KDE -v /tmp/.X11-unix:/tmp/.X11-unix -v ${HOME}/.Xauthority:/.Xauthority --tty --interactive ${finalimagename} sudo -u kdeuser gdb /usr/bin/kbibtex ; xhost -local:root"
0341         echo
0342         echo "To run a container with just Bash, issue:"
0343         echo "  podman container run --rm --net=host --tty --interactive ${finalimagename} /bin/bash"
0344         echo "-------------------------------------------------------------------------------"
0345 fi