"Fossies" - the Fresh Open Source Software Archive

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