#! /bin/bash

# Usage: deployarmimg image device edition version
# Example: deployarmimg Manjaro-ARM-minimal-rpi2-18.07.zip rpi2 minimal 18.07

# Set globals
LIBDIR=/usr/share/manjaro-arm-tools/lib
TORRENT=false

#imports
source $LIBDIR/functions.sh
enable_colors

# Available options
opt=":i:d:e:v:k:u:th"

while getopts "${opt}" arg; do
  case $arg in
    i)
      IMAGE="${OPTARG}"
      ;;
    d)
      DEVICE="${OPTARG}"
      ;;
    e)
      EDITION="${OPTARG}"
      ;;
    v)
      VERSION="${OPTARG}"
      ;;
    k)
      GPGMAIL="${OPTARG}"
      ;;
    u)
      STORAGE_USER="${OPTARG}"
      ;;
    t)
      TORRENT="true"
      ;;
    \?)
      echo "Invalid option: -${OPTARG}"
      exit 1
      ;;
    h|?)
      usage_deploy_img
      exit 1
      ;;
    :)
      echo "Option -${OPTARG} requires an argument, aborting"
      exit 1
      ;;
  esac
done

#required options
if [ "x" == "x$IMAGE" ]; then
  echo "-i [option] is required"
  exit
fi
if [ "x" == "x$GPGMAIL" ]; then
  echo "-k [option] is required"
  exit
fi


# Create checksums for the image
checksum_img

#create torrent for image
if [[ $TORRENT = "true" ]]
then
create_torrent
fi

# Upload image + checksums to image server
img_upload


# remove pkg + sig from cache folder
#msg "Removing local files..."
#rm $image*
 
