#!/usr/bin/env bash

# Usage:       ./generate_compressed_files
#                or
#              bash generate_compressed_files
# Description: (see scriptname!)
# Author:      Patrick Neumann (patrick@neumannsland.de)
# Version:     1.0
# Date:        13.02.2019
# License:     GPL3
# Warranty:    This program is distributed WITHOUT ANY WARRANTY
# Todo:        -

# Notwendige Pakete
DEBS="gzip"
DEBS="${DEBS} bzip2"
DEBS="${DEBS} xz-utils"
DEBS="${DEBS} lzop"
DEBS="${DEBS} zip"
DEBS="${DEBS} unzip"
DEBS="${DEBS} rar"
DEBS="${DEBS} unrar"
DEBS="${DEBS} p7zip-full"

echo "Checking dependencies..."
for deb in ${DEBS} ; do
  if ! dpkg -s "${deb}" 2>&1 >/dev/null ; then
    sudo apt install "${deb}" --assume-yes
  fi
done

# Zielverzeichnis
DIRECTORY="compressed_files"
FILE="file.txt"

echo "Creating target directory..."
mkdir "${DIRECTORY}"
cd "${DIRECTORY}"

# Testdatei
echo "Generating text file..."
cat <<EOF > "${FILE}"
Eins
eins zwei
zWei
zwei drei
drEi
drei vier
vieR
vier eins
EOF

# .txt -> .txt.gz
echo "Generating .gz file..."
gzip --keep "${FILE}"

# Datei mit falscher Dateinamenserweiterung
echo "Generating file with wrong extension..."
cp "${FILE}".gz wrong_extension.txt.bz2

# .txt -> .txt.bz2
echo "Generating .bz2 file..."
bzip2 --keep "${FILE}"

# .txt -> .txt.xz
echo "Generating .xz file..."
xz --keep "${FILE}"
# .txt -> .txt.lzma
echo "Generating .lzma file..."
xz --keep --format=lzma "${FILE}"

# .txt -> .txt.lzop
echo "Generating .lzop file..."
lzop --keep "${FILE}"

# .txt -> .txt.zip
echo "Generating .zip file..."
zip --quiet "${FILE}".zip "${FILE}"

# .txt -> .txt.rar
echo "Generating .rar file..."
rar a -inul "${FILE}".rar "${FILE}"

# .txt -> .txt.7z
echo "Generating .7z file..."
7z a "${FILE}".7z "${FILE}" >/dev/null
# .txt -> .txt.lzma2
echo "Generating .lzma2 file..."
7z a -m0=lzma2 "${FILE}".lzma2 "${FILE}" >/dev/null

# symbolic link
echo "Generating symbolic link..."
ln -s "${FILE}" "./symlink"

echo "Generating directory..."
# directory
mkdir "./directory"

echo "Done."

exit 0