#!/bin/bash

# Git-aware repository restructuring script
# Uses git mv to preserve file history during reorganization

set -e

echo "=== Git-aware Repository Restructuring ==="
echo "This script will reorganize files using 'git mv' to preserve history"
echo ""

# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    echo "Warning: Not in a git repository. Using regular 'mv' commands."
    MV_CMD="mv"
else
    echo "Git repository detected. Using 'git mv' to preserve history."
    MV_CMD="git mv"
fi

echo ""
echo "=== Creating new directory structure ==="

# Create new directory structure
mkdir -p tools/{security,forensics,data,hashing,network,formats,cloud,system,ctf,text}
mkdir -p projects/{go-tools,puzzlebox,timesketch,rust-tools}
mkdir -p scripts/{proxy,display,setup,windows}
mkdir -p config/{shell,visidata/plugins,applications}
mkdir -p archive/{collected,experimental,binaries,awk}

echo "=== Moving security tools ==="
$MV_CMD codegrab/scan_vt.py tools/security/
$MV_CMD codegrab/vt_download.py tools/security/
$MV_CMD codegrab/vt_ip.py tools/security/
$MV_CMD codegrab/vt_pdns.py tools/security/
$MV_CMD codegrab/imphash.py tools/security/
$MV_CMD codegrab/scapy_arp.py tools/security/
$MV_CMD codegrab/simple_portscan.py tools/security/
$MV_CMD codegrab/smtpbanner.py tools/security/
$MV_CMD codegrab/testpw.py tools/security/
$MV_CMD codegrab/certwipe tools/security/

echo "=== Moving forensics tools ==="
$MV_CMD codegrab/chechsqlite.py tools/forensics/
$MV_CMD codegrab/process_leak.py tools/forensics/
$MV_CMD codegrab/extractfolder.py tools/forensics/

echo "=== Moving data processing tools ==="
$MV_CMD tools/domgrep.py tools/data/
$MV_CMD tools/geturls.py tools/data/
$MV_CMD tools/urldecode.py tools/data/
$MV_CMD tools/unum.py tools/data/
$MV_CMD codegrab/vba_chr_decode.py tools/data/
$MV_CMD tools/quickchardet.py tools/data/
$MV_CMD codegrab/kv_parse.py tools/data/
$MV_CMD tools/concat.py tools/data/
$MV_CMD tools/split_linewise.py tools/data/
$MV_CMD codegrab/json_save.py tools/data/
$MV_CMD tools/csv_get tools/data/
$MV_CMD codegrab/uniq.py tools/data/
$MV_CMD tools/between tools/data/

echo "=== Moving hashing tools ==="
$MV_CMD tools/libarchivesum.py tools/hashing/
$MV_CMD tools/tarsum.py tools/hashing/
$MV_CMD codegrab/hashzip.py tools/hashing/
$MV_CMD tools/scatterhash.py tools/hashing/

echo "=== Moving network tools ==="
$MV_CMD tools/ipgrep tools/network/
$MV_CMD codegrab/fritzshark.sh tools/network/
$MV_CMD codegrab/fritzshark2.sh tools/network/
$MV_CMD tools/get_stp.sh tools/network/
$MV_CMD tools/get_ntp.py tools/network/

echo "=== Moving format conversion tools ==="
$MV_CMD codegrab/convert2pdf.sh tools/formats/
$MV_CMD codegrab/flatpdf.sh tools/formats/
$MV_CMD tools/rename.mime.py tools/formats/

echo "=== Moving cloud service tools ==="
$MV_CMD codegrab/cloudsend.py tools/cloud/
$MV_CMD codegrab/cloudsend.sh tools/cloud/
$MV_CMD codegrab/speech.py tools/cloud/
$MV_CMD codegrab/vqa3.py tools/cloud/
$MV_CMD codegrab/youtube_resolve.sh tools/cloud/

echo "=== Moving system utilities ==="
$MV_CMD tools/backup_docker.sh tools/system/
$MV_CMD tools/restore_docker.sh tools/system/
$MV_CMD tools/watchgrowth.sh tools/system/
$MV_CMD codegrab/wipe.sh tools/system/
$MV_CMD codegrab/ltop.py tools/system/

echo "=== Moving CTF tools ==="
$MV_CMD codegrab/ctf/filtertext.py tools/ctf/
$MV_CMD codegrab/ctf/getjs.py tools/ctf/
$MV_CMD codegrab/ctf/guess.py tools/ctf/
$MV_CMD codegrab/ctf/search.py tools/ctf/
$MV_CMD codegrab/ctf/transpose.py tools/ctf/
$MV_CMD codegrab/ctf/ps_.py tools/ctf/
$MV_CMD codegrab/ctf/submit_flag.sh tools/ctf/

echo "=== Moving text analysis tools ==="
$MV_CMD codegrab/probability.py tools/text/
$MV_CMD codegrab/depth tools/text/

echo "=== Moving experimental tools to archive ==="
$MV_CMD codegrab/kv.py archive/experimental/
$MV_CMD codegrab/flm.py archive/experimental/
$MV_CMD codegrab/hydrogentest.py archive/experimental/
$MV_CMD codegrab/matplottest.py archive/experimental/
$MV_CMD codegrab/lpic.sh archive/experimental/
$MV_CMD codegrab/fuzz.sh archive/experimental/

echo "=== Moving multi-file projects ==="
$MV_CMD tools/go projects/go-tools
$MV_CMD codegrab/puzzlebox projects/
$MV_CMD codegrab/deploy_timesketch.sh projects/timesketch/

# Move Rust tools if they exist
if [ -d tools/rs ]; then
    $MV_CMD tools/rs projects/rust-tools
fi

echo "=== Moving system scripts ==="
$MV_CMD systemscripts/proxy scripts/
$MV_CMD systemscripts/reset_screens.sh scripts/display/
$MV_CMD systemscripts/toggle_display.sh scripts/display/
$MV_CMD systemscripts/toggle_touchpad scripts/display/
$MV_CMD systemscripts/terminal-logs.sh scripts/setup/
$MV_CMD systemscripts/automountctl scripts/setup/

# Move additional system scripts if they exist
[ -f systemscripts/mount_container ] && $MV_CMD systemscripts/mount_container scripts/setup/
[ -f systemscripts/fullhd ] && $MV_CMD systemscripts/fullhd scripts/setup/
[ -f systemscripts/share.sh ] && $MV_CMD systemscripts/share.sh scripts/setup/

echo "=== Moving PowerShell scripts ==="
$MV_CMD codegrab/Get-ZimmermanTools.ps1 scripts/windows/
$MV_CMD codegrab/sbom.ps1 scripts/windows/
if [ -d codegrab/powershell ]; then
    $MV_CMD codegrab/powershell/getscreen.psm1 scripts/windows/
fi

echo "=== Organizing configuration files ==="
$MV_CMD config/bash_aliases config/shell/
$MV_CMD config/bash_prompt config/shell/
$MV_CMD config/shell_aliases config/shell/
$MV_CMD config/inputrc config/shell/
$MV_CMD systemscripts/agnoster.zsh-theme config/shell/
$MV_CMD systemscripts/solarized.dircolors config/shell/

$MV_CMD config/visidatarc config/visidata/
$MV_CMD config/visidataplugins/hidecol.py config/visidata/plugins/
[ -f config/access_log.vdj ] && $MV_CMD config/access_log.vdj config/visidata/

# Applications directory should already be in the right place
# Just ensure it exists
mkdir -p config/applications

echo "=== Moving items to archive ==="
$MV_CMD collected archive/

# Move binaries and scripts to archive
[ -f codegrab/csv_cols ] && $MV_CMD codegrab/csv_cols archive/binaries/
[ -f codegrab/mapping ] && $MV_CMD codegrab/mapping archive/binaries/
[ -f tools/csv2dot ] && $MV_CMD tools/csv2dot archive/binaries/
[ -f tools/mailunpack ] && $MV_CMD tools/mailunpack archive/binaries/
[ -f tools/noerr ] && $MV_CMD tools/noerr archive/binaries/
[ -f tools/openflattenpdf.sh ] && $MV_CMD tools/openflattenpdf.sh archive/binaries/
[ -f tools/sep_test.sh ] && $MV_CMD tools/sep_test.sh archive/binaries/
[ -f tools/showgm.sh ] && $MV_CMD tools/showgm.sh archive/binaries/
[ -f tools/showosm.sh ] && $MV_CMD tools/showosm.sh archive/binaries/
[ -f tools/sparsecmp.sh ] && $MV_CMD tools/sparsecmp.sh archive/binaries/
[ -f tools/trunc_by_hash.py ] && $MV_CMD tools/trunc_by_hash.py archive/binaries/

# Move AWK scripts
[ -f codegrab/ips.awk ] && $MV_CMD codegrab/ips.awk archive/awk/
[ -f codegrab/map.awk ] && $MV_CMD codegrab/map.awk archive/awk/

# Move any remaining compiled binaries
[ -f codegrab/rootshell.c ] && $MV_CMD codegrab/rootshell.c archive/binaries/
[ -f codegrab/usbreset.c ] && $MV_CMD codegrab/usbreset.c archive/binaries/
[ -f codegrab/imphash.go ] && $MV_CMD codegrab/imphash.go archive/binaries/

echo "=== Cleaning up empty directories ==="
# Remove empty directories (but be careful with git)
if [ "$MV_CMD" = "git mv" ]; then
    echo "Leaving directory cleanup for manual review due to git"
else
    rmdir codegrab/ctf codegrab/powershell codegrab systemscripts tools config/visidataplugins 2>/dev/null || true
fi

echo ""
echo "=== Repository restructuring complete! ==="
echo ""
echo "New structure:"
echo "├── tools/          - Single-file utilities by purpose"
echo "├── projects/       - Multi-file projects"  
echo "├── scripts/        - System management scripts"
echo "├── config/         - Configuration files"
echo "├── dockerfiles/    - Docker configurations (unchanged)"
echo "└── archive/        - Legacy and experimental items"
echo ""

if [ "$MV_CMD" = "git mv" ]; then
    echo "All moves used 'git mv' - file history preserved!"
    echo "You can review changes with: git status"
    echo "Commit when ready with: git commit -m 'Restructure repository for better organization'"
else
    echo "Standard 'mv' used - consider initializing git if needed"
fi
