#!/usr/bin/env bash

if ! [ -f ./tool/ventoy_lib.sh ]; then
    if [ -f ${0%Ventoy2Disk.sh}/tool/ventoy_lib.sh ]; then
        cd ${0%Ventoy2Disk.sh}    
    fi
fi

if [ -f ./ventoy/version ]; then
    curver=$(cat ./ventoy/version) 
fi

if uname -m | grep -E -q 'aarch64|arm64'; then
    export TOOLDIR=aarch64
elif uname -m | grep -E -q 'x86_64|amd64'; then
    export TOOLDIR=x86_64
elif uname -m | grep -E -q 'mips64'; then
    export TOOLDIR=mips64el
else
    export TOOLDIR=i386
fi
export PATH="$(pwd)/tool/$TOOLDIR:$PATH"


echo ''
echo '**********************************************'
echo "      Ventoy: $curver  $TOOLDIR"
echo "      longpanda admin@ventoy.net"
echo "      https://www.ventoy.net"
echo '**********************************************'
echo ''


#use static linked mkexfatfs for musl-libc environment
if [ -f mkexfatfs_static ]; then
    if ldd --version 2>&1 | grep -qi musl; then
        mv mkexfatfs mkexfatfs_shared
        mv mkexfatfs_static mkexfatfs
    else
        if ./mkexfatfs -V > /dev/null 2>&1; then
            echo "mkexfatfs can not run, check static version" >> /var/log/ventoy.log
        else
            if ./mkexfatfs_static -V > /dev/null 2>&1; then
                echo "Use static version of mkexfatfs" >> /var/log/ventoy.log
                mv mkexfatfs mkexfatfs_shared
                mv mkexfatfs_static mkexfatfs
            fi
        fi
    fi
fi

./tool/VentoyWorker.sh $*
