After gemini-propoesed fixes
This commit is contained in:
parent
2e31283b61
commit
83324dd0f7
11 changed files with 228 additions and 307 deletions
133
cdz
133
cdz
|
|
@ -1,118 +1,85 @@
|
|||
#!/usr/bin/env bash
|
||||
# Usage: cdz <archive>
|
||||
|
||||
if [[ $LD_INTERNAL -ne 1 ]];
|
||||
then
|
||||
if [[ $LD_INTERNAL -ne 1 ]]; then
|
||||
LD_INTERNAL=1
|
||||
. $(dirname $(realpath $0))/daisy.source
|
||||
fi
|
||||
|
||||
target=$1
|
||||
|
||||
# Check if file exists
|
||||
if [[ -z "$target" ]];
|
||||
then
|
||||
if [[ -z "$target" ]]; then
|
||||
echo "No target specified."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! test -f "$target";
|
||||
then
|
||||
if [[ ! -f "$target" ]]; then
|
||||
echo "File not found: \"$target\""
|
||||
exit 2
|
||||
fi
|
||||
|
||||
# Check if archivemount is present
|
||||
which archivemount 1>/dev/null 2>/dev/null
|
||||
hasmounter=$?
|
||||
target_abs=$(realpath "$target")
|
||||
name=$(basename "$target")
|
||||
|
||||
file "$target" 1>/dev/null
|
||||
exitcode=$?
|
||||
report=$(file "$target")
|
||||
name=$(echo $@ | sed 's/:.*//' | sed 's|.*/||')
|
||||
has_archivemount=$(command -v archivemount >/dev/null 2>&1; echo $?)
|
||||
|
||||
if [[ $has_archivemount -eq 0 && $NO_ARCHIVEMOUNT -ne 1 ]]; then
|
||||
use_mounter=1
|
||||
else
|
||||
use_mounter=0
|
||||
fi
|
||||
|
||||
mime_type=$(file --mime-type -b "$target")
|
||||
|
||||
# Check for archive type, supported types are zip/tar/rar
|
||||
comm1=(:)
|
||||
comm2=(echo "Unsupported archive type$add: \"$target\"")
|
||||
comm2=(echo "Unsupported archive type: $mime_type")
|
||||
comm3=(:)
|
||||
comm4=(:)
|
||||
comm5=(:)
|
||||
|
||||
echo $report | grep "tar archive" 1>/dev/null
|
||||
istar=$?
|
||||
echo $report | grep "Zip archive" 1>/dev/null
|
||||
iszip=$?
|
||||
echo $report | grep "Android" 1>/dev/null
|
||||
iszip=$?
|
||||
echo $report | grep "RAR archive" 1>/dev/null
|
||||
israr=$?
|
||||
|
||||
# TAR archives come in many forms, if none of our tests say it's tar
|
||||
# ...but it looks like tar and barks like tar, let's take the shot.
|
||||
# Seems to work fairly well for the record.
|
||||
res=$(echo "$target" | grep ".tar")
|
||||
if [[ $res != "" ]];
|
||||
then
|
||||
istar=0
|
||||
if [[ $use_mounter -eq 1 ]]; then
|
||||
echo "Using archivemount..."
|
||||
comm2=(archivemount -o allow_root,use_ino "$target_abs")
|
||||
comm4=(cd ..)
|
||||
comm5=(umount)
|
||||
elif [[ $mime_type == *"tar"* || $target == *.tar* ]]; then
|
||||
comm2=(tar xvf "$target_abs" -C)
|
||||
elif [[ $mime_type == "application/zip" ]]; then
|
||||
if command -v unzip >/dev/null 2>&1; then
|
||||
comm2=(unzip -q "$target_abs" -d)
|
||||
else
|
||||
comm1=(echo "unzip is missing")
|
||||
comm3=(exit 1)
|
||||
fi
|
||||
elif [[ $mime_type == "application/x-rar" ]]; then
|
||||
if command -v unrar >/dev/null 2>&1; then
|
||||
# unrar needs the directory as the last argument
|
||||
comm2=(unrar x -idq "$target_abs")
|
||||
else
|
||||
comm1=(echo "unrar is missing")
|
||||
comm3=(exit 1)
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ $NO_ARCHIVEMOUNT -eq 1 ]]; then
|
||||
hasmounter=1
|
||||
fi
|
||||
|
||||
if (( $hasmounter == 0 )); then
|
||||
echo "We have \`archivemount\`, so we'll use that!"
|
||||
echo "If you'd prefer we not use it, please specify NO_ARCHIVEMOUNT=1"
|
||||
istar=1
|
||||
iszip=1
|
||||
israr=1
|
||||
fi
|
||||
|
||||
# Now we set the right command
|
||||
if (( $istar == 0 )); then
|
||||
comm2=(tar xvf "$target" -C)
|
||||
elif (( $iszip == 0 )); then
|
||||
which unzip 1>/dev/null
|
||||
exitcode=$?
|
||||
if (( $exitcode == 0 )); then
|
||||
comm2=(unzip -q "$target" -d)
|
||||
else
|
||||
comm1=(echo "The utility 'unzip' is missing, please install it")
|
||||
comm3=(exit 1)
|
||||
fi
|
||||
elif (( $israr == 0 )); then
|
||||
which unrar 1>/dev/null
|
||||
exitcode=$?
|
||||
if (( exitcode == 0 )); then
|
||||
comm2=(unrar -i nul "$target")
|
||||
else
|
||||
comm1=(echo "The utility 'unrar' is missing, please install it")
|
||||
comm3=(exit 1)
|
||||
fi
|
||||
elif (( $hasmounter == 0 )); then
|
||||
comm2=(archivemount -o allow_root -o use_ino "$target")
|
||||
comm4=(cd ..)
|
||||
comm5=(umount)
|
||||
fi
|
||||
|
||||
# Create the temp dir, usually
|
||||
dir=$(mktemp -d /tmp/extracted.XXXXXXXX)
|
||||
|
||||
# And the rest of the commands
|
||||
"${comm1[@]}"
|
||||
"${comm2[@]}" $dir
|
||||
if [[ ${comm2[0]} == "unrar" ]]; then
|
||||
"${comm2[@]}" "$dir"
|
||||
else
|
||||
"${comm2[@]}" "$dir"
|
||||
fi
|
||||
"${comm3[@]}"
|
||||
|
||||
currentpath=$(pwd)
|
||||
sym="$currentpath/$name.tmp"
|
||||
ln -f -s "$dir" "$sym"
|
||||
sym="$(pwd)/$name.tmp"
|
||||
ln -sf "$dir" "$sym"
|
||||
cd "$sym"
|
||||
|
||||
echo "A symlink has been made under the name \"$sym\"."
|
||||
echo "This symlink points to the data directory \"$dir\"."
|
||||
echo "Extracted to $dir (linked via $sym)"
|
||||
echo "Type exit to finish."
|
||||
|
||||
echo "Type 'exit' to exit the extracted archive's folder and auto-delete it."
|
||||
eval $SHELL
|
||||
|
||||
"${comm4[@]}"
|
||||
"${comm5[@]}" $dir
|
||||
rm -rf $dir
|
||||
"${comm5[@]}" "$dir"
|
||||
rm -rf "$dir"
|
||||
rm "$sym"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue