#!/bin/bash

# Ensure the script receives a directory name as an argument
if [ $# -ne 1 ]; then
    echo "Usage: $0 <directory_name>"
    exit 1
fi

directory="/home/ubuntu/html/$1"
zip="/home/ubuntu/html/revive-adserver-5.5.1.zip"

# Check if the directory already exists
if [ ! -d "$directory" ]; then
    # Create the directory
    mkdir -p "$directory" && echo "Directory '$directory' created successfully"
else
    echo "Directory '$directory' already exists"
    exit 1
fi

# Copy the zip file into the created directory
cp "$zip" "$directory" && echo "Zip file copied successfully"

# Change directory to the newly created directory
cd "$directory" || exit

# Unzip the file
unzip -q "$zip" && echo "Zip file '$zip' extracted successfully"

# Find the extracted folder
extracted_folder=$(find . -maxdepth 1 -type d -name "*[0-9]*.*" -print -quit)

# Move the extracted files to the parent directory
mv "$extracted_folder"/* . && echo "Extracted files moved successfully"

# Delete the empty revive folder
rm -rf "$extracted_folder" && echo "Revive empty folder deleted successfully"

# Change back to the parent directory
cd ..

# Set execute permissions
chmod -R a+w "$directory/var"
chmod -R a+w "$directory/plugins"
chmod -R a+w "$directory/www/admin/plugins"
chmod -R a+w "$directory/www/images"
echo "permission done"
