#!/bin/bash
export PATH=$PATH:$3/bin
if [ ! -e "sdk/$2.sdk" ]; then
	echo "sdk/$2.sdk not found"
	exit 1
fi
case $1 in
  install)
	while IFS='' read -r line || [[ -n "$line" ]]; do
		a=(${line//:/ })
		case ${a[0]} in
			Short | Version | Author | Uploader | Type | Replaces | Architecture)
				echo "$line"
				;;
			Url)
				file=$(basename ${a[2]})
				if [ "${a[1]}" == "git" ]; then
					if [ ! -e "projects/$file" ]; then
						pushd projects
						git clone https:${a[2]} || (rm -rf $file; exit 1)
						popd;
					fi
					pushd projects/$file
					git pull
					popd
					rsync projects/$file/* build/$2/
				else
					if [ ! -e "download/$file" ]; then
						echo wget ${a[1]}:${a[2]} -O download/$file
						wget ${a[1]}:${a[2]} -O download/$file || (rm download/$file; exit 1)
					fi
					if [ ! -e "build/$2" ] || [ "$(ls -l build/$2)" == "total 0" ]; then
						mkdir -p build/$2
						pushd build/$2
							lha x "../../download/$file"
							find . -type d -exec chmod +x {} \;
						popd
					fi
				fi
			;;
			fd2sfd)
				fd=${a[1]}
				proto=${a[2]}
				file=$(basename $fd)
				name=${file%???}
				mkdir -p $3/m68k-amigaos/lib/sfd/
				$3/bin/fd2sfd -o $3/m68k-amigaos/lib/sfd/$name.sfd $3/m68k-amigaos/lib/fd/$fd $3/m68k-amigaos/include/$proto || exit 1
			;;
			clib)
				sfd=${a[1]}
				file=$(basename $sfd)
				name=${file%????}
				mkdir -p $3/m68k-amigaos/include/clib/
				$3/bin/sfdc --mode=clib --target=m68k-amigaos --output=$3/m68k-amigaos/include/clib/${name}_protos.h $3/m68k-amigaos/lib/sfd/$sfd || exit 1
			;;
			sfdc)
				sfd=${a[1]}
				file=$(basename $sfd)
				name=${file%????}
				mkdir -p $3/m68k-amigaos/include/proto/
				rm $3/m68k-amigaos/include/proto/$name.h
				$3/bin/sfdc --mode=proto --target=m68k-amigaos --output=$3/m68k-amigaos/include/proto/$name.h $3/m68k-amigaos/lib/sfd/$sfd || exit 1
				sed -i.bak -e 's/\(<clib\/.*>\)/\L\1/' -e 's/\(<defines\/.*>\)/\L\1/' -e 's/\(<inline\/.*>\)/\L\1/' -e 's/\(<pragmas\/.*>\)/\L\1/' $3/m68k-amigaos/include/proto/$name.h
				rm $3/m68k-amigaos/include/proto/$name.h.bak
				mkdir -p $3/m68k-amigaos/include/inline/
				$3/bin/sfdc --mode=macros --target=m68k-amigaos --output=$3/m68k-amigaos/include/inline/$name.h $3/m68k-amigaos/lib/sfd/$sfd || exit 1
				$3/bin/sfdc --mode=macros --target=m68kvbcc-amigaos --output=$3/m68k-amigaos/include/inline/${name}_protos.h $3/m68k-amigaos/lib/sfd/$sfd || exit 1
				mkdir -p $3/m68k-amigaos/include/lvo/
				$3/bin/sfdc --mode=lvo --target=m68k-amigaos --output=$3/m68k-amigaos/include/lvo/$name.i $3/m68k-amigaos/lib/sfd/$sfd || exit 1
				if [ "${a[2]}" != "" ] && [ "${a[3]}" != "" ]; then
					echo fixup names from ${a[2]} to ${a[3]}
					echo sed -i.bak -e "s/${a[2]}/${a[3]}/" $3/m68k-amigaos/include/proto/$name.h
					sed -i.bak -e "s/${a[2]}/${a[3]}/" $3/m68k-amigaos/include/proto/$name.h
					sed -i.bak -e "s/${a[2]}/${a[3]}/" $3/m68k-amigaos/include/lvo/$name.i
					sed -i.bak -e "s/${a[2]}/${a[3]}/" $3/m68k-amigaos/include/inline/$name.h
					sed -i.bak -e "s/${a[2]}/${a[3]}/" $3/m68k-amigaos/include/inline/${name}_protos.h
					rm $3/m68k-amigaos/include/proto/$name.h.bak $3/m68k-amigaos/include/lvo/$name.i.bak $3/m68k-amigaos/include/inline/$name.h.bak $3/m68k-amigaos/include/inline/${name}_protos.h.bak
				fi
			;;
			stubs)
				sfd=${a[1]}
				file=$(basename $sfd)
				name=${file%????}
				$3/bin/sfdc --mode=autoopen --target=m68k-amigaos --output=build/$2/$name.c $3/m68k-amigaos/lib/sfd/$name.sfd || exit 1
				sed -i.bak -e 's/__inline//g' -e 's/: \"d0\",/:/g' build/$2/$name.c || exit 1
				rm build/$2/$name.c.bak
				echo $3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/$name.c -c
				$3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/$name.c -c -o build/$2/$name.o || exit 1
				echo $3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/libstubs.a build/$2/$name.o
				(
					flock -x 200
					$3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/libstubs.a build/$2/$name.o || exit 1
				) 200>/tmp/amiga-gcc-libstubs-lock
				rm -f /tmp/amiga-gcc-libstubs-lock
			;;
			lib)
				sfd=${a[1]}
				file=$(basename $sfd)
				name=${file%????}
				$3/bin/sfdc --mode=stubs --target=m68k-amigaos --output=build/$2/lib$2.c $3/m68k-amigaos/lib/sfd/$name.sfd || exit 1
				sed -i.bak -e 's/__inline//g' -e 's/: \"d0\",/:/g' build/$2/lib$2.c || exit 1
				rm build/$2/lib$2.c.bak
				echo $3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/lib$2.c -c
				$3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/lib$2.c -c -o build/$2/lib$2.o || exit 1
				echo $3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/lib$2.a build/$2/lib$2.o
				$3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/lib$2.a build/$2/lib$2.o || exit 1
			;;
			liba)
				sfd=${a[1]}
				file=$(basename $sfd)
				name=${file%????}
				$3/bin/sfdc --mode=stubs --target=m68k-amigaos --output=build/$2/lib$2.c $3/m68k-amigaos/lib/sfd/$name.sfd || exit 1
				sed -i.bak -e 's/__inline//g' -e 's/: \"d0\",/:/g' build/$2/lib$2.c || exit 1
				rm build/$2/lib$2.c.bak
			;;
			libc)
				echo $3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/lib$2.c -c
				$3/bin/m68k-amigaos-gcc -Os -noixemul -fomit-frame-pointer build/$2/lib$2.c -c -o build/$2/lib$2.o || exit 1
				echo $3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/lib$2.a build/$2/lib$2.o
				$3/bin/m68k-amigaos-ar r $3/m68k-amigaos/lib/lib$2.a build/$2/lib$2.o || exit 1
			;;
			
			redirinc)
				echo creating redirecting header $3/m68k-amigaos/include/${a[1]}
				echo "#include \"${a[2]}\"" >$3/m68k-amigaos/include/${a[1]}
			;;
			sed)
				x=${a[@]:2}
				x=${x##\"}
				x=${x%%\"}
				echo applying: sed -i.bak -E \""$x"\" $3/${a[1]}
				LC_CTYPE=C sed -i.bak -E "$x" $3/${a[1]}
				rm $3/${a[1]}.bak
			;;
			symlink)
				echo creating symlink from $3/m68k-amigaos/${a[2]} to $3/m68k-amigaos/${a[1]}
				rm -rf $3/m68k-amigaos/${a[2]}
				ln -s $3/m68k-amigaos/${a[1]} $3/m68k-amigaos/${a[2]}
			;;
			stdargs)
				echo adding __stdargs to build/$2/${a[1]}
				LC_CTYPE=C sed -i.bak -E 's/(.*)__stdargs (.*)/\1\2/g' build/$2/${a[1]}
				LC_CTYPE=C sed -i.bak -E 's/([a-zA-Z0-9_]*[[:blank:]]*.*[[:blank:]]+\*?[[:blank:]]*)([a-zA-Z0-9_]+)[[:blank:]]*\(/__stdargs \1\2(/g' build/$2/${a[1]}
				rm build/$2/${a[1]}.bak
			;;
			patch)
				patch=patches/$2/${a[1]}
				echo applying patch $patch to build/$2
				patch -N -p0 -r - -d build/$2 < $patch
			;;
			*)
				if [ "$line" != "" ]; then
					if [ "${a[1]}" == "=" ]; then
						file=${a[2]}
						line=${a[0]}
						cp build/$2/$line build/$2/$(dirname $line)/$file
					else
						file=$(basename $line)
					fi
					dir=$(basename $(dirname $line))
					#lower-case the directory part
					outdir=$(echo "$dir" | tr '[:upper:]' '[:lower:]')
					if [[ $file == *.h ]] || [[ $file == *.i ]]; then
						mkdir -p "$3/m68k-amigaos/include/$outdir"
						echo cp "build/$2/$line" "$3/m68k-amigaos/include/$outdir/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/include/$outdir/$file"
					elif [[ $file == *.guide ]]; then
						mkdir -p "$3/m68k-amigaos/guide"
						echo cp "build/$2/$line" "$3/m68k-amigaos/guide/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/guide/$file"
					elif [[ $file == *.doc ]]; then
						mkdir -p "$3/m68k-amigaos/doc"
						echo cp "build/$2/$line" "$3/m68k-amigaos/doc/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/doc/$file"
					elif [[ $file == *.sfd ]]; then
						mkdir -p "$3/m68k-amigaos/lib/sfd"
						echo cp "build/$2/$line" "$3/m68k-amigaos/lib/sfd/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/lib/sfd/$file"
					elif [[ $file == *.fd ]]; then
						mkdir -p "$3/m68k-amigaos/lib/fd"
						echo cp "build/$2/$line" "$3/m68k-amigaos/lib/fd/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/lib/fd/$file"
					elif [[ $file == *.a ]]; then
						echo cp "build/$2/$line" "$3/m68k-amigaos/lib/$file"
						cp "build/$2/$line" "$3/m68k-amigaos/lib/$file"
					fi
				fi
			;;
		esac
	done < "sdk/$2.sdk"
  ;;
  clean)
	rm -rf build/$2
  ;;
esac
