{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6e26abf5-c92c-44b9-bd7a-92f7ea8f3cb8",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "hide_cell",
     "remove-cell"
    ]
   },
   "outputs": [],
   "source": [
    "my_notebook_text = \"\"\"\n",
    "Homework 1 assugnment due by Tue 9/8/2026.\n",
    "\n",
    "- On your virtual desktop (VD), create directory ```HW_1```.\n",
    "- Inside ```HW_1```, complete exercises in section #1.20.\n",
    "- Create directory ```Files```.\n",
    "- Copy file ```vmlinuz-6.12.105+deb13-amd64``` into directory ```Files```.\n",
    "- Find out the directory disk utilization by using command ```du```.\n",
    "- Download file from URL https://ftp.osuosl.org/pub/midnightcommander/mc-4.8.33.tar.bz2\n",
    "- Extract all the files from archive mc-4.8.33.tar.bz2\n",
    "- Find files with name ```Makefile.am```  in directory ```mc-4.8.33```\n",
    "- Find directories with name ```data```.\n",
    "- Find and delete directories with name ```hu```.\n",
    "- Find the largest file in the directory.\n",
    "- Copy directories ```data``` recursively into new directory ```all_data``` in your HW_1.\n",
    "- Archive and compress directory ```all_data``` into file ```all_data.tgz```\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "33012b43-f6f9-41bc-9960-eb6bfa7fad52",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": [
     "remove-cell",
     "hide_cell"
    ]
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Both GOOGLE_API_KEY and GEMINI_API_KEY are set. Using GOOGLE_API_KEY.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# Homework 1: Linux Command Line & File Management\n",
      "\n",
      "**Due Date:** Tuesday, September 8, 2026\n",
      "\n",
      "---\n",
      "\n",
      "## Virtual Desktop Environment Workflow\n",
      "\n",
      "Refer to the high-level workflow diagram below for establishing your remote environment session before starting the assignment.\n",
      "\n",
      "```text\n",
      "[Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\n",
      "                                                                                     |\n",
      "                                                                                     v\n",
      "                                                                   [Create & Navigate to ~/HW_1]\n",
      "                                                                                     |\n",
      "                                                                                     v\n",
      "                                                                 [Execute Assigned Shell Commands]\n",
      "```\n",
      "\n",
      "---\n",
      "\n",
      "## Instructions\n",
      "\n",
      "Complete all tasks directly on your assigned Virtual Desktop (VD) instance. Document your commands and output as required by your instructor.\n",
      "\n",
      "### Section 1: Directory Setup & Reading\n",
      "1. Access your Virtual Desktop environment.\n",
      "2. Create a working directory named `HW_1` in your home folder:\n",
      "   ```bash\n",
      "   mkdir ~/HW_1\n",
      "   ```\n",
      "3. Navigate into `HW_1` and complete all exercises listed in **Section 1.20** of your course textbook.\n",
      "\n",
      "---\n",
      "\n",
      "### Section 2: System File Copying & Disk Inspection\n",
      "1. Inside `HW_1`, create a sub-directory named `Files`:\n",
      "   ```bash\n",
      "   mkdir Files\n",
      "   ```\n",
      "2. Copy the system kernel file `/boot/vmlinuz-6.1.0-21-amd64` into your newly created `Files` directory.\n",
      "3. Check the disk space utilization of the `Files` directory using the `du` command.\n",
      "\n",
      "---\n",
      "\n",
      "### Section 3: File Acquisition & Archive Extraction\n",
      "1. Download the Midnight Commander source archive from the following URL using `wget` or `curl`:\n",
      "   ```text\n",
      "   https://ftp.osuosl.org/pub/midnightcommander/mc-4.8.33.tar.bz2\n",
      "   ```\n",
      "2. Extract all files from the compressed archive `mc-4.8.33.tar.bz2`.\n",
      "\n",
      "---\n",
      "\n",
      "### Section 4: Search, Cleanup, and Consolidation Operations\n",
      "\n",
      "Perform the following operations within the extracted source directory tree:\n",
      "\n",
      "1. **Locate Build Files:** Find all instances of files named `Makefile.am` within the `mc-4.8.33` directory.\n",
      "2. **Locate Data Directories:** Search for all directories named `data`.\n",
      "3. **Targeted Cleanup:** Locate and remove all directories named `hu`.\n",
      "4. **Size Analysis:** Identify the largest single file within the directory.\n",
      "5. **Recursive Data Copy:** Recursively copy all directories named `data` into a new directory named `all_data` inside your `HW_1` folder.\n",
      "6. **Archive Creation:** Create a compressed gzipped tar archive of the `all_data` directory named `all_data.tgz`:\n",
      "   ```bash\n",
      "   tar -czvf all_data.tgz all_data\n",
      "   ```\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import time\n",
    "import base64 \n",
    "from google import genai\n",
    "from google.genai.errors import APIError\n",
    "\n",
    "os.environ[\"GEMINI_API_KEY\"] = \"AQ.Ab8RN6K9G5S-h6IWgpUgfmHbIk7b8iZlWTuYptMFqOT4Ph6FnQ\"\n",
    "client = genai.Client()\n",
    "\n",
    "# Use triple quotes \"\" \" so you can paste long paragraphs easily!\n",
    "\n",
    "prompt_instructions_simple = f\"Rewrite this text for a university course. Make it clear and write WCAG 2.1 AA compliant alt text descriptions for any charts mentioned: {my_notebook_text}\"\n",
    "        \n",
    "\n",
    "prompt_instructions_detailed1 = (\n",
    "    \"Rewrite the following text for a university course to make it clear and engaging. \"\n",
    "    \"For any charts, graphs, or data visuals mentioned, do NOT write short alt text. Instead, \"\n",
    "    \"provide a comprehensive Detailed Description. \"\n",
    "    \"Format each Detailed Description using this structure:\\n\"\n",
    "    \"1. Overview: One sentence describing what the chart is showing.\\n\"\n",
    "    \"2. Setup: Explain the axes, labels, units, and layout.\\n\"\n",
    "    \"3. Key Takeaways: Break down the main data trends, peaks, or critical data points.\\n\\n\"\n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "# Force the model to include structured Detailed Descriptions AND Text-Based Diagrams\n",
    "\n",
    "prompt_instructions_detailed2 = (\n",
    "    \"Rewrite the following text for a university course to make it clear and engaging. \"\n",
    "    \"For EVERY chart, graph, diagram, or image mentioned in the text, you MUST generate \"\n",
    "    \"two components:\\n\\n\"\n",
    "    \n",
    "    \"A) A Comprehensive Detailed Description:\\n\"\n",
    "    \"   1. Overview: One sentence describing what the visual is showing.\\n\"\n",
    "    \"   2. Setup: Explain the axes, labels, units, and layout.\\n\"\n",
    "    \"   3. Key Takeaways: Break down the main data trends, peaks, or critical data points.\\n\\n\"\n",
    "    \n",
    "    \"B) A Text-Based Diagram:\\n\"\n",
    "    \"   - Create a clean, text-only layout of the visual asset.\\n\"\n",
    "    \"   - Use structured Markdown tables for grid-based data, charts, and plots.\\n\"\n",
    "    \"   - Use clean ASCII art flowcharts (e.g., [Step 1] -> [Step 2]) for process diagrams.\\n\"\n",
    "    \"   - Never skip this component. Every single visual asset must have a text-based representation.\\n\\n\"\n",
    "    \n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "\n",
    "# Prompt tailored strictly for Jupyter Notebook Markdown injection\n",
    "prompt_instructions_detailed3 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate. Immediately BELOW the \"\n",
    "    \"original untouched image link, add a clean Markdown block containing these two elements:\\n\\n\"\n",
    "    \n",
    "    \"A) A Comprehensive Detailed Description:\\n\"\n",
    "    \"   1. Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   2. Setup: Explain the primary components, labels, and basic layout without hyper-granular technical specs.\\n\"\n",
    "    \"   3. Key Takeaways: Core conceptual trends or interactions shown.\\n\\n\"\n",
    "    \n",
    "    \"B) A Text-Based Diagram:\\n\"\n",
    "    \"   - Provide a clean, text-only ASCII or Markdown table layout of the visual asset.\\n\"\n",
    "    \"   - Keep it high-level, clear, and perfectly formatted for a standard markdown code block.\\n\\n\"\n",
    "    \n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "\n",
    "prompt_instructions_detailed4 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate. Use the following strict sequence \"\n",
    "    \"when dealing with image placements:\\n\\n\"\n",
    "    \n",
    "    \"1. First, insert the NEW Text-Based Structured Alternative:\\n\"\n",
    "    \"   - For numerical data charts or grid plots: Provide a clean Markdown Data Table summarizing the information.\\n\"\n",
    "    \"   - For architecture, networks, or process workflows: Provide a text flowchart inside a preformatted code block using simple structural symbols like `[Box A] ===> [Box B]` and `|` or `v` paths.\\n\"\n",
    "    \"   - Ensure the structure is perfectly neat, readable, and uses standard characters that export flawlessly to static HTML.\\n\\n\"\n",
    "    \n",
    "    \"2. Second, place the ORIGINAL untouched image link exactly as it was.\\n\\n\"\n",
    "    \n",
    "    \"3. Third, immediately BELOW the original image link, add the Comprehensive Detailed Description:\\n\"\n",
    "    \"   - Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   - Setup: Explain the primary components, labels, axes, or layouts without hyper-granular technical specs.\\n\"\n",
    "    \"   - Key Takeaways: Core conceptual trends or interactions shown.\\n\\n\"\n",
    "    \n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "\n",
    "prompt_instructions_detailed5 = (\n",
    "    \"You are processing raw course text meant for Jupyter Notebook markdown cells.\\n\\n\"\n",
    "    \n",
    "    \"CRITICAL RULE: Do NOT delete, alter, or remove any original image links, image paths, \"\n",
    "    \"or image tags (e.g., lines starting with `![...](...)` or containing `<img ...>`). \"\n",
    "    \"Leave them exactly where they are in the original text sequence.\\n\\n\"\n",
    "    \n",
    "    \"INSTRUCTION:\\n\"\n",
    "    \"Rewrite the text to make it clear and university-appropriate.Make it clear and write WCAG 2.1 AA compliant. Use the following strict sequence \"\n",
    "    \"when dealing with image placements:\\n\\n\"\n",
    "    \n",
    "    \"1. First, insert the NEW Text-Based Structured Alternative ABOVE the image:\\n\"\n",
    "    \"   - For numerical data charts or grid plots: Provide a clean Markdown Data Table.\\n\"\n",
    "    \"   - For hardware architecture, network layouts, or OS Process lifecycles (like fork(), process states, execution threads): \"\n",
    "    \"Provide a high-level text flowchart inside a preformatted ```text code block.\\n\"\n",
    "    \"   - For Terminal Commands or Pipelines (like stdin/stdout piping, shell interactions): \"\n",
    "    \"Provide a vertical box-and-arrow sequence map illustrating data flow from one utility to the next.\\n\"\n",
    "    \"   - Keep diagram characters perfectly aligned and compatible with static HTML page exports.\\n\\n\"\n",
    "    \"   - CRITICAL RULE FOR VIRTUAL DESKTOP LOGIN CONTEXT:\\n\"\n",
    "    \"     If the text or diagram is about logging into the Virtual Desktop or using Ravada, \"\n",
    "    \"the text diagram sequence MUST start exactly with this workflow path:\\n\"\n",
    "    \"     [Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\\n\\n\"\n",
    "    \n",
    "    \"2. Second, place the ORIGINAL untouched image link exactly as it was.\\n\\n\"\n",
    "    \n",
    "    \"3. Third, immediately BELOW the original image link, add the Comprehensive Detailed Description:\\n\"\n",
    "    \"Inside this caption block, you MUST start with the exact text '**Accessible Image Description:**'. Place it always under the image.\"\n",
    "    \"   - Overview: One sentence describing what the visual shows at a high level.\\n\"\n",
    "    \"   - Setup: Explain the primary components, labels, axes, or code pathways without hyper-granular specs.\\n\"\n",
    "    \"   - Key Takeaways: Core conceptual trends or execution results shown.\\n\\n\"\n",
    "\n",
    "    f\"Course Text:\\n{my_notebook_text}\"\n",
    ")\n",
    "prompt_instructions = prompt_instructions_detailed5\n",
    "\n",
    "# Trying the 2.5-flash model which often has lower server congestion\n",
    "try:\n",
    "    response = client.models.generate_content(\n",
    "        model='models/gemini-3.6-flash',\n",
    "        contents= prompt_instructions,\n",
    "        )\n",
    "\n",
    "    #print(response.text)\n",
    "\n",
    "    output_text = response.text\n",
    "\n",
    "        # Post-process the text to extract the Mermaid syntax and convert it to a valid web image\n",
    "    if \"[MERMAID_START]\" in output_text:\n",
    "        start_idx = output_text.find(\"[MERMAID_START]\") + len(\"[MERMAID_START]\\n\")\n",
    "        end_idx = output_text.find(\"[MERMAID_END]\")\n",
    "        mermaid_code = output_text[start_idx:end_idx].strip()\n",
    "        \n",
    "        # Base64 encode the string for the Mermaid.ink service\n",
    "        encoded_bytes = base64.b64encode(mermaid_code.encode('utf-8'))\n",
    "        encoded_string = encoded_bytes.decode('utf-8')\n",
    "        image_url = f\"https://mermaid.ink{encoded_string}\"\n",
    "        \n",
    "        # Replace the raw markers with a real markdown image tag\n",
    "        compiled_tag = f\"#### Text-Based Flowchart Diagram\\n![Alternative Text Diagram]({image_url})\"\n",
    "        final_output = output_text[:output_text.find(\"[MERMAID_START]\")] + compiled_tag + output_text[end_idx + len(\"[MERMAID_END]\"):]\n",
    "        print(final_output)\n",
    "    else:\n",
    "        print(output_text)\n",
    "    \n",
    "    # --- THE MAGIC TRICK ---\n",
    "    # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "    #get_ipython().set_next_input(response.text)\n",
    "    \n",
    "except APIError as e:\n",
    "    if e.code == 503:\n",
    "        print(\"Servers are still busy. Waiting 5 seconds before a quick automatic retry...\")\n",
    "        time.sleep(5)\n",
    "        # Automatic second attempt\n",
    "        response = client.models.generate_content(\n",
    "            model='gemini-3.6-flash',\n",
    "            contents=f\"Rewrite this text for a university course. Make it clear and write WCAG 2.1 AA compliant alt text descriptions for any charts mentioned: {my_notebook_text}\",)\n",
    "        print(response.text)\n",
    "        # --- THE MAGIC TRICK ---\n",
    "        # This forces Jupyter to create a new cell below and paste the AI response inside it!\n",
    "        #get_ipython().set_next_input(response.text)\n",
    "    else:\n",
    "        raise e"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6c8ab5b-8094-4e5d-9a5d-fcdbfdd8c6e3",
   "metadata": {
    "editable": true,
    "slideshow": {
     "slide_type": ""
    },
    "tags": []
   },
   "source": [
    "# Homework 1: Linux Command Line & File Management\n",
    "\n",
    "**Due Date:** Tuesday, September 8, 2026\n",
    "\n",
    "---\n",
    "\n",
    "## Virtual Desktop Environment Workflow\n",
    "\n",
    "Refer to the high-level workflow diagram below for establishing your remote environment session before starting the assignment.\n",
    "\n",
    "```text\n",
    "[Web access to Ravada portal with authentication] ===> [Start VM] ===> [Access VM via SSH with provided port]\n",
    "                                                                                     |\n",
    "                                                                                     v\n",
    "                                                                   [Create & Navigate to ~/HW_1]\n",
    "                                                                                     |\n",
    "                                                                                     v\n",
    "                                                                 [Execute Assigned Shell Commands]\n",
    "```\n",
    "\n",
    "---\n",
    "\n",
    "## Instructions\n",
    "\n",
    "Complete all tasks directly on your assigned Virtual Desktop (VD) instance. Document your commands and output as required by your instructor.\n",
    "\n",
    "### Section 1: Directory Setup & Reading\n",
    "1. Access your Virtual Desktop environment.\n",
    "2. Create a working directory named `HW_1` in your home folder:\n",
    "   ```bash\n",
    "   mkdir ~/HW_1\n",
    "   ```\n",
    "3. Navigate into `HW_1` and complete all exercises listed in **Section 1.20** of your course textbook.\n",
    "\n",
    "---\n",
    "\n",
    "### Section 2: System File Copying & Disk Inspection\n",
    "1. Inside `HW_1`, create a sub-directory named `Files`:\n",
    "   ```bash\n",
    "   mkdir Files\n",
    "   ```\n",
    "2. Copy the system kernel file `vmlinuz-6.12.105+deb13-amd64` into your newly created `Files` directory.\n",
    "3. Check the disk space utilization of the `Files` directory using the `du` command.\n",
    "\n",
    "---\n",
    "\n",
    "### Section 3: File Acquisition & Archive Extraction\n",
    "1. Download the Midnight Commander source archive from the following URL using `wget` or `curl`:\n",
    "   ```text\n",
    "   https://ftp.osuosl.org/pub/midnightcommander/mc-4.8.33.tar.bz2\n",
    "   ```\n",
    "2. Extract all files from the compressed archive `mc-4.8.33.tar.bz2`.\n",
    "\n",
    "---\n",
    "\n",
    "### Section 4: Search, Cleanup, and Consolidation Operations\n",
    "\n",
    "Perform the following operations within the extracted source directory tree:\n",
    "\n",
    "1. **Locate Build Files:** Find all instances of files named `Makefile.am` within the `mc-4.8.33` directory.\n",
    "2. **Locate Data Directories:** Search for all directories named `data`.\n",
    "3. **Targeted Cleanup:** Locate and remove all directories named `hu`.\n",
    "4. **Size Analysis:** Identify the largest single file within the directory.\n",
    "5. **Recursive Data Copy:** Recursively copy all directories named `data` into a new directory named `all_data` inside your `HW_1` folder.\n",
    "6. **Archive Creation:** Create a compressed gzipped tar archive of the `all_data` directory named `all_data.tgz`:\n",
    "   ```bash\n",
    "   tar -czvf all_data.tgz all_data\n",
    "   ```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
