"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-lzma.in" (28 Mar 2021, 6897 Bytes) of package /linux/privat/littleutils-1.2.4.tar.lz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "to-lzma.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # to-lzma: Converts .Z, .lzo, .zip, .rar, .gz, .bz2, and .zst files to lzma
    5 # format.  Version using lzma (with gzip-style options).
    6 
    7 # Copyright (C) 2006-2021 by Brian Lindholm.  This file is part of the
    8 # littleutils utility set.
    9 #
   10 # The to-lzma utility is free software; you can redistribute it and/or modify
   11 # it under the terms of the GNU General Public License as published by the Free
   12 # Software Foundation; either version 3, or (at your option) any later version.
   13 #
   14 # The to-lzma utility is distributed in the hope that it will be useful, but
   15 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   16 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   17 # more details.
   18 #
   19 # You should have received a copy of the GNU General Public License along with
   20 # the littleutils.  If not, see <https://www.gnu.org/licenses/>.
   21 
   22 # get command-line options
   23 DELORIG='n'
   24 MAXCOMP='n'
   25 OVERWRITE='y'
   26 PREFER=''
   27 RETAINTIME='n'
   28 declare -i THREADS=1
   29 while getopts 46dhkmnp:t opts
   30 do
   31   case $opts in
   32     4) PREFER='--prefer-family=IPv4' ;;
   33     6) PREFER='--prefer-family=IPv6' ;;
   34     d) DELORIG='y' ;;
   35     h) echo 'to-lzma LU_VERSION'
   36        echo 'usage: to-lzma [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   37        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   38        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   39        exit 0 ;;
   40     k) DELORIG='small' ;;
   41     m) MAXCOMP='y' ;;
   42     n) OVERWRITE='n' ;;
   43     p) THREADS=$OPTARG ;;
   44     t) RETAINTIME='y' ;;
   45     *) echo 'to-lzma LU_VERSION'
   46        echo 'usage: to-lzma [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   47        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   48        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   49        exit 1 ;;
   50   esac
   51 done
   52 shift $((${OPTIND}-1))
   53 if [ $THREADS -eq 1 ]; then
   54   if [ "$MAXCOMP" = 'y' ]; then
   55     LZMA_OPTS='-9'
   56   else
   57     LZMA_OPTS='-6'
   58   fi
   59 else
   60   if [ "$MAXCOMP" = 'y' ]; then
   61     LZMA_OPTS="-T $THREADS -9"
   62   else
   63     LZMA_OPTS="-T $THREADS -6"
   64   fi
   65 fi
   66 
   67 # set up traps
   68 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   69 
   70 # run through files
   71 declare -i S0=0 S1=0
   72 while [ $# -gt 0 ]; do
   73 
   74   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   75   NAME="$1"
   76   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   77     wget $PREFER -q "$NAME"
   78     NAME="${NAME##*/}"
   79     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   80       echo "${NAME} failed to download!"
   81       shift; continue
   82     fi
   83   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   84     echo "${NAME} is not a readable file!"
   85     shift; continue
   86   fi
   87   echo -n "${NAME}... "
   88 
   89   # determine filepath and check for writeability
   90   FILENAME="${NAME##*/}"
   91   FILEPATH="${NAME%${FILENAME}}"
   92   FILEPATH="${FILEPATH%/}"
   93   if [ "X$FILEPATH" = 'X' ]; then
   94     FILEPATH='.'
   95   fi
   96   if [ ! -w "$FILEPATH" ]; then
   97     echo 'failed because target directory is non-writeable'
   98     shift; continue
   99   fi
  100 
  101   # determine new name and check for availability
  102   if  [ "${NAME#${NAME%.bz2}}" = '.bz2' ]; then
  103     NEWNAME="${NAME%.bz2}.lzma"
  104   elif [ "${NAME#${NAME%.tbz}}" = '.tbz' ]; then
  105     NEWNAME="${NAME%.tbz}.tlz"
  106   elif  [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
  107     NEWNAME="${NAME%.gz}.lzma"
  108   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
  109     NEWNAME="${NAME%.tgz}.tlz"
  110   elif  [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  111     NEWNAME="${NAME%.lzo}.lzma"
  112   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  113     NEWNAME="${NAME%.rar}.tar.lzma"
  114   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  115     NEWNAME="${NAME%.RAR}.tar.lzma"
  116   elif  [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  117     NEWNAME="${NAME%.Z}.lzma"
  118   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  119     NEWNAME="${NAME%.zip}.tar.lzma"
  120   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  121     NEWNAME="${NAME%.ZIP}.tar.lzma"
  122   elif  [ "${NAME#${NAME%.zst}}" = '.zst' ]; then
  123     NEWNAME="${NAME%.zst}.lzma"
  124   else
  125     NEWNAME="${NAME}.lzma"
  126   fi
  127   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  128     echo 'skipped because target already exists'
  129     shift; continue
  130   fi
  131 
  132   # recompress based on filetype
  133   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  134   TMPFILE=$(tempname -d "$FILEPATH" .to-lzma_$$) || exit 99
  135   if [ "X$FILETYPE" = 'Xlzma' ]; then
  136     echo 'already converted to lzma'
  137     rm -f $TMPFILE
  138     shift; continue
  139   elif [ "X$FILETYPE" = 'Xbzip' ]; then
  140     bzip2 -c -d -q "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  141     if [ $? -ne 0 ]; then
  142       echo 'failed on bzip2-to-lzma conversion'
  143       rm -f $TMPFILE
  144       shift; continue
  145     fi
  146   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  147     gzip -c -d -q "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  148     if [ $? -ne 0 ]; then
  149       echo 'failed on gzip-to-lzma conversion'
  150       rm -f $TMPFILE
  151       shift; continue
  152     fi
  153   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  154     lzop -c -d -q "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  155     if [ $? -ne 0 ]; then
  156       echo 'failed on lzop-to-lzma conversion'
  157       rm -f $TMPFILE
  158       shift; continue
  159     fi
  160   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  161     gzip -c -d -q "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  162     if [ $? -ne 0 ]; then
  163       echo 'failed on compress-to-lzma conversion'
  164       rm -f $TMPFILE
  165       shift; continue
  166     fi
  167   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  168     rar2tarcat "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  169     if [ $? -ne 0 ]; then
  170       echo 'failed on rar-to-lzma conversion'
  171       rm -f $TMPFILE
  172       shift; continue
  173     fi
  174   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  175     zip2tarcat "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  176     if [ $? -ne 0 ]; then
  177       echo 'failed on zip-to-lzma conversion'
  178       rm -f $TMPFILE
  179       shift; continue
  180     fi
  181   elif [ "X$FILETYPE" = 'Xzsta' ]; then
  182     zstd -c -d -q "$NAME" | PROGLZMA -q $LZMA_OPTS > $TMPFILE
  183     if [ $? -ne 0 ]; then
  184       echo 'failed on zst-to-lzma conversion'
  185       rm -f $TMPFILE
  186       shift; continue
  187     fi
  188   else
  189     echo 'skipped on unrecognized filetype'
  190     rm -f $TMPFILE
  191     shift; continue
  192   fi
  193 
  194   # perform the final move, deleting old or new if necessary
  195   chmod --reference="$NAME" $TMPFILE
  196   mv $TMPFILE "$NEWNAME"
  197   if [ "$RETAINTIME" = 'y' ]; then
  198     touch --reference="$NAME" "$NEWNAME"
  199   fi
  200   S0=$(filesize "$NAME")
  201   S1=$(filesize "$NEWNAME")
  202   if [ $S1 -lt $S0 ]; then
  203     if [ "$DELORIG" != 'n' ]; then
  204       rm -f "$NAME"
  205       echo "$S0 vs. $S1 (deleting original)"
  206     else
  207       echo "$S0 vs. $S1"
  208     fi
  209   else
  210     if [ "$DELORIG" = 'n' ]; then
  211       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  212     elif [ "$DELORIG" = 'y' ]; then
  213       rm -f "$NAME"
  214       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  215     else
  216       rm -f "$NEWNAME"
  217       echo 'new file deleted (not smaller than old)'
  218     fi
  219   fi
  220 
  221   shift
  222 done