"Fossies" - the Fresh Open Source Software Archive

Member "littleutils-1.2.4/scripts/to-bzip.in" (28 Mar 2021, 6325 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-bzip: Converts .Z, .zip, .rar, .gz, and .lzo files to bzip2 format.
    5 
    6 # Copyright (C) 2006-2021 by Brian Lindholm.  This file is part of the
    7 # littleutils utility set.
    8 #
    9 # The to-bzip 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-bzip 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-bzip LU_VERSION'
   34        echo 'usage: to-bzip [-(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-bzip LU_VERSION'
   43        echo 'usage: to-bzip [-(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='bzip2'
   52 else
   53   command -v lbzip2 > /dev/null
   54   if [ $? -eq 0 ]; then
   55     if [ $THREADS -eq 0 ]; then
   56       ENGINE='lbzip2'
   57     else
   58       ENGINE="lbzip2 -n $THREADS"
   59     fi
   60   else
   61     command -v pbzip2 > /dev/null
   62     if [ $? -eq 0 ]; then
   63       if [ $THREADS -eq 0 ]; then
   64         ENGINE='pbzip2'
   65       else
   66         ENGINE="pbzip2 -p${THREADS}"
   67       fi
   68     else
   69       echo 'to-bzip2 warning: neither lbzip2 nor pbzip2 found; reverting to bzip2'
   70       ENGINE='bzip2'
   71     fi
   72   fi
   73 fi
   74 
   75 # set up traps
   76 trap 'rm -f $TMPFILE ; exit 1' 1 2 3 13 15
   77 
   78 # run through files
   79 declare -i S0=0 S1=0
   80 while [ $# -gt 0 ]; do
   81 
   82   # if file is actually a URL, attempt to download, otherwise verify that it already exists
   83   NAME="$1"
   84   if [ "${NAME:0:8}" = 'https://' -o "${NAME:0:7}" = 'http://' -o "${NAME:0:6}" = 'ftp://' ]; then
   85     wget $PREFER -q "$NAME"
   86     NAME="${NAME##*/}"
   87     if [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   88       echo "$NAME failed to download!"
   89       shift; continue
   90     fi
   91   elif [ ! -f "$NAME" -o ! -r "$NAME" ]; then
   92     echo "$NAME is not a readable file!"
   93     shift; continue
   94   fi
   95   echo -n "${NAME}... "
   96 
   97   # determine filepath and check for writeability
   98   FILENAME="${NAME##*/}"
   99   FILEPATH="${NAME%${FILENAME}}"
  100   FILEPATH="${FILEPATH%/}"
  101   if [ "X$FILEPATH" = 'X' ]; then
  102     FILEPATH='.'
  103   fi
  104   if [ ! -w "$FILEPATH" ]; then
  105     echo 'failed because target directory is non-writeable'
  106     shift; continue
  107   fi
  108 
  109   # determine new name and check for availability
  110   if  [ "${NAME#${NAME%.gz}}" = '.gz' ]; then
  111     NEWNAME="${NAME%.gz}.bz2"
  112   elif [ "${NAME#${NAME%.tgz}}" = '.tgz' ]; then
  113     NEWNAME="${NAME%.tgz}.tbz"
  114   elif  [ "${NAME#${NAME%.lzo}}" = '.lzo' ]; then
  115     NEWNAME="${NAME%.lzo}.bz2"
  116   elif  [ "${NAME#${NAME%.rar}}" = '.rar' ]; then
  117     NEWNAME="${NAME%.rar}.tar.bz2"
  118   elif  [ "${NAME#${NAME%.RAR}}" = '.RAR' ]; then
  119     NEWNAME="${NAME%.RAR}.tar.bz2"
  120   elif  [ "${NAME#${NAME%.Z}}" = '.Z' ]; then
  121     NEWNAME="${NAME%.Z}.bz2"
  122   elif  [ "${NAME#${NAME%.zip}}" = '.zip' ]; then
  123     NEWNAME="${NAME%.zip}.tar.bz2"
  124   elif  [ "${NAME#${NAME%.ZIP}}" = '.ZIP' ]; then
  125     NEWNAME="${NAME%.ZIP}.tar.bz2"
  126   else
  127     NEWNAME="${NAME}.bz2"
  128   fi
  129   if [ "$OVERWRITE" = 'n' -a -e "$NEWNAME" ]; then
  130     echo 'skipped because target already exists'
  131     shift; continue
  132   fi
  133 
  134   # recompress based on filetype
  135   FILETYPE=$(file -b "$NAME" | cut -b1-4 | tr '[:upper:]' '[:lower:]')
  136   TMPFILE=$(tempname -d "$FILEPATH" .to-bzip_$$) || exit 99
  137   if [ "X$FILETYPE" = 'Xbzip' ]; then
  138     echo 'already converted to bzip2'
  139     rm -f $TMPFILE
  140     shift; continue
  141   elif [ "X$FILETYPE" = 'Xgzip' ]; then
  142     gzip -c -d -q "$NAME" | $ENGINE -c -9 -q > $TMPFILE
  143     if [ $? -ne 0 ]; then
  144       echo 'failed on gzip-to-bzip2 conversion'
  145       rm -f $TMPFILE
  146       shift; continue
  147     fi
  148   elif [ "X$FILETYPE" = 'Xlzop' ]; then
  149     lzop -c -d -q "$NAME" | $ENGINE -c -9 -q > $TMPFILE
  150     if [ $? -ne 0 ]; then
  151       echo 'failed on lzop-to-bzip2 conversion'
  152       rm -f $TMPFILE
  153       shift; continue
  154     fi
  155   elif [ "X$FILETYPE" = 'Xcomp' ]; then
  156     gzip -c -d -q "$NAME" | $ENGINE -c -9 -q > $TMPFILE
  157     if [ $? -ne 0 ]; then
  158       echo 'failed on compress-to-bzip2 conversion'
  159       rm -f $TMPFILE
  160       shift; continue
  161     fi
  162   elif [ "X$FILETYPE" = 'Xrar ' ]; then
  163     rar2tarcat "$NAME" | $ENGINE -c -9 -q > $TMPFILE
  164     if [ $? -ne 0 ]; then
  165       echo 'failed on rar-to-bzip2 conversion'
  166       rm -f $TMPFILE
  167       shift; continue
  168     fi
  169   elif [ "X$FILETYPE" = 'Xzip ' ]; then
  170     zip2tarcat "$NAME" | $ENGINE -c -9 -q > $TMPFILE
  171     if [ $? -ne 0 ]; then
  172       echo 'failed on zip-to-bzip2 conversion'
  173       rm -f $TMPFILE
  174       shift; continue
  175     fi
  176   else
  177     echo 'skipped on unrecognized filetype'
  178     rm -f $TMPFILE
  179     shift; continue
  180   fi
  181 
  182   # perform the final move, deleting old or new if necessary
  183   chmod --reference="$NAME" $TMPFILE
  184   mv $TMPFILE "$NEWNAME"
  185   if [ "$RETAINTIME" = 'y' ]; then
  186     touch --reference="$NAME" "$NEWNAME"
  187   fi
  188   S0=$(filesize "$NAME")
  189   S1=$(filesize "$NEWNAME")
  190   if [ $S1 -lt $S0 ]; then
  191     if [ "$DELORIG" != 'n' ]; then
  192       rm -f "$NAME"
  193       echo "$S0 vs. $S1 (deleting original)"
  194     else
  195       echo "$S0 vs. $S1"
  196     fi
  197   else
  198     if [ "$DELORIG" = 'n' ]; then
  199       echo "$S0 vs. $S1 (warning: new file is not smaller)"
  200     elif [ "$DELORIG" = 'y' ]; then
  201       rm -f "$NAME"
  202       echo "$S0 vs. $S1 (deleting original: new file is not smaller)"
  203     else
  204       rm -f "$NEWNAME"
  205       echo 'new file deleted (not smaller than old)'
  206     fi
  207   fi
  208 
  209   shift
  210 done