Posted to tcl by dbohdan at Mon Sep 21 08:21:05 GMT 2020view pretty

#! /usr/bin/env jimsh

package require jimlib
namespace import lib::$ lib::id

proc decompressor path {
    set ext [file ext [$ path]]
    switch -- [$ ext] {
        .bz2    { list bzcat -- }
        .gz     { list zcat -- }
        .lz4    { list lz4cat -- }
        .xz     { list xzcat -- }
        .zst    { list zstdcat -- }
        default { list cat -- }
    }
}

proc main files {
    set verbose 0
    if {[lindex [$ files] 0] eq {--verbose}} {
        set files [lrange [$ files] 1 end]
        set verbose 1
    }

    set ch stdout

    foreach file [$ files] {
        if {[$ verbose]} {
            puts stderr [list [$ file]]
        }

        set decomp [decompressor [$ file]]
        set dest $(
            [$ decomp] eq [list cat --]
            ? [$ file]
            : [file rootname [$ file]]
        )
        set contents [exec {*}[$ decomp] [$ file]]
        set stat [file stat [$ file]]

        lib::file::tar::write \
            [$ ch] \
            [$ contents] \
            -filename [$ dest] \
            -mtime [$ stat mtime] \
            -uid [$ stat uid] \
            -gid [$ stat gid] \
            -mode [$ stat mode] \

    }

    lib::file::tar::finish [$ ch]
}

main [$ argv]

# vim: set syntax=tcl shiftwidth=4 smarttab expandtab: