#!/bin/bash

# Get user inputs
get_inputs() {
    echo "Enter the output directory for the DMG file: " 
    read -r output_dir
    output_dir=$(echo "$output_dir"| sed "s/'//g")
    echo "Enter the path to the .pkg file: " 
    read -r pkg_file
    pkg_file=$(echo "$pkg_file"| sed "s/'//g")

    # Validate inputs
    if [ ! -d "$output_dir" ]; then
        echo "Error: Output directory $output_dir does not exist."
        exit 1
    fi
    if [ ! -f "$pkg_file" ]; then
        echo "Error: .pkg file $pkg_file does not exist."
        exit 1
    fi

    dmg_path="$output_dir/InstallMacOS.dmg"
    mount_point="/mnt/apfs_dmg"
    extract_dir="/tmp/pkg_extracted"
}

# Create a blank 16GB DMG file
create_blank_dmg() {
    echo "Creating a 16GB DMG file at $dmg_path..."
    dd if=/dev/zero of=$dmg_path bs=1G count=16384
}

# Format the DMG with HFS+
format_dmg_apfs() {
    echo "Formatting with APFS..."
    mkfs.apfs -v macOS_Installer $dmg_path
}

# Extract .pkg file
extract_pkg() {
    echo "Extracting .pkg..."
    mkdir -p $extract_dir
    7z x -mmt$(nproc --all) -txar $pkg_file -o$extract_dir SharedSupport.dmg Payload
}

# Mount the DMG file
mount_dmg() {
    echo "Mounting $dmg_path at $mount_point..."
    sudo mkdir -p $mount_point
    sudo mount -o loop $dmg_path $mount_point
}

# Copy files to the mounted DMG
copy_to_dmg() {
    echo "Copying files...(May take a while!)"
    sudo rsync -az $extract_dir/* $mount_point/
}

# Call pbzx.py to decompress the payload
decompress_payload() {
    echo "Decompressing Payload..."
    rsync -az $(dirname "$0")/pbzx.py $mount_point    
    cd $mount_point    
    python3 $mount_point/pbzx.py -n Payload | cpio -idmu
    mv Applications/* $mount_point/
    rmdir ./Applications
    rm Payload
    rm pbzx.py
    cd ./Install\ */Contents/
    mkdir SharedSupport
    mv $mount_point/SharedSupport.dmg $mount_point/Install\ */Contents/SharedSupport/
}

# Unmount the DMG file
unmount_dmg() {
    cd /tmp
    echo "Unmounting..."
    sudo umount $mount_point
}

# Clean up all the temporary files
cleanup() {
    echo "Cleaning up..."
    rm -rf "$extract_dir"
    echo "Process completed successfully."
}

display_art(){
    clear
    printf '\e[8;46;101t'  
    cat << 'EOF'
##++%%#,,+##%+#+%#%++:%%%%##%#:++...................................................................
:+##%%+#:.:+####++##%%+%++%%###+:,..................................................................
#%::#%%%#+,+%%%##%%+%###%###%%#+::,,,...............................................................
####:+%%%##+%####++:.:%%%####%:::.,::...............................................................
#####%+#####+%####%++++%+:+%##%,,,.:+...............................................................
######%###############%++:,.,%#%,,.++,.,,...........................................................
%%%#############%:,+%##++:+:,.%%:+.,:+::............................................................
%+:+++########%+::::%%,,:::::::#++:.,::::,..........................................................
:+#+:::%#####+,.,,+%#:,++++++:+%::,......,,.........................................................
,,:#+:,:%#%+:,:+,:%#+,:%:..,,:::.,,,......,:........................................................
,,,:#+,:+#%::%+%%%##:,+%:....++#+:,,........+:......................................................
:,,::%+,+###%:,,:+#%+++%%,....:#%+,,+++,......+.....................................................
##%:++#+:+%:,,,.,+#+++%##.::..,++:,::,,:%:.....:....................................................
####%%%##%++,.,,,:%%:+###,,+++....,:,,...:%++:,:,...................................................
+#######+.,++,,..,%#:,+%##+:#%.,:,,+,,+:,,+###%+::,..............|| Developed by Kartik Joshi ||....
,::+#%:%+,,.,:+%%##:,:%####%#%,+:......,####,,....,::.......................... | ..................
:::+++,:#+..,+%###+::+::::+##%:.......,.:###+........:......................... | ..................
%%%++:::%++++%####%%%:::,,,%#%%:......,:,,,,:,::..,::+::,...................... | ..................
#####++:::++++%#######+%%++#%+:#+..:...:+:...,:,.,...,..,:..................... | ..................
%++%%%+++:...,:%:.%##+,+++%%%,,:::.:,.......,..,,..,,,::..,,................... | ..................
::+:,:%+:...,+%:.,+##,,,,,+++,,:%:..+,....,...,...,,..,+,..,,.................. v ..................
:,:%%%%%:,....:::%%##:%.,,::::+,::+,:::,,........,.....:++:+%,.........|| Just Few Clicks ||........
,,,:+###%+,::,..,%#%+##::,,::+%%.,,:+::......::,,::,.....:%##+................... | ................
++++%###%+%##:,,,+%:###+,,,.,::%%,:%,%......,::............+#:................... | ................
:+%%####%%##%::,,##%#%+%+:.,.,,,,,+#,:,.,.++#%%............,,:................... | ................
+%#####++%##%:+.:###%%+##%,..,,...:#:,:,+:##:,:,,,.........:.,................... & ................
%%##%++%##%%+.,,%%%#++%###,..++,,.,#,.::+:%,+%%#%%+:,......+:.................... | ................
##%:...:+:::+.,%+%+%+:%###+.,##,,.,%,.,%:,,+########%,....+%..................... | ................
##+:,.,+++:+%,+%,%+++,##+##,##%:,.,%:.,,,,,###%:,,:%#+%::%+:..............|| V O I L A ! ||.........
##+::%#######%:%,%:+::##+%##%#%::.:%:,..:%###:......,::,:#+...................... | ................
#############%:%,%:+:+#%++%#%#+,,.:%:...+%###%:.......,.%%,...................... | ................
#############%:+:%:::+%%:::%%%:,,,%%,...+###%#:,......,,%:....................... | ................
%%##+########+:+:%::,++%+:,+#+:,,:%+...,+####+::......,:+........................ | ................
+%#%%#######%%,+%+::,+:%%,:,%:+:,:%:,..,:##:#%:,......,.......................... | ................
%##%##%##%%#++,+#%::,::+%++:++++++%:,..,%##:+#:,....,,........................... | ................
##%#%%###++%:+,:##,:,,+::%+:+%+++%#+.,:%####:#:.,..::,........................... | ................
##%%%%%%%::%+%,+#%:::.+:,+%%++::%+#+:+%###%#%+:,,::.:,........................... v ................
##%+#%+%%:,%%%:%%%%::.,+.++#%+:+%+##%#####%##+%::%,:,........|| Basic PKG2DMG convertor utility ||..
####+:+%+,,%%%+#++%:%:,:,,%##++#+,+%#######%##:%+,:%:...............................................
##%#++:%,,,+%%%#++%%%+,.,:+##%+%,,:%%############+++:...............................................
#+%%+::%.,,%++#%:+%#+%,..:+##%#+.,:++%%#####%%######+...............................................
,+#+:::+..%++:#%::%#%%:,.:+%#%%::,,::%%#####+%%%####,...............................................
:##:::+::%#%+:#+,,###%%,.++%#%::+,,::%%####%%%%%%%%+,...............................................
##%:++#%##%#+:#+,:%##%#,,++###::+:,,:+%######%++%++,................................................
----------------------------------------------------------------------------------------------------
                                                                                                    
EOF
}
# Main function
main() {
    printf '\e[107m\e[1;30m' 
    [ "$UID" -eq 0 ] || { echo "This script must be run as root."; exit 1;}

    # Display art to welcome users
    display_art 

    # Get user inputs
    get_inputs
    
    # Create blank DMG
    create_blank_dmg "$dmg_path"

    # Format DMG with APFS
    format_dmg_apfs "$dmg_path"

    # Extract .pkg
    extract_pkg "$pkg_file" "$extract_dir"

    # Mount DMG
    mount_dmg "$dmg_path" "$mount_point"

    # Copy extracted files to DMG
    copy_to_dmg "$extract_dir" "$mount_point"

    # Decompress payload using pbzx.py
    decompress_payload "$mount_point"

    # Unmount DMG
    unmount_dmg "$mount_point"

    # Clean up
    cleanup
}

# Run the main function
main