#! /bin/bash

# Script to build the dev- and web- images.
# 1 - Create a directory in which you will put this script (scripting/)
# 2 - Create a base/ directory inside scripting (scripting/base/)
# 3 - Put the zipped base images into the base/ directory
#       The zips must contain a directory which includes the changes and image files
# 4 - Launch the script from the scripting/ directory

# To create a base image:
# - You have to install Universe on it
# - You have to install Installer on it (in a version which is compatible with Universe)
# - You should set the display depth to 32bits
# - Reduce the size of the window

# Ask a question and let the user answer with Yes or no (Yes is the
# default).
# if yesno "Do you want to try again?"; then
#   ...
# fi
function yesno() {
    read -p "$1 (Y/n) " answer
    answer=${answer:-"Y"}
    if [ "$answer" = "Y" ]; then
	return 0;
    else
	return 1;
    fi
}

function ask() {
    read -p "  $1 ($2)" tmpVar

    # if the user does not answer, use the default value given in $2
    tmpVar=${tmpVar:-"$2"}

    echo "${tmpVar}"
}

function constructImageName() {
    if [ "$VENDOR" = "sq" ]; then
	if [ "$BETA" -eq 0 ]; then
	    revision="3.10.2"
	    subrevision='7179'
	else
	    revision="3.9.1"
	    subrevision='7075'
	fi
    else
	revision='0.1'
	subrevision='10072'
    fi

    revision=$(ask 'What is the image main revision?' "$revision")
    subrevision=$(ask 'What is the image sub revision?' "$subrevision")
    year=$(ask 'What is the current year?' $(date +'%y'))
    month=$(ask 'What is the current month?' $(date +'%m'))
    version=$(ask "What is this $type image revision?" "1")
    echo "${VENDOR}${revision}-${subrevision}${DISTRIBUTION}${year}.${month}.${version}"
}

function createInstallationScript() {
    
    echo "
Preferences disable: #showDeprecationWarnings.
installer := UGlobalInstaller universe: UUniverse systemUniverse.
installer requestPackageList.
installer selectAllUpgrades.
installer doInstall." > installScript.st
    echo "
universe := Installer universe.
universe
  update;
  answer: '*username*' with: 'admin';
  answer: '*password*' with: 'seaside';
  answer: 'Would you like*' with: true;
  answer: '*port number*' with: '8080';
  answer: '*is * running*' with: true;
  answer: '*name of your Pier kernel*' with: 'Pier';
  answer: '*entry point*' with: 'pier';
  answer: '*Application' with: true." >> installScript.st
    for param in "$@"; do
	echo "
universe install: '$param'." >> installScript.st
    done 
    echo "Preferences enable: #showDeprecationWarnings.
SmalltalkImage current snapshot: true andQuit: true. " >> installScript.st
}

function generateImage() {
    squeakArchive="$1"
    shift 1
    rm -rf tmp/ 2>/dev/null
    mkdir tmp
    cd tmp
    echo "  Extracting the base image..."
    unzip -qq ../"${squeakArchive}" >/dev/null
    cd *
    createInstallationScript "$@"
    echo "  Launching Squeak..."
    nice -n 20 squeak *.image "${PWD}"/installScript.st
    newName=$(constructImageName)

    # Removes unneeded files
    ls -x1 |  grep --invert-match --extended '(\.image)|(\.changes)' | xargs rm -rf

    # Rename the .image and .changes to use the new name
    rename 's/^.*\./'${newName}'./' *
    cd ..

    # Renames the extracted directory to the name of the image file
    mv * "${newName}"
    echo "  Compressing the new image..."
    zip -9rq "${newName}.zip" "${newName}" >/dev/null
    mv "${newName}.zip" ../upload/
    cd ..
}

function genereate-squeak-dev() {
    for squeakArchive in base/Squeak*.zip; do # for all squeak base images
	export VENDOR=sq
	echo "- Squeak base image is: ${squeakArchive}"
	if ! yesno "  Do you want to generate from this image?"; then
	    continue
	fi

	yesno "  Is it a beta?"
	export BETA=$?
	
	if yesno "  Do you want to generate a squeak-dev image?"; then
	    export DISTRIBUTION=dev
	    echo "  Generating a squeak-dev..."
	    if [ $BETA -eq 0 ]; then
		generateImage "${squeakArchive}" 'Squeak dev packages beta' 'Squeak-dev image'
	    else
		generateImage "${squeakArchive}" 'Squeak-dev image'
	    fi
	fi

	if yesno "  Do you want to generate a squeak-web image?"; then
	    export DISTRIBUTION=web
	    echo "  Generating a squeak-web..."
	    if [ $BETA -eq 0 ]; then
		generateImage "${squeakArchive}" 'Squeak web packages' 'Squeak dev packages beta' 'Squeak-dev image'
	    else
		generateImage "${squeakArchive}" 'Squeak web packages' 'Squeak-dev image'
	    fi
	fi
    done
}

function generate-pharo() {
    for squeakArchive in base/Pharo*.zip; do # for all pharo base images
	export VENDOR=pharo
	echo "- Pharo base image is: ${squeakArchive}"
	if ! yesno "  Do you want to generate from this image?"; then
	    continue
	fi

	if yesno "  Do you want to generate a pharo-dev image?"; then
	    export DISTRIBUTION=dev
	    echo "  Generating a pharo-dev..."
	    generateImage "${squeakArchive}" 'Pharo packages'
	fi

	if yesno "  Do you want to generate a pharo-web image?"; then
	    export DISTRIBUTION=web
	    echo "  Generating a pharo-web..."
	    generateImage "${squeakArchive}" 'Squeak web packages' 'Pharo packages'
	fi
    done
}

mkdir -p upload

if yesno "Do you want to generate squeak-dev images?"; then
    generate-squeak-dev
fi

if yesno "Do you want to generate pharo images?"; then
    generate-pharo
fi