|
| 1 | +#!/bin/bash |
| 2 | +# A simple loong64 => loongarch64 .deb converter to help traverse the worlds. |
| 3 | +# |
| 4 | +# Mingcong Bai <[email protected]>, 2024 |
| 5 | + |
| 6 | +_display_usage() { |
| 7 | + printf "\ |
| 8 | +Usage: |
| 9 | +
|
| 10 | + loong64-it [PACKAGE1] [PACKAGE2] ... |
| 11 | +
|
| 12 | + - PACKAGE{1..N}: Path to the new-world .deb package to convert. |
| 13 | +
|
| 14 | +" |
| 15 | +} |
| 16 | + |
| 17 | +# Autobuild-like echo functions. |
| 18 | +abwarn() { echo -e "[\e[33mWARN\e[0m]: \e[1m$*\e[0m"; } |
| 19 | +aberr() { echo -e "[\e[31mERROR\e[0m]: \e[1m$*\e[0m"; exit 1; } |
| 20 | +abinfo() { echo -e "[\e[96mINFO\e[0m]: \e[1m$*\e[0m"; } |
| 21 | +abdbg() { echo -e "[\e[32mDEBUG\e[0m]: \e[1m$*\e[0m"; } |
| 22 | + |
| 23 | +_convert_loong64() { |
| 24 | + abinfo "Examining package information: $1 ..." |
| 25 | + dpkg -I "$SRCDIR"/$1 || \ |
| 26 | + aberr "Invalid dpkg package: control (metadata) archive not found: $?" |
| 27 | + CONTROL_EXT="$(ar t "$SRCDIR"/$1 | grep control.tar* | cut -f3 -d'.')" |
| 28 | + case "${CONTROL_EXT}" in |
| 29 | + gz) |
| 30 | + TAR_COMP_FLAG="z" |
| 31 | + ;; |
| 32 | + xz) |
| 33 | + TAR_COMP_FLAG="J" |
| 34 | + ;; |
| 35 | + bz2) |
| 36 | + TAR_COMP_FLAG="j" |
| 37 | + ;; |
| 38 | + "") |
| 39 | + TAR_COMP_FLAG="" |
| 40 | + ;; |
| 41 | + *) |
| 42 | + aberr "Invalid control archive extension ${CONTROL_EXT}!" |
| 43 | + ;; |
| 44 | + esac |
| 45 | + |
| 46 | + abinfo "Unpacking: $1 ..." |
| 47 | + cd $(mktemp -d) || \ |
| 48 | + aberr "Failed to create temporary directory to unpack $1: $?." |
| 49 | + DEBDIR="$(pwd)" |
| 50 | + ar xv "$SRCDIR"/$1 || \ |
| 51 | + aberr "Failed to unpack $1: $?." |
| 52 | + |
| 53 | + abinfo "Unpacking metadata archive: $1 ..." |
| 54 | + mkdir "$DEBDIR"/metadata || \ |
| 55 | + aberr "Failed to create temporary directory for extracting the metdata archive from $1: $?." |
| 56 | + tar -C "$DEBDIR"/metadata -xvf control.tar."${CONTROL_EXT}" || \ |
| 57 | + aberr "Failed to unpack metadata archive from $1: $?." |
| 58 | + |
| 59 | + abinfo "Converting dpkg Architecture key: $1 ..." |
| 60 | + if ! egrep '^Architecture: loong64$' "$DEBDIR"/metadata/control; then |
| 61 | + aberr "Failed to detect a \"loong64\" architecture signature in control file - this is not a valid new-world LoongArch package!" |
| 62 | + fi |
| 63 | + sed -e 's|^Architecture: loong64$|Architecture: loongarch64|g' \ |
| 64 | + -i "$DEBDIR"/metadata/control |
| 65 | + |
| 66 | + abinfo "Building metadata archive (control.tar.${CONTROL_EXT}): $1 ..." |
| 67 | + cd "$DEBDIR"/metadata |
| 68 | + tar cvf${TAR_COMP_FLAG} "$DEBDIR"/control.tar."${CONTROL_EXT}" * || \ |
| 69 | + aberr "Failed to build metadata archive (control.tar.${CONTROL_EXT}) for $1: $?." |
| 70 | + cd "$DEBDIR" |
| 71 | + |
| 72 | + abinfo "Rebuilding dpkg package $1: loongarch64 ..." |
| 73 | + ar rv "$SRCDIR"/$1 control.tar.${CONTROL_EXT} || \ |
| 74 | + aberr "Failed to rebuild dpkg package $1: $?." |
| 75 | + |
| 76 | + #abinfo "Cleaning up: $1 ..." |
| 77 | + #rm -r "$DEBDIR" |
| 78 | + |
| 79 | + abinfo """Your requested package: |
| 80 | +
|
| 81 | + $1 |
| 82 | +
|
| 83 | +Has been successfully converted as a loongarch64 package! |
| 84 | +""" |
| 85 | +} |
| 86 | + |
| 87 | +# Display usage info if `-h' or `--help' is specified. |
| 88 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 89 | + _display_usage |
| 90 | + exit 0 |
| 91 | +fi |
| 92 | + |
| 93 | +# Display usage info with directions if no option is specified. |
| 94 | +if [ -z "$1" ]; then |
| 95 | + abwarn "Please specify package(s) to convert.\n" |
| 96 | + _display_usage |
| 97 | + exit 1 |
| 98 | +fi |
| 99 | + |
| 100 | +# Record working directory. |
| 101 | +SRCDIR="$(pwd)" |
| 102 | + |
| 103 | +# Rebuilding all requested packages. |
| 104 | +for i in "$@"; do |
| 105 | + _convert_loong64 $i |
| 106 | +done |
0 commit comments