"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-lzma_alone.in" (28 Mar 2021, 6802 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.

    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_alone (with 7zip-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 while getopts 46dhkmnp:t opts
   29 do
   30   case $opts in
   31     4) PREFER='--prefer-family=IPv4' ;;
   32     6) PREFER='--prefer-family=IPv6' ;;
   33     d) DELORIG='y' ;;
   34     h) echo 'to-lzma LU_VERSION'
   35        echo 'usage: to-lzma [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   36        echo '               [-m(ax_compression)] [-n(o_overwrite)]'
   37        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   38        exit 0 ;;
   39     k) DELORIG='small' ;;
   40     m) MAXCOMP='y' ;;
   41     n) OVERWRITE='n' ;;
   42     t) RETAINTIME='y' ;;
   43     *) echo 'to-lzma LU_VERSION'
   44        echo 'usage: to-lzma [-(IPv)4] [-(IPv)6] [-d(el_orig)] [-k(eep_smallest)]'
   45        echo '               [-m(ax_compression)] [-n(o_overwrite)]'
   46        echo '               [-t(imestamp retain)] [-h(elp)] filename ...'
   47        exit 1 ;;
   48   esac
   49 done
   50 shift $((${OPTIND}-1))
   51 if [ "$MAXCOMP" = 'y' ]; then
   52   LZMA_OPTS='-eos -si -so -a1 -d26 -fb256 -mfbt4'
   53 else
   54   LZMA_OPTS='-eos -si -so -a1 -d24 -fb128 -mfbt4'
   55 fi
   56 
   57 # set up traps
   58 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   59 
   60 # run through files
   61 declare -i S0=0 S1=0
   62 while [ $# -gt 0 ]; do
   63 
   64   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   65   NAME="$1"
   66   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   67     wget $PREFER -q "$NAME"
   68     NAME="${NAME##*/}"
   69     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   70       echo "${NAME} failed to download!"
   71       shift; continue
   72     fi
   73   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   74     echo "${NAME} is not a readable file!"
   75     shift; continue
   76   fi
   77   echo -n "${NAME}... "
   78 
   79   # determine filepath and check for writeability
   80   FILENAME="${NAME##*/}"
   81   FILEPATH="${NAME%${FILENAME}}"
   82   FILEPATH="${FILEPATH%/}"
   83   if [ "X$FILEPATH" = 'X' ]; then
   84     FILEPATH='.'
   85   fi
   86   if [ ! -w "$FILEPATH" ]; then
   87     echo 'failed because target directory is non-writeable'
   88     shift; continue
   89   fi
   90 
   91   # determine new name and check for availability
   92   if  [ "${NAME#${NAME%.bz2}}" = '.bz2' ]; then
   93     NEWNAME="${NAME%.bz2}.lzma"
   94   elif [ "${NAME#${NAME%.tbz}}" = '.tbz' ]; then
   95     NEWNAME="${NAME%.tbz}.tlz"
   96   elif  [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
   97     NEWNAME="${NAME%.gz}.lzma"
   98   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
   99     NEWNAME="${NAME%.tgz}.tlz"
  100   elif  [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  101     NEWNAME="${NAME%.lzo}.lzma"
  102   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  103     NEWNAME="${NAME%.rar}.tar.lzma"
  104   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  105     NEWNAME="${NAME%.RAR}.tar.lzma"
  106   elif  [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  107     NEWNAME="${NAME%.Z}.lzma"
  108   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  109     NEWNAME="${NAME%.zip}.tar.lzma"
  110   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  111     NEWNAME="${NAME%.ZIP}.tar.lzma"
  112   elif  [ "${NAME#${NAME%.zst}}" = '.zst' ]; then
  113     NEWNAME="${NAME%.zst}.lzma"
  114   else
  115     NEWNAME="${NAME}.lzma"
  116   fi
  117   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  118     echo 'skipped because target already exists'
  119     shift; continue
  120   fi
  121 
  122   # recompress based on filetype
  123   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  124   TMPFILE=$(tempname -d "$FILEPATH" .to-lzma_$$) || exit 99
  125   if [ "X$FILETYPE" = 'Xlzma' ]; then
  126     echo 'already converted to lzma'
  127     rm -f $TMPFILE
  128     shift; continue
  129   elif [ "X$FILETYPE" = 'Xbzip' ]; then
  130     bzip2 -c -d -q "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE 2> /dev/null
  131     if [ $? -ne 0 ]; then
  132       echo 'failed on bzip2-to-lzma conversion'
  133       rm -f $TMPFILE
  134       shift; continue
  135     fi
  136   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  137     gzip -c -d -q "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE 2> /dev/null
  138     if [ $? -ne 0 ]; then
  139       echo 'failed on gzip-to-lzma conversion'
  140       rm -f $TMPFILE
  141       shift; continue
  142     fi
  143   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  144     lzop -c -d -q "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE 2> /dev/null
  145     if [ $? -ne 0 ]; then
  146       echo 'failed on lzop-to-lzma conversion'
  147       rm -f $TMPFILE
  148       shift; continue
  149     fi
  150   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  151     gzip -c -d -q "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE 2> /dev/null
  152     if [ $? -ne 0 ]; then
  153       echo 'failed on compress-to-lzma conversion'
  154       rm -f $TMPFILE
  155       shift; continue
  156     fi
  157   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  158     rar2tarcat "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE
  159     if [ $? -ne 0 ]; then
  160       echo 'failed on rar-to-lzma conversion'
  161       rm -f $TMPFILE
  162       shift; continue
  163     fi
  164   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  165     zip2tarcat "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE
  166     if [ $? -ne 0 ]; then
  167       echo 'failed on zip-to-lzma conversion'
  168       rm -f $TMPFILE
  169       shift; continue
  170     fi
  171   elif [ "X$FILETYPE" = 'Xzsta' ]; then
  172     zstd -c -d -q "$NAME" | PROGLZMA e $LZMA_OPTS > $TMPFILE 2> /dev/null
  173     if [ $? -ne 0 ]; then
  174       echo 'failed on zst-to-lzma conversion'
  175       rm -f $TMPFILE
  176       shift; continue
  177     fi
  178   else
  179     echo 'skipped on unrecognized filetype'
  180     rm -f $TMPFILE
  181     shift; continue
  182   fi
  183 
  184   # perform the final move, deleting old or new if necessary
  185   chmod --reference="$NAME" $TMPFILE
  186   mv $TMPFILE "$NEWNAME"
  187   if [ "$RETAINTIME" = 'y' ]; then
  188     touch --reference="$NAME" "$NEWNAME"
  189   fi
  190   S0=$(filesize "$NAME")
  191   S1=$(filesize "$NEWNAME")
  192   if [ $S1 -lt $S0 ]; then
  193     if [ "$DELORIG" != 'n' ]; then
  194       rm -f "$NAME"
  195       echo "$S0 vs. $S1 (deleting original)"
  196     else
  197       echo "$S0 vs. $S1"
  198     fi
  199   else
  200     if [ "$DELORIG" = 'n' ]; then
  201       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  202     elif [ "$DELORIG" = 'y' ]; then
  203       rm -f "$NAME"
  204       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  205     else
  206       rm -f "$NEWNAME"
  207       echo 'new file deleted (not smaller than old)'
  208     fi
  209   fi
  210 
  211   shift
  212 done