#!/usr/bin/env bash
cd /var/tmp

apt install -y toilet fzf mpc vis ffmpeg

# pip3 install --user fmui

git clone http://github.com/seebye/fmui /usr/local

USER=seebye
PROJECT=fmui
PRERELEASE=false
# RELEASE=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/releases?per_page=5" | jq -r '.[0] | select( .prerelease = false ) |.tag_name')
# VERSION=$(echo "${RELEASE}" | cut -c 2- | cut -f1 -d"-")
VERSION=$(date +%Y-%m-%d_)git
DOWNLOAD_DIR=/tmp

RSS_FEED="https://github.com/${USER}/${PROJECT}/releases.atom"
# xdg-open ${RSS_FEED}
echo ${RSS_FEED}

# make a containing directory
mkdir -p /var/tmp/fmui-"${RELEASE}"
cd /var/tmp/fmui-"${RELEASE}" || exit

cat >./description-pak <<EOF
**f**zf **m**pd **u**ser **i**nterface
EOF

cat >./checkinstall_it.sh <<'EOF'

echo "ENTERING CHECKINSTALL"

BASE_URL='https://api.github.com/repos'
BASE_USER='seebye'
BASE_REPO='fmui'
LICENSE_PAGE_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/license
RELEASES_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/releases

# CONTENT=$(curl -s ${RELEASES_URL}/latest)
# LATEST_URL=$(echo "${CONTENT}" | jq --raw-output '.assets[] | select(.browser_download_url | test("Linux-x86_64")) | .browser_download_url')

echo "LATEST_URL: " "${LATEST_URL}"

# RELEASE=$(echo "${CONTENT}" | jq --raw-output '.tag_name')

# LICENSE_URL=$(curl "${LICENSE_PAGE_URL}" | jq --raw-output '.download_url')
# wget -c ${LICENSE_URL}

VERSION=$( date +%Y-%m-%d_ )git
VERSION=1.0 
RELEASE=git

# make a new temporary directory for this use
BASE_TMP_DIR=~/tmptmp/checkinstall_tmp
mkdir -p ${BASE_TMP_DIR}

# do your work
checkinstall -y --fstrans \
	--pkgname=fmui \
	--pkgversion=${VERSION}\
	--pkgrelease=${RELEASE} \
	--pkgarch=amd64 \
	--pkggroup=admin \
	--pkglicense=LICENSE \
 	--pkgsource=${LATEST_URL} \
 	--maintainer=cyteen@ring-zero.co.uk \
 	--requires=toilet,fzf,mpc,vis,ffmpeg \
 	-D \
 	bash ./install.sh
EOF

cat >./install.sh <<'EOF'
BASE_URL='https://api.github.com/repos'
BASE_USER='seebye'
BASE_REPO='fmui'
# RELEASES_URL="${BASE_URL}/${BASE_USER}/${BASE_REPO}"/releases
# CONTENT=$(curl -s ${RELEASES_URL}/latest)
# LATEST_URL=$(echo "${CONTENT}" | jq --raw-output '.assets[] | select(.browser_download_url | test("Linux-x86_64")) | .browser_download_url')
LATEST_URL=http://github.com/seebye/fmui

# Download and copy the latest version to /usr/local/bin and make executable
git clone ${LATEST_URL} /var/tmp/fmui 
cp -a /var/tmp/fmui /usr/local/ 
chmod 0775 /usr/local/fmui
chmod +x /usr/local/fmui/fmui && 
chmod +x /usr/local/fmui/actions.sh
chmod +x /usr/local/fmui/cover.sh
chmod +x /usr/local/fmui/defaults.sh
chmod +x /usr/local/fmui/essentials.sh
chmod +x /usr/local/fmui/info.sh
chmod +x /usr/local/fmui/keys.sh
chmod +x /usr/local/fmui/mpc.sh
ln -s /usr/local/fmui/fmui /usr/local/bin/fmui
EOF

bash ./checkinstall_it.sh

cat >~/.config/fmui <<'EOF'
# Default keybindings
# Key 	Action 	Subscreens
# q 	Quit fzf 	
# u 	Update fzf preview window 	
# v 	Start visualizer 	
# i 	Show song info screen 	
# j 	Go down 	
# k 	Go up 	
# down 	Go down 	
# up 	Go up 	
# return 	Play choice 	
# mod-r 	Toggle repeat 	
# mod-s 	Shuffle 	
# mod-d 	Update mpd database, restart 	
# left 	Seek backwards 	
# right 	Seek forwards 	
# h 	Seek backwards 	✓
# l 	Seek forwards 	✓
# p 	Toggle play 	✓
# c 	Toggle consume 	✓
# s 	Toggle single song 	✓
# r 	Toggle random 	✓
# < 	Play previous song 	✓
# > 	Play next song 	✓
 
# readonly VOLUME_STEP=2
# # format? see man fzf
# readonly ACTION_VOLUME_UP="execute-silent:amixer set Master ${VOLUME_STEP}%+ unmute"
# readonly ACTION_VOLUME_DOWN="execute-silent:amixer set Master ${VOLUME_STEP}%- unmute"
# 
# key_bindings+=(
#     [+]="$ACTION_VOLUME_UP"
#     [=]="$ACTION_VOLUME_UP"
#     [-]="$ACTION_VOLUME_DOWN"
# )
EOF
