#! /usr/bin/bash

# Get to openshift-docs directory
cd ~/git-repos/openshift-docs

# Get to installing BM directory
cd installing/installing_bare_metal

# Setup UPI directory
mkdir upi
cd upi
ln -s ../../modules/ modules
ln -s ../../snippets/ snippets
ln -s ../../_attributes/ _attributes
ln -s ../../images/ images

# Return to BM directory and move appropriate modules down 1 level.
cd ..
mv installation-config-parameters-bare-metal.adoc upi
mv installing-bare-metal.adoc upi
mv installing-bare-metal-network-customizations.adoc upi
mv installing-restricted-networks-bare-metal.adoc upi
mv scaling-a-user-provisioned-cluster-with-the-bare-metal-operator.adoc upi

# Create IPI directory
mkdir ipi
cd ipi
ln -s ../../modules/ modules
ln -s ../../snippets/ snippets
ln -s ../../_attributes/ _attributes
ln -s ../../images/ images

# Get to openshift-docs directory
cd ~/git-repos/openshift-docs

# Get to installing BM IPI directory
cd installing/installing_bare_metal_ipi

# Move assemblies to new IPI subdirectory.
mv *.adoc ../installing_bare_metal/ipi

# Remove the old IPI directory.
cd ..
rm -rf installing_bare_metal_ipi

# Fix hyperlinks in the bare metal assembies
cd ~/git-repos/openshift-docs
cd installing/installing_bare_metal
cd upi
find . -maxdepth 1 -type f -exec sed -i 's|xref:\.\./\.\./|xref:../../../|g' {} +
cd ~/git-repos/openshift-docs
cd installing/installing_bare_metal
cd ipi
find . -maxdepth 1 -type f -exec sed -i 's|xref:\.\./\.\./|xref:../../../|g' {} +

cd ~/git-repos/openshift-docs
# Find xref:../../installing/installing_bare_metal/*.adoc that were moved to upi directory
# Replace with xref:../../installing/installing_bare_metal/upi/*.adoc
find . -type f -name "*.adoc" -exec sed -i \
  -e 's|\(xref:.*installing/installing_bare_metal/\)installation-config-parameters-bare-metal|\1upi/installation-config-parameters-bare-metal|g' \
  -e 's|\(xref:.*installing/installing_bare_metal/\)installing-bare-metal|\1upi/installing-bare-metal|g' \
  -e 's|\(xref:.*installing/installing_bare_metal/\)installing-bare-metal-network-customizations|\1upi/installing-bare-metal-network-customizations|g' \
  -e 's|\(xref:.*installing/installing_bare_metal/\)installing-restricted-networks-bare-metal|\1upi/installing-restricted-networks-bare-metal|g' \
  -e 's|\(xref:.*installing/installing_bare_metal/\)scaling-a-user-provisioned-cluster-with-the-bare-metal-operator|\1upi/scaling-a-user-provisioned-cluster-with-the-bare-metal-operator|g' {} +

# Find xref:../../installing/installing_bare_metal_ipi/*.adoc
# Replace with xref:../../installing/installing_bare_metal/ipi/*.adoc

find . -type f -name "*.adoc" -exec sed -i 's|xref:\.\./installing/installing_bare_metal_ipi/|xref:../installing/installing_bare_metal/ipi/|g' {} +
find . -type f -name "*.adoc" -exec sed -i 's|xref:\.\./\.\./installing/installing_bare_metal_ipi/|xref:../../installing/installing_bare_metal/ipi/|g' {} +
find . -type f -name "*.adoc" -exec sed -i 's|xref:\.\./\.\./\.\./installing/installing_bare_metal_ipi/|xref:../../../installing/installing_bare_metal/ipi/|g' {} +

# Fix hyperlink comments in modules
cd modules
find . -type f -exec sed -i 's|installing/installing_bare_metal/|installing/installing_bare_metal/upi/|g' {} +
find . -type f -exec sed -i 's|installing/installing_bare_metal_ipi/|installing/installing_bare_metal/ipi/|g' {} +
