#!/bin/bash # Part of SMLinux distribution # Bash script to convert tar.{gz,bz,xz} files to tar.lz # Copyright (c) 2022-2025 PktSurf # Permission to use, copy, modify, and distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. err() { printf "**ERROR**\n$@\n" exit 1 } # All the stuff in this script has been tested only in the bash shell. if [[ -z $BASH_VERSINFO ]] ; then err "Is this really a GNU bash shell?" fi usage() { cat << EOF Bash script to convert tar.gz, tar.xz files to tar.lz. Example: cd into the directory containing the above tarball and then run # converttolz -i source-1.0.tar.gz -o source-1.0.tar.lz In most cases, the name of the newly extracted source directory usually matches the name and version of the tarball itself when extracted. Example: # ls source-1.0.tar.gz source-1.0 However in some cases, the name of the source directory may be different. Example: # ls source-1.0.tar.gz src In this case, you may wish to fix the name of the source directory in the resulting output tarball by providing the directory name first hand Example: # converttolz -i source-1.0.tar.gz -o source-1.0.tar.lz -d src Note: converttolz has no way of auto-detecting which name of the source directory to expect. It is assumed that the name will be identical to the name of the destination tarball minus the file extension. You need to tell convertolz the name of the custom directory on case-by-case basis. Also note: this script *will* rectify the name of the source directory, that is, convert src to source-1.0 and repack to .tar.lz Usage: converttolz -i -o -d -r -v Options: -d : Unexpected name of the source directory -i : Input file name -o : Output file name -r : Remove the source directory after the conversion -x : Remove input file -v : Verbose compression -h : Display this help EOF exit 0 } while getopts ':d:i:o:rxv' option; do case "$option" in d) srcdir="$OPTARG" ;; i) inputfile="$OPTARG" ;; o) outputfile="$OPTARG" ;; r) removesourcedir=1 ;; x) removeinputfile=1 ;; v) verbose=1 ;; *) usage ;; esac done if [[ $OPTIND = 1 ]] ; then usage fi if [[ -z $inputfile ]] ; then err "Please provide input tarball name!" elif [[ ! -f $inputfile ]] ; then err "$inputfile not found!" fi # Check if outputfile is supplied if [[ -z $outputfile ]] ; then err "Please provide output tarball name!" fi # Check if inputfile and output file are the same if [[ $inputfile = $outputfile ]] ; then err "'$inputfile' and '$outputfile' are the same!" fi # Determine what the output filename and the supposed extracted directory # name will be from the $outputfile variable assumeddirname=${outputfile%%.tar.lz} # Extract the tarball provided in inputfile tar xf "$inputfile" if [[ $? -gt 0 ]] ; then err "Failed to extract "'$inputfile'" with errors!" fi if [[ ! -d $assumeddirname ]] ; then if [[ -z $srcdir ]] ; then err "Looks like some custom source directory. Please provide its name" elif [[ -n $srcdir ]] && [[ ! -d $srcdir ]] ; then err "Custom source directory "'$srcdir'" was not found! Exiting." else mv "$srcdir" "$assumeddirname" fi fi newdirname="$assumeddirname" if [[ $verbose = 1 ]] ; then tarlz -cvf "$outputfile" "$newdirname" else tarlz -cf "$outputfile" "$newdirname" fi if [[ $removesourcedir = 1 ]] ; then rm -r $newdirname fi if [[ $removeinputfile = 1 ]] ; then rm $inputfile fi if [[ -f smbuild ]] ; then bldpkg -g fi