#!/bin/bash
# Experiment 3: Demonstrate UNIX file permissions using chmod
# Run: chmod +x 03_unix_chmod.sh && ./03_unix_chmod.sh

echo "============================================"
echo " UNIX File Permissions (chmod)"
echo "============================================"

mkdir -p sandbox
cd sandbox

# Create a sample file
touch sample.txt
echo "Hello permissions" > sample.txt

echo
echo "--- Initial permissions ---"
ls -l sample.txt

echo
echo "Permission breakdown:"
echo "  Position 1   : file type (- = file, d = dir, l = link)"
echo "  Positions 2-4: owner    permissions (r,w,x)"
echo "  Positions 5-7: group    permissions (r,w,x)"
echo "  Positions 8-10: others  permissions (r,w,x)"

echo
echo "--- chmod 777 (rwx for everyone) ---"
chmod 777 sample.txt
ls -l sample.txt

echo
echo "--- chmod 644 (rw for owner, r for group/others) ---"
chmod 644 sample.txt
ls -l sample.txt

echo
echo "--- chmod 755 (rwx owner, rx group/others) ---"
chmod 755 sample.txt
ls -l sample.txt

echo
echo "--- chmod 400 (read-only for owner, nothing else) ---"
chmod 400 sample.txt
ls -l sample.txt

echo
echo "--- Symbolic mode examples ---"
chmod u+x sample.txt          # add execute for user
ls -l sample.txt
chmod g-w sample.txt          # remove write for group
ls -l sample.txt
chmod o=r sample.txt          # set others to read only
ls -l sample.txt
chmod a+rwx sample.txt        # everyone gets all
ls -l sample.txt

echo
echo "Octal cheat sheet:"
echo "  r = 4, w = 2, x = 1"
echo "  Sum per digit. e.g. 7 = rwx, 6 = rw, 5 = rx, 4 = r"
echo "  Order is owner / group / others"

cd ..
rm -rf sandbox
