"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-lzip.in" (28 Mar 2021, 7743 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-lzip.in": 1.2.3_vs_1.2.4.

    1 #! PROGBASH
    2 # vim: set filetype=bash:
    3 
    4 # to-lzip: Converts .Z, .lzo, .zip, .rar, .gz, .bz2, .zst, and .lzma files to
    5 # lz format.
    6 
    7 # Copyright (C) 2009-2021 by Brian Lindholm.  This file is part of the
    8 # littleutils utility set.
    9 #
   10 # The to-lzip 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-lzip 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 LZMAUTIL='PROGLZMA'
   25 MAXCOMP='n'
   26 OVERWRITE='y'
   27 PREFER=''
   28 RETAINTIME='n'
   29 declare -i THREADS=1
   30 while getopts 46dhkmnp:t opts
   31 do
   32   case $opts in
   33     4) PREFER='--prefer-family=IPv4' ;;
   34     6) PREFER='--prefer-family=IPv6' ;;
   35     d) DELORIG='y' ;;
   36     h) echo 'to-lzip LU_VERSION'
   37        echo 'usage: to-lzip [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   38        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   39        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   40        exit 0 ;;
   41     k) DELORIG='small' ;;
   42     m) MAXCOMP='y' ;;
   43     n) OVERWRITE='n' ;;
   44     p) THREADS=$OPTARG ;;
   45     t) RETAINTIME='y' ;;
   46     *) echo 'to-lzip LU_VERSION'
   47        echo 'usage: to-lzip [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   48        echo '               [-m(ax_compression)] [-n(o_overwrite)] [-p threads]'
   49        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   50        exit 1 ;;
   51   esac
   52 done
   53 shift $((${OPTIND}-1))
   54 if [ "$MAXCOMP" = 'y' ]; then
   55   LZIP_OPTS='-9'
   56 else
   57   LZIP_OPTS='-6'
   58 fi
   59 if [ $THREADS -eq 1 ]; then
   60   ENGINE='lzip'
   61 else
   62   command -v plzip > /dev/null
   63   if [ $? -eq 0 ]; then
   64     if [ $THREADS -eq 0 ]; then
   65       if [ -r /proc/cpuinfo ]; then
   66         NCORE=$(grep -c '^processor' /proc/cpuinfo)
   67       else
   68         NCORE=$(nproc)
   69       fi
   70       ENGINE="plzip -n $NCORE"
   71     else
   72       ENGINE="plzip -n $THREADS"
   73     fi
   74   else
   75     echo 'to-lzip warning: plzip not found; reverting to lzip'
   76     ENGINE='lzip'
   77   fi
   78 fi
   79 
   80 # set up traps
   81 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   82 
   83 # run through files
   84 declare -i S0=0 S1=0
   85 while [ $# -gt 0 ]; do
   86 
   87   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   88   NAME="$1"
   89   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   90     wget $PREFER -q "$NAME"
   91     NAME="${NAME##*/}"
   92     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   93       echo "$NAME failed to download!"
   94       shift; continue
   95     fi
   96   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   97     echo "$NAME is not a readable file!"
   98     shift; continue
   99   fi
  100   echo -n "${NAME}... "
  101 
  102   # determine filepath and check for writeability
  103   FILENAME="${NAME##*/}"
  104   FILEPATH="${NAME%${FILENAME}}"
  105   FILEPATH="${FILEPATH%/}"
  106   if [ "X$FILEPATH" = 'X' ]; then
  107     FILEPATH='.'
  108   fi
  109   if [ ! -w "$FILEPATH" ]; then
  110     echo 'failed because target directory is non-writeable'
  111     shift; continue
  112   fi
  113 
  114   # determine new name and check for availability
  115   if  [ "${NAME#${NAME%.bz2}}" = '.bz2' ]; then
  116     NEWNAME="${NAME%.bz2}.lz"
  117   elif [ "${NAME#${NAME%.tbz}}" = '.tbz' ]; then
  118     NEWNAME="${NAME%.tbz}.tlz"
  119   elif  [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
  120     NEWNAME="${NAME%.gz}.lz"
  121   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
  122     NEWNAME="${NAME%.tgz}.tlz"
  123   elif [ "${NAME#${NAME%.lzma}}" = '.lzma' ]; then
  124     NEWNAME="${NAME%.lzma}.lz"
  125   elif  [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  126     NEWNAME="${NAME%.lzo}.lz"
  127   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  128     NEWNAME="${NAME%.rar}.tar.lz"
  129   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  130     NEWNAME="${NAME%.RAR}.tar.lz"
  131   elif  [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  132     NEWNAME="${NAME%.Z}.lz"
  133   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  134     NEWNAME="${NAME%.zip}.tar.lz"
  135   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  136     NEWNAME="${NAME%.ZIP}.tar.lz"
  137   elif  [ "${NAME#${NAME%.zst}}" = '.zst' ]; then
  138     NEWNAME="${NAME%.zst}.lz"
  139   else
  140     NEWNAME="${NAME}.lz"
  141   fi
  142   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  143     echo 'skipped because target already exists'
  144     shift; continue
  145   fi
  146 
  147   # recompress based on filetype
  148   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  149   # file 5.02 and earlier cannot detect lzma, listing "data" instead
  150   if [ "$FILETYPE" = 'data' -a "${NAME#${NAME%.lzma}}" = '.lzma' ]; then
  151     FILETYPE='lzma'
  152   fi
  153   TMPFILE=$(tempname -d "$FILEPATH" .to-lzip_$$) || exit 99
  154   if [ "X$FILETYPE" = 'Xlzip' ]; then
  155     echo 'already converted to lz'
  156     rm -f $TMPFILE
  157     shift; continue
  158   elif [ "X$FILETYPE" = 'Xbzip' ]; then
  159     bzip2 -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  160     if [ $? -ne 0 ]; then
  161       echo 'failed on bzip2-to-lzip conversion'
  162       rm -f $TMPFILE
  163       shift; continue
  164     fi
  165   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  166     gzip -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  167     if [ $? -ne 0 ]; then
  168       echo 'failed on gzip-to-lzip conversion'
  169       rm -f $TMPFILE
  170       shift; continue
  171     fi
  172   elif [ "X$FILETYPE" = 'Xlzma' ]; then
  173     if [ "X$LZMAUTIL" = 'Xlzma_alone' ]; then
  174       lzma_alone d "$NAME" -so | $ENGINE -q $LZIP_OPTS > $TMPFILE
  175     else
  176       lzma -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  177     fi
  178     if [ $? -ne 0 ]; then
  179       echo 'failed on lzma-to-lzip conversion'
  180       rm -f $TMPFILE
  181     else
  182       CONVERTED='y'
  183     fi
  184   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  185     lzop -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  186     if [ $? -ne 0 ]; then
  187       echo 'failed on lzop-to-lzip conversion'
  188       rm -f $TMPFILE
  189       shift; continue
  190     fi
  191   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  192     gzip -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  193     if [ $? -ne 0 ]; then
  194       echo 'failed on compress-to-lzip conversion'
  195       rm -f $TMPFILE
  196       shift; continue
  197     fi
  198   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  199     rar2tarcat "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  200     if [ $? -ne 0 ]; then
  201       echo 'failed on rar-to-lzip conversion'
  202       rm -f $TMPFILE
  203       shift; continue
  204     fi
  205   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  206     zip2tarcat "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  207     if [ $? -ne 0 ]; then
  208       echo 'failed on zip-to-lzip conversion'
  209       rm -f $TMPFILE
  210       shift; continue
  211     fi
  212   elif [ "X$FILETYPE" = 'Xzsta' ]; then
  213     zstd -c -d -q "$NAME" | $ENGINE -q $LZIP_OPTS > $TMPFILE
  214     if [ $? -ne 0 ]; then
  215       echo 'failed on zst-to-lzip conversion'
  216       rm -f $TMPFILE
  217       shift; continue
  218     fi
  219   else
  220     echo 'skipped on unrecognized filetype'
  221     rm -f $TMPFILE
  222     shift; continue
  223   fi
  224 
  225   # perform the final move, deleting old or new if necessary
  226   chmod --reference="$NAME" $TMPFILE
  227   mv $TMPFILE "$NEWNAME"
  228   if [ "$RETAINTIME" = 'y' ]; then
  229     touch --reference="$NAME" "$NEWNAME"
  230   fi
  231   S0=$(filesize "$NAME")
  232   S1=$(filesize "$NEWNAME")
  233   if [ $S1 -lt $S0 ]; then
  234     if [ "$DELORIG" != 'n' ]; then
  235       rm -f "$NAME"
  236       echo "$S0 vs. $S1 (deleting original)"
  237     else
  238       echo "$S0 vs. $S1"
  239     fi
  240   else
  241     if [ "$DELORIG" = 'n' ]; then
  242       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  243     elif [ "$DELORIG" = 'y' ]; then
  244       rm -f "$NAME"
  245       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  246     else
  247       rm -f "$NEWNAME"
  248       echo 'new file deleted (not smaller than old)'
  249     fi
  250   fi
  251 
  252   shift
  253 done